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

时间:2018-05-06 19:48:42

标签: spring spring-boot spring-webflux asyncresttemplate

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

我知道这可以使用AsyncRestTemplate实现,因为这里提到How to use AsyncRestTemplate to make multiple calls simultaneously?

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

4 个答案:

答案 0 :(得分:6)

假设一个WebClient包装器(如reference doc中所示):

@Service
public class MyService {

    private final WebClient webClient;

    // the reference doc seems incorrect saying "public MyBean(" :)
    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);
    }

}

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

// ... 
  @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);

[2] [3]

谢谢,欢迎&amp;亲切的问候,

答案 1 :(得分:3)

您可以使用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;
        });
    }

这里:

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

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

答案 2 :(得分:0)

另一种方式:

public Mono<Boolean> areVersionsOK(){
        final Mono<Boolean> isPCFVersionOK = getPCFInfo2();
        final Mono<Boolean> isBlueMixVersionOK = getBluemixInfo2();

        return isPCFVersionOK.mergeWith(isBlueMixVersionOK)
            .filter(aBoolean -> {
                return aBoolean;
            })
            .collectList().map(booleans -> {
                return booleans.size() == 2;
        });

    }

答案 3 :(得分:0)

您可以使用简单的RestTemplateExecutorService同时进行HTTP调用:

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();

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();