As you create more microservices, it is hard to keep track. Eureka helps in discovering and locating the services. It acts as a load balancer and service registry. All the services are identified by their names without port information. If service A running on port 8080 at URL http://localhost:8080 registers itself as SERVCIEA on Eureka, other services on Eureka can call it as SERVCIEA instead of calling http://localhost:8080.
If you rather watch the video on what’s in this post, see below, otherwise continue reading.
Eureka acts as an internal DNS and middle tier load balancer. With Eureka, load balancing happens at the instance level and the client instances know the information about which servers they need to talk to, making it ideal for the client-based load balancer.
To implement it follow the steps listed below.
Go to https://start.spring.io/ and download the project with Eureka Server as a dependency.
Add the following text to the application.properties.
Below, we are configuring the name for this service, port to run at and telling it not to register itself as a service.
1 2 3 4 5 6 7 8 |
spring.application.name=eureka-service server.port=8761 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false logging.level.com.netflix.eureka=OFF logging.level.com.netflix.discovery=OFF |
Add the following annotation to SpringBoot main class and run the application.
1 |
@EnableEurekaServer |
Now go to http://localhost:8761/ and you should see a screen like this.
Create 2 additional microservices, so they can talk to each other via Eureka.
Go to https://start.spring.io/ and download 2 projects with Eureka Discovery as a dependency. One would be ServiceA and other would ServiceB.
Add the following annotation to Spring boot main classe for both ServiceA and ServiceB applications.
1 |
@EnableDiscoveryClient |
Create application.yml in both the applications in resource folders.
Add the following in serviceA’s applcation.yml, the name of this application, port and Eureka information to register itself as a service with Eureka.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
spring: application: name: serviceA server: port: 8080 eureka: client: registerWithEureka: true fetchRegistry: true serviceUrl: defaultZone: http://localhost:8761/eureka/ instance: hostname: localhost |
Add the following in serviceB’s applcation.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
spring: application: name: serviceB server: port: 8081 eureka: client: registerWithEureka: true fetchRegistry: true serviceUrl: defaultZone: http://localhost:8761/eureka/ instance: hostname: localhost |
Add the following endpoint to Spring boot main class in ServiceA.
1 2 3 4 5 6 7 |
@RestController class MessageRestController { @RequestMapping("/contact") public String contact() { return "I am Service A, Congratualtions, you contacted me thru Eureka"; } } |
We will be calling this endpoint “/contact” from ServiceB.
Go ahead and run ServiceA and go to http://localhost:8761/ you should see ServiceA listed as below with Eureka.
Add the following to Spring boot main class in ServiceB to call the ServiceA’s endpoint above.
1 2 3 4 5 6 7 8 9 10 11 12 |
@RestController class MessageRestController { @Autowired private RestTemplate restTemplate; @RequestMapping("/cotactserver") public String cotactserver() { String url = "http://<strong>SERVICEA</strong>/contact"; return restTemplate.getForObject(url, String.class); } } |
Notice the URL in the code above, we are not referencing ServiceA with complete URL as http://localhost:8080, rather with the name that it registered itself with Eureka.
Run ServiceB and check Eureka http://localhost:8761, it should show both the services.
Go to the above endpoint http://localhost:8081/cotactserver it will call SERVICEA and you should see the output as below.
Download eureka.zip for source code in my Microservices Git repo
Here are few links for additional information.
https://spring.io/guides/gs/service-registration-and-discovery/
https://github.com/Netflix/eureka/wiki/Eureka-at-a-glance