如何使用Spring WebClient同时进行多个调用?

ddz*_*dzz 9 spring spring-boot asyncresttemplate spring-webflux

我想同时执行3个调用,并在完成所有操作后处理结果.

我知道这可以使用AsyncRestTemplate实现,因为这里提到如何使用AsyncRestTemplate同时进行多个调用?

但是,不推荐使用AsyncRestTemplate而使用WebClient.我必须在项目中使用Spring MVC,但感兴趣的是我是否可以使用WebClient来执行同时调用.有人可以建议如何使用WebClient正确完成此操作吗?

xer*_*593 11

假设一个WebClient包装器(如参考文档):

@Service
public class MyService {

    private final WebClient webClient;

    public MyService(WebClient.Builder webClientBuilder) {
        this.webClient = webClientBuilder.baseUrl("http://example.org").build();
    }

    public Mono<Details> someRestCall(String name) {
        return this.webClient.get().url("/{name}/details", name)
                        .retrieve().bodyToMono(Details.class);
    }

}
Run Code Online (Sandbox Code Playgroud)

...,您可以通过以下方式异步调用它:

// ... 
  @Autowired
  MyService myService
  // ...

   Mono<Details> foo = myService.someRestCall("foo");
   Mono<Details> bar = myService.someRestCall("bar");
   Mono<Details> baz = myService.someRestCall("baz");

   // ..and use the results (thx to: [2] & [3]!):

   // Subscribes sequentially:

   // System.out.println("=== Flux.concat(foo, bar, baz) ===");
   // Flux.concat(foo, bar, baz).subscribe(System.out::print);

   // System.out.println("\n=== combine the value of foo then bar then baz ===");
   // foo.concatWith(bar).concatWith(baz).subscribe(System.out::print);

   // ----------------------------------------------------------------------
   // Subscribe eagerly (& simultaneously):
   System.out.println("\n=== Flux.merge(foo, bar, baz) ===");
   Flux.merge(foo, bar, baz).subscribe(System.out::print);
Run Code Online (Sandbox Code Playgroud)

[2] [3]

谢谢,亲切的问候,

  • @ xerx593请使用`Flux.merge`更新您的答案,因为`Flux.concat`会顺序订阅(不会同时进行REST调用)-`Flux.merge`会急切地订阅所有发布者。 (2认同)

Jus*_*tas 6

RestTemplate您可以使用简单的and并发进行 HTTP 调用ExecutorService

RestTemplate restTemplate = new RestTemplate();
ExecutorService executorService = Executors.newCachedThreadPool();

Future<String> firstCallFuture = executorService.submit(() -> restTemplate.getForObject("http://first-call-example.com", String.class));
Future<String> secondCallFuture = executorService.submit(() -> restTemplate.getForObject("http://second-call-example.com", String.class));

String firstResponse = firstCallFuture.get();
String secondResponse = secondCallFuture.get();

executorService.shutdown();
Run Code Online (Sandbox Code Playgroud)

或者

Future<String> firstCallFuture = CompletableFuture.supplyAsync(() -> restTemplate.getForObject("http://first-call-example.com", String.class));
Future<String> secondCallFuture = CompletableFuture.supplyAsync(() -> restTemplate.getForObject("http://second-call-example.com", String.class));

String firstResponse = firstCallFuture.get();
String secondResponse = secondCallFuture.get();
Run Code Online (Sandbox Code Playgroud)

  • 值得注意的是 RestTemplate 很快就会被弃用。 (11认同)
  • 唯一的:https://docs.spring.io/spring/docs/current/javadoc-api/index.html?org/springframework/web/client/RestTemplate.html (2认同)

Sar*_*oev 5

您可以使用 Spring 反应式客户端WebClient发送并行请求。在这个例子中,

public Mono<UserInfo> getUserInfo(User user) {
        Mono<UserInfo> userInfoMono = getUserInfo(user.getId());
        Mono<OrgInfo> organizationInfoMono = getOrgInfo(user.getOrgId());

        return Mono.zip(userInfoMono, organizationInfoMono).map(tuple -> {
            UserInfo userInfo = tuple.getT1();
            userInfo.setOrganization(tuple.getT2());
            return userInfo;
        });
    }
Run Code Online (Sandbox Code Playgroud)

这里:

  • getUserInfo 进行 HTTP 调用以从另一个服务获取用户信息并返回 Mono
  • getOrgInfo 方法进行 HTTP 调用以从另一个服务获取组织信息并返回 Mono
  • Mono.zip() 等待来自所有单声道的所有结果并合并为一个新的单声道并返回它。

然后,调用getOrgUserInfo().block()以获取最终结果。