Search code examples
javaspringspring-bootmicroservices

How does the one Spring Boot microservice know how it can send a POST to another service?


In this tutorial (Spring Cloud Microservices), the code from this repository is used to illustrate the example:

https://github.com/saturnism/spring-cloud-gcp-guestbook/tree/master/1-bootstrap

In the repo, two Maven projects are found using Spring Boot (guestbook-frontend and guestbook-service). Both are run as a service. Via the frontend service, messages can be posted to the backend service. My question is, where is it configured such that the frontend knows where the POSTs go to? Where is the connection between the two?


Solution

  • In order to explain this question in detail, I will highlight the core patterns of microservices

    1. Core Development Patterns
    2. Routing Patterns
    3. Client Resiliency Pattern
    4. Security Pattern
    5. Logging and Tracing Pattern
    6. Build and Deployment Pattern

    Above mentioned patterns are used to deal with problems that come when we move onto microservices. One of the problems which your question is highlighting is that "We can never be sure which machine will be running our microservice" One naive approach to resolve this problem is that we maintain some kind of map with all the details of machines and its services. And in order to establish communication between services we can use that map. But this approach is naive and it has its own problems.

    SO to deal with this we will have to make use of Routing Patterns. In Routing Patterns there is a concept of Service Discovery There are different ways to do Service Discovery that is provided by different platforms. For instance: Netflix provides Eureka and Feign Client which later on was added in Spring Cloud, HashiCorp provides Service Discovery with Consul, and AWS ECS also has its service discovery mechanism.

    Having said that I can conclude that there are different ways by which you can call another service from one microservice of spring-boot. But since you are more concerned about spring cloud, In spring Cloud you would need Eureka Server and Feign Client. Adding them to your project is fairly simple.

    you can follow this tutorial to use Eureka and Feign Client https://dzone.com/articles/microservices-communication-feign-as-rest-client