In this article, we will discuss how to do Microservice Registry Discovery using Netflix Eureka. We will see an example of a Microservice and also will register it with Netflix Eureka and then will discover a Microservice using the Eureka server.
Table of Content
- What is Eureka
- Why we use Service Discovery
- Eureka Registry
- Making microservice to act as a Eureka Server
- Make your Application as a client of Eureka Server
Microservice Registry Discovery using Netflix Eureka Component
What is Eureka
Eureka is a Component of Netflix which is integrated with spring boot to create microservices architecture.
In this post, we will discuss the Eureka service registry/ discovery with the help of annotation. How simple annotation help us to configure a service register and help to discover the server.
Why we use Service Discovery
Suppose we have many microservices which are distributed on the different server. Where instances of the service dynamically change due to some property changes like, IP Address, a name of the instance, and failures which is common nowadays. Then in this situation service discovery help us to automatically identify the server by communicating with each other. Let’s see the following diagram:
As you can see in the above diagram, this is how services register itself with Eureka server to make available to other services.
Eureka Registering
First of all, Microservices add dependency of Eureka Client in the pom.xml file. And when run the application, Microservices provide metadata about itself to the Eureka Server like Hostname, the port on which service is available, Health indicator URL if you have added actuator in the classpath.
Now Eureka communicate with each other with the help of Heartbeat protocol to check the instance is available or not. If an instance is failed then eureka server remove that instance from the configuration table.
Making microservice to act as a Eureka Server
To implement Eureka server to act as the service registry, You need to add a spring-cloud-started-eureka-server dependency in your pom file:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
And you need to add property information in the YAML file like port information, Make this application as a eureka server, not Eureka client by using registerWithEureka false. Let’s see the following diagram:
After adding this dependency to the application YAML file. Then you need to enable the Eureka server in a Sprtingboot main application class using @EnableEurekaServer annotation. Which is basically check your classpath weather the jar is available or not if available then it will make your application as a Eureka server.
package com.dineshonjava; @SuppressWarnings("unused") @SpringBootApplication @EnableEurekaServer public class DiscoveryMicroserviceServerApplication { public static void main(String[] args) { SpringApplication.run(DiscoveryMicroserviceServerApplication.class, args); } }
You can verify it by using URL http://localhost:1111 to view the Eureka Dashboard. Let’s see the following diagram about the Eureka Dashboard:
Make your Application as a client of Eureka Server:
Now Add below dependency in your client application to make your microservice to register with Eureka Server which Continuous communicating with each other to give information about available services in the microservice architecture application.
1. Add below dependency in the pom file
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
2. Add the URL of the Eureka Service on which Eureka server is available and where this Microservice will register itself in the application.yaml file.
eureka: client: serviceUrl: defaultZone: http://localhost:1111/eureka/
3. Add the annotation @EnableDiscoveryClient in the main application file where the @SpringBootApplication annotation is used.
Now Run the Eureka Server application first then run the client application. It will take maximum 30 sec to register client application with Eureka server. Refresh the Eureka server application to reflect the Eureka client application in the dashboard.
Another nice article from Fahim. Good Luck.
Really you guys doing a good job.
Instead of doing this can you guys give some working example and sample code.
Thank you, Dinesh.
Please add some working example as well. So, a reader could access from GIT.
Thank you.
Nice article. Very informative one. Keep writing.
Thanks a lot for the post. Really thank you! Much obliged.
Thanks a lot for the article post. Much thanks again. Fantastic.
Wow nyc.. Everything is well explained… Well done keep it up… 🙂