Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async communication between spring boot micro services

I am new to spring boot and created 2 micro services. They need to communicate with one other in synchronous and asynchronous way. For synchronous communication, I can use the RestTemplate. But how to do for asynchronous calling ? my requirement for Asynchonous is: lets say I am querying for something from one micro service. To fetch the queried data it will take sometime because of queried for large sum of data. In this case, I need to save the request into some transaction table and return the response with transactionId and callBackAPI. After sometime if I call the callBackAPI with transactionId. Then I should be able to get the previously queried data.

Please help me with this.

Thanks.

like image 556
Krish Avatar asked Oct 29 '22 01:10

Krish


1 Answers

Two solutions :

Async call from your client : Spring provides an Asynchronous version of the RestTemplate : AsyncRestTemplate with this solution, your client is asynchronous, you don't need to store the data in a table with the transaction id and stuff.

Make your endpoint Asynchronous (if you don't need the response) : Spring lets you create asynchronous methods(services) that you can call from your RestController. With this solution you can do what you described in the question(creating and storing a transaction id that will be returned directly to the client and start the async job).

like image 64
Akli REGUIG Avatar answered Nov 11 '22 14:11

Akli REGUIG