完整微服务化示例:使用 Apache ServiceComb 进行微服务开发、容器化、弹性伸缩(三)

阅读数:3 2019 年 12 月 25 日 18:10

完整微服务化示例:使用 Apache ServiceComb 进行微服务开发、容器化、弹性伸缩(三)

养蜂人 (Beekeeper)

养蜂人研究蜜蜂繁殖规律,计算每只蜜蜂 (雄蜂 / 雌蜂) 的祖先数量。因为蜜蜂繁殖规律和黄金分割数列相关,所以养蜂人同时消费技工提供的计算服务。

研究 ( http://www.dave-cushman.net/bee/fibonacci.html ) 表明,雄蜂 (Drone) 由未受精卵孵化而生,只有母亲;而雌蜂 (Queen) 由受精卵孵化而生,既有母又有父。

完整微服务化示例:使用 Apache ServiceComb 进行微服务开发、容器化、弹性伸缩(三)

Credit: Dave Cushman’s website

参考上图,蜜蜂的某一代祖先数量符合黄金分割数列的模型,由此我们可以很快实现服务功能。

蜜蜂繁殖规律研究服务

首先我们定义黄金数列运算接口:

复制代码
public interface FibonacciCalculator {
long term(int n);
}

接下来定义并实现蜜蜂繁殖规律研究服务:

复制代码
interface BeekeeperService {
long ancestorsOfDroneAt(int generation);
long ancestorsOfQueenAt(int generation);
}
class BeekeeperServiceImpl implements BeekeeperService {
private final FibonacciCalculator fibonacciCalculator;
BeekeeperServiceImpl(FibonacciCalculator fibonacciCalculator) {
this.fibonacciCalculator = fibonacciCalculator;
}
@Override
public long ancestorsOfDroneAt(int generation) {
if (generation <= 0) {
return 0;
}
return fibonacciCalculator.term(generation + 1);
}
@Override
public long ancestorsOfQueenAt(int generation) {
if (generation <= 0) {
return 0;
}
return fibonacciCalculator.term(generation + 2);
}
}

这里我们用到之前定义的 FibonacciCalculator 接口,并希望通过这个接口远程调用技工服务端点。@RpcReference 注释能帮助我们自动从 Service Center 中获取 microserviceName = “worker”, schemaId = “fibonacciRpcEndpoint” , 即服务名为 worker 已经 schema ID 为 fibonacciRpcEndpoint 的端点:

复制代码
@Configuration
class BeekeeperConfig {
@RpcReference(microserviceName = "worker", schemaId = "fibonacciRpcEndpoint")
private FibonacciCalculator fibonacciCalculator;
@Bean
BeekeeperService beekeeperService() {
return new BeekeeperServiceImpl(fibonacciCalculator);
}
}

我们在技工一节已定义好对应的服务名和 schema ID 端点,通过上面的配置,ServiceComb 会自动将远程技工服务 实例和 FibonacciCalculator 绑定在一起。

养蜂人服务端点

与上一节技工服务相似,我们在这里也需要提供养蜂人服务端点,让用户可以进行调用:

复制代码
@RestSchema(schemaId = "beekeeperRestEndpoint")
@RequestMapping("/rest")
@Controller
public class BeekeeperController {
private static final Logger logger = LoggerFactory.getLogger(BeekeeperController.class);
private final BeekeeperService beekeeperService;
@Autowired
BeekeeperController(BeekeeperService beekeeperService) {
this.beekeeperService = beekeeperService;
}
@RequestMapping(value = "/drone/ancestors/{generation}", method = GET, produces = APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public Ancestor ancestorsOfDrone(@PathVariable int generation) {
logger.info(
"Received request to find the number of ancestors of drone at generation {}",
generation);
return new Ancestor(beekeeperService.ancestorsOfDroneAt(generation));
}
@RequestMapping(value = "/queen/ancestors/{generation}", method = GET, produces = APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public Ancestor ancestorsOfQueen(@PathVariable int generation) {
logger.info(
"Received request to find the number of ancestors of queen at generation {}",
generation);
return new Ancestor(beekeeperService.ancestorsOfQueenAt(generation));
}
}
class Ancestor {
private long ancestors;
Ancestor() {
}
Ancestor(long ancestors) {
this.ancestors = ancestors;
}
public long getAncestors() {
return ancestors;
}
}

因为养蜂人需要消费技工提供的服务,所以其 microservice.yaml 配置稍有不同:

复制代码
# all interconnected microservices must belong to an application wth the same ID
APPLICATION_ID: company
service_description:
# name of the declaring microservice
name: beekeeper
version: 0.0.1
cse:
service:
registry:
address: http://sc.servicecomb.io:30100
rest:
address: 0.0.0.0:8090
handler:
chain:
Consumer:
default: bizkeeper-consumer,loadbalance
references:
# this one below must refer to the microservice name it communicates with
worker:
version-rule: 0.0.1

这里我们需要定义 cse.references.worker.version-rule ,让配置名称中指向技工服务名 worker ,并匹配其版本号。

最后定义养蜂人服务应用入口:

复制代码
@SpringBootApplication
@EnableServiceComb
public class BeekeeperApplication {
public static void main(String[] args) {
SpringApplication.run(BeekeeperApplication.class, args);
}
}

本文转载自微服务蜂巢公众号。

原文链接: https://mp.weixin.qq.com/s?__biz=MzUxNTEwNTg5Mg==&mid=2247488670&idx=1&sn=7556990599cea8e0fd80ed54b9f39026&chksm=f9bae195cecd688332940d0b296909e8130d828104c36c20f49d76d43561f626eae24bd0c1ef&scene=0&xtrack=1&key=17fbc717c1803f309d535a4bdbc0f9c0d8d2089a5c7e455df8c0a5e6d824a6534b8476d2ad151cd45ed10eb7a15577914596444e867d96f615c277fc05fe951cef48de8b7d0732dcb4bc74f5c0e2f95a&ascene=14&uin=MTI5MjAyNjcyMQ%3D%3D&devicetype=Windows+10&version=62070158&lang=zh_CN&exportkey=AfH3CzqE%2F1ENttvg815y9Uo%3D&pass_ticket=oGcazNeaRfkuszcDU0L7jpfeTFZ3%2FULBAbPnhurUkiyW7DLvBVsoC%2Fh5OWX1zIsH

评论

发布