testotp mail
testotp mail

Reputation: 1

Spring boot Request and response in two different APIs

I have three microservices A , B and C.

A is sending one request(exposed by B) to B for getting some data and B will send the same data request to C in different API (exposed by C).

Now C will send the response for the request to B in another API (exposed by B

Based on the response from C, B will send the response to A in the same API exposed by B.

what is best way to implement this use case.

I have tried to wait the request till the response received from C service

Upvotes: 0

Views: 96

Answers (2)

Michael Gantman
Michael Gantman

Reputation: 7790

You are talking about synchronous and asynchronous communication. What you described on communication between B and C is asynchronous communication which means that B sends a request but only receives an acknowledgment in response to its request. The actual response will be sent when C is ready by separate request from C to B when C is ready with no time frame guarantee. Usually such communications are done using Queues such as Rabit MQ or Kafka - i.e. B can make a regular Http Request to C but see will send a response through Queue for which B should be listening. But your way while less typical is acceptable as well.
The communication between A and B that you described is simple synchronous communication where A sends a request to B and gets the actual answer in response to the original request. The problem with this model where A -> B -> C, since B -> C is asynchronous without any timeframe it really makes it unreasonable (if not impossible) to have A -> B to be synchronous (with short timeframe limitation). If you had it other way around where B -> C would be synchronous and A -> B than could be asynchronous if you wish.
In your case given B -> C being asynchronous I would say that A -> B must be asynchronous as well. Or you can make it that once C is ready with its answer it could send it directly to A, but of course than B has to tell C that it is intended for A. Or more typically C would publish its answer into a queue and Just B or both B and A (in this case instead of Queue you would use a topic) can listen for an answer

Upvotes: 0

beyzanur
beyzanur

Reputation: 38

You can use message queue structures like Kafka, RabbitMQ or AWS SQS.

And you can create a different queue for the other service by triggering the endpoint running in service B by sending a message to the queue while in service A.

For example:

enter image description here

Upvotes: 0

Related Questions