[英]What is the best approach or design pattern to call multiple microservices several times from a single service?
Microservice A has dependency on Microservice B &C.微服务 A 依赖于微服务 B & C。 When client calls a certain endpoint on service A, that will cause multiple HTTP requests to services B & C from A to get the dependent details.
当客户端调用服务 A 上的某个端点时,这将导致从 A 到服务 B 和 C 的多个 HTTP 请求以获取依赖项的详细信息。 What would be the optimal, performance effective design pattern or approach to handle this scenario simultaneously?
同时处理这种情况的最佳、性能有效的设计模式或方法是什么?
NB : We are not using API gateway in this case.注意:在这种情况下我们没有使用 API 网关。
Based on your question I assume there is no ability to leverage the event-based/reactive approach , and architectural decision is already made with tradeoffs as considered here (note, in this source the approach proposed below is referenced as a 'Hybrid').根据您的问题,我认为没有能力利用基于事件的/反应式方法,并且已经根据此处考虑的权衡做出架构决策(请注意,在此来源中,下面提出的方法被称为“混合”)。
Orchestration编排
Under these conditions, the pattern you're looking for is called Orchestration .在这些条件下,您要查找的模式称为Orchestration 。 Check out this great answer for wider overview.
查看这个很棒的答案以获得更广泛的概述。
As a quick recap, you can use something like Spring Integration to implement the following key points:快速回顾一下,您可以使用Spring Integration 之类的东西来实现以下关键点:
Update - rely on implementation of Reactor pattern on client side更新 - 依赖于客户端Reactor 模式的实现
If you can use Spring 5/Spring Boot 2.x, you can also make the calls to B & C in a reactive way using Spring WebFlux based on Project Reactor to achieve above points.如果您可以使用 Spring 5/Spring Boot 2.x,您还可以使用基于Project Reactor 的Spring WebFlux以响应方式调用 B&C 来实现上述几点。
Schematically, you can do something like:从原理上讲,您可以执行以下操作:
@Service
public class MyService {
private final WebClient webClient;
...
public Mono<Details> someRestCall(String name) {
return this.webClient.get().url("{name}/details", name)
.retrieve().bodyToMono(ResponseEntity.class);
}
}
...
Mono<ResponseEntity> b1 = myService.someRestCall("serviceB");
Mono<ResponseEntity> c1 = myService.someRestCall("serviceC");
Mono<ResponseEntity> b2 = myService.someOtherRestCall("serviceB");
ResponseEntity response = Flux
.parallel(NUM_CPUS)
.merge(b1, c1, b2)
.limitRequest(MAX_REQUESTS)
.onErrorReturn(ERR_RESPONSE_ENTITY)
.blockLast(CUTOFF_TIMEOUT);
(based on this example ) (基于这个例子)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.