Search code examples
javaspring-bootmicroservicesspring-cloudspring-cloud-feign

Make Simultaneous Rest API calls using FeignClient in Spring Boot


Let's say I have 2 microservices A and B. Now for a particular request on service A, A needs to make multiple API calls of the same API to B(with different parameters). Initially, I had used feign client for making rest calls between microservices[with implicit support for load balancing, service discovery and adding port metadata etc. from Feign client].

The problem with this approach, the calls are synchronous and are taking a lot of time. How/ What to use to fire off multiple requests and wait for the response in a non-blocking and asynchronous way?

TIA!


Solution

  • I think you can consider Spring WebClient which is an asynchronous, non-blocking solution provided by the Spring Reactive framework.

    And of course, you can make simultaneous calls too. Example,

    Mono<String> response1 = request1();
    Mono<String> response2 = request2();
    
    Mono.zip(response1, response2)
            .flatMap(result -> transformer(result));