Each of the services register themselves on Discovery Server to become Microservice. Discovery server is the layer of abstraction between client and Microservices. Even client is required to be registered on Discovery Server.
There are 2 models of service discovery:
1. Client Side Service Discovery
2. Server Side Service Discovery
Spring Cloud uses Client Side Service Discovery. Eureka is one of the technologies which Spring Cloud is integrated with for discovering services. Eureka is Netflix’s project. Netflix is leader in market which provides microservices libraries and made open source that work well with Spring Boot.
Lot more from Netflix:
Eureka – Service Discovery
Ribbon
Hystrix – Circuit Breaker Pattern
Zuul
Spring has layer of abstraction that work well together and since Spring has layer of abstraction so you don’t have to worry much about technologies.
For example:
There is an API which allows you to use data interactions provided by Spring so you don’t mess up with JDBC, you use the Spring abstractions. Then you can possibly change the underlying technology. Let’s say you’re using Hibernate and you can change it to some other JPA providers and Spring code is going to be same. Just change the configuration and spring is going to handle the rest.
Eureka
Eureka Server – Acts as Service Discovery Server
Eureka Client – Eureka clients are basically Microservices which register with Eureka Server and either provides or consumes services.
Steps to making this work:
1. Start the Eureka Server
2. Have microservices register (publish) using Eureka client
3. Have microservices locate (consume) using Eureka client
Creating Eureka Server
1. Use Spring Intializr and create Spring Boot application by adding starter dependency as Eureka Server
2. Annotate Spring Boot application’s main class with @EnableEurekaServer
3. Run the application
Eureka Server is up and running!!!
Creating Eureka Client
1. Use Spring Intializr and create Spring Boot application by adding starter dependency as Eureka Discovery
2. Annotate Spring Boot application’s main class with @EnableEurekaClient
3. Run the application
Eureka Client is up and running and registered with Eureka Server!!!
P.S. If your Java version is above 9, you can encounter with JAXB errors in your application since JAXB libraries are deprecated and support has been removed from Java 9 onward. Eureka libraries uses JAXB functionality so to resolve these errors explicitly add dependencies for JAXB.
Instead of using hardcoded URL, now you can use the name of Eureka Client registered with Eureka server:
RestTemplate restTemplate = new RestTemplate(); User user = restTemplate.getForObject("http://user-details-service/users/1", User.class);
In above example, user-details-service is the name of your microservice registered as Eureka client on Eureka server.
spring.application.name=user-details-service
To enable RestTemplate to discover the service use below annotation on RestTemplate:
@LoadBalanced
It discovers the service in load balanced way.