Spring Boot – how to communicate between microservices?

Spring Boot – how to communicate between microservices?

Of course you can.
Microservices are just REST-Services.
You need to understand how REST-Services work.
After that just write 2 Microservices (2 Rest-Services: producer-service and consumer-service) with Spring-boot, let them run under different server-ports, call the consumer-service from the other, and thats it: you have your Microservices.
Now this is the primitive way to write Microservices.

To make them evolve, you need to add some magic (no rocket science), for example using Ribbon to distribute load between two instances of your producer-service.

You may use a discovery service which is just a spring-boot application with the annotation @EnableEurekaServer (You need to add the appropriate dependency in your pom)
Now add to your first (primitive) Microservices the annotation @EnableDiscoveryClient to the main classes and the defaultZone pointing to your eureka-service in the application.properties (or application.yml) of both, start your eureka-service (discovery service) and the 2 Microservices: those will register on the discovery-service. Of course now you dont need to hard-code the http address of the producer-service in the consumer-service.
Take a look at this tutorial

Edited on 21th of November 2018 at 12:41 GMT

Suppose that your first (trivial) microservice (a pure rest-service) is running on your PC under port 8091.

In the controller of your second (trivial) microservice you call your first service using the RestTemplate.getForEntity(url,responseType,uriVariables) like so for the example in the linked tutorial:

ResponseEntity<CurrencyConversionBean> responseEntity = 
   new RestTemplate().getForEntity(
        http://localhost:8091/currency-exchange/from/{from}/to/{to}, CurrencyConversionBean.class,  uriVariables);

url: the url of your first (micro)(rest)service.
responseType: the class/type of the object awaited as response.
uriVariables: is a map containing variables for the URI template.

There are different ways to communicate between microservices. But which one to use: depends on the usecase.

  1. Api call: That is making actual rest api call to the other service using RestTemplate , FeignClient etc as.
ResponseType obj=  new RestTemplate().getForObject(URL, ResponseType.class, params);
  1. But what if the usecase is different, like, you have customer microservice and orders microservice both are using separate database . You have customer name and other details in orders database as well. Once the customer update their name, you have to update the details in Orders database as well . How this can be done. Through API call? Then what if account microservice also needs this update. So Rest api will be an overhead. In this use case we can use MessageQueues like RabbitMQ. Customer microservice will create an event of customer update and which ever microservice is interested in this can subscribe.

Communication through message queue like RabbitMQ

Spring.io rabbit mq guide

Spring Boot – how to communicate between microservices?

That depends on your choice, weather you want sync communication or async communication between your services.
For sync services you can use either of these 3rd party tools:

  1. Hashcorp Consul
  2. Netflix Eureka [You may client load balancing using Netflix RIBBON ]
  3. NATS

For Async you can use messaging solutions like:

  1. Redis [use list/streams]
  2. ActiveMQ
  3. RabitMQ
  4. Kafka
  5. NATS

Leave a Reply

Your email address will not be published. Required fields are marked *