Spring Boot - how to communicate between microservices?

后端 未结 6 2149
天涯浪人
天涯浪人 2021-02-07 15:41

I\'m currently working on a Spring Boot microservices project. I have created services and each service is running separately. With this, I need some services to communicate wit

6条回答
  •  醉话见心
    2021-02-07 16:33

    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

提交回复
热议问题