Satya
Satya

Reputation: 21

Two way communication between two micro services (spring boot)

I have two micro services M1 and M2, I need to send request to M2 from M1, and I need to wait for the response from M2, because the code works based on the response from M2 How to implement this in spring boot?

Upvotes: 1

Views: 4821

Answers (5)

Rouzbeh Zarandi
Rouzbeh Zarandi

Reputation: 1082

in most of case its always recommended to use asynchronous communication between services to maintain them decouple. if you want use asynchronous communication for your case you need to implement bio-directional event channel as @Harry Coder. how ever its require to implement patterns such as SAGA,CAP Theory, circuit breaker to bring reliability to such implementation specially your case since you waiting for immediate response.

if immediate response is critical you can use synchronous communication such as API call or gRPC. if you need performant and fast communication gRPC worth to implement for those services. it is good to use libraries like Mass Transit for less coupling in your implementation.

Update
if you insist using synchronous communication and the response is critically important i recommend choosing gRPC. it let you provide two way communication.

you can setup a service as client or server or both server/Client which the third implementation stablish two-way communication between M1 and M2.

in gRPC you need to write protocol called protobuffer which makes this communication robust. it is worth to consider it over rest in some cases.

enter image description here

Upvotes: 0

pan-leszeczek
pan-leszeczek

Reputation: 142

You create REST API in M2 that will consume or respond to your input from M1

for instance you may have following endpoint in your @RestController annotated class in M2:

@GetMapping("/consumer")
public ResponseEntity<YourResponseDTO> produceResponse(){
    YourResponseDTO result = resultOfYourBusinessLogic();
return ResponseEntity.ok(result);

Then in your M1 service the easiest in my opinion is to use RestTemplate to call M2 Here is an example:

RestTemplate rt = new RestTemplate();
YourResponseDTO result = rt.getForObject("http://urlOfYourM2/consumer", YourResponseDTO.class);

Please note that this is just an example of call for GET request that produces "YourReponseDTO" Object. You gave very less details about the actual needs of the message exchange and thus the RestTemplate may be used in different ways (different types of request PUT/POST/etc...) you can customize it according needs with error handlers, connection timeout settings etc.

Downside of this approach is that you need to share between M1 and M2 a DTO model to properly map the response objects. But anyways RestTemplate is a client implementation you can use to communicate synchronously between microservices

Upvotes: 1

Souf KMD
Souf KMD

Reputation: 199

You can use synchronous communication with the declarative Rest client "Spring Cloud OpenFeign" as bellow :

Spring Cloud - Synchronous Communication with Feign

Upvotes: 0

Harry Coder
Harry Coder

Reputation: 2740

There are many ways to do it. I will suggest you to implement a producer/consumer pattern between M1 and M2 with a messaging tool such as Active MQ, Rabbit MQ or Kafka.

  1. M1 produce a message to a queue Q1
  2. M2 process the message from Q1
  3. M2 returns a response to Q2
  4. M1 consume the message from Q2

Take a look here.

Upvotes: 2

Umberto
Umberto

Reputation: 44

Create a REST API in your M1 micro service through a controller and then call it from M2 using some API client library.

Upvotes: 1

Related Questions