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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With