简体   繁体   English

从单个服务多次调用多个微服务的最佳方法或设计模式是什么?

[英]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 之类的东西来实现以下关键点:

  • While processing the request to A, execute calls to B & C concurrently where possible to achieve quickest response time from A在处理对 A 的请求时,尽可能同时执行对 B 和 C 的调用,以实现 A 的最快响应时间
  • Accumulate, transform and aggregate the results of concurrent calls into complete response entity将并发调用的结果累加、转化、聚合成完整的响应实体
  • Leverage thread pools to limit concurrently-running requests to B and C to prevent amplification of cascading failures利用线程池限制对 B 和 C 的并发运行请求,以防止级联故障放大
  • Fail fast: early cancel the subsequent bunch of calls if some of requests fails (ie do not call C if call to B was not successful)快速失败:如果某些请求失败,则提前取消随后的一系列调用(即,如果调用 B 不成功,则不调用 C)
  • Cut-off: involve the maximal processing time you can wait for completion of currently-running bunch of calls to B & C and respond with error by A upon elapsed截止:涉及您可以等待当前正在运行的 B&C 调用的完成的最大处理时间,并在经过 A 时以错误响应

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM