Deekshith Anand
Deekshith Anand

Reputation: 2662

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!

Upvotes: 0

Views: 4342

Answers (2)

Sam
Sam

Reputation: 4284

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

Upvotes: 1

jayaneetha
jayaneetha

Reputation: 77

You can use ExecutorServices to create two tasks and use CompletableFutures to capture the two responses from the two microservices

Upvotes: 1

Related Questions