Netflix Eureka Discovery – Microservices
Learn about using Netflix Eureka with microservices to monitor and maintain a registry of all the microservices in the ecosystem.
Join the DZone community and get the full member experience.
Join For FreeIn the headline, we saw three buzzwords:
- Microservices
- Netflix Eureka
- Discovery (Registry)
What Is a Microservice?
In simple words, microservice(s) are clusters of small applications that work together in coordination to provide a complete solution.
When we say a lot of small applications running independently together, then they will all have their own URLs and ports. In that scenario, it would be very cumbersome to maintain all these microservices to run in synchronization, and more importantly, with monitoring. This problem will increase manifold when we start implementing load balancers.
To solve this issue, we need a tool that will monitor and maintain the registry of all the microservices in the ecosystem.
What Is Netflix Eureka?
This is a tool provided by Netflix to provide a solution to the above problem. It consists of the Eureka Server and Eureka clients. Eureka Server is in itself a microservice to which all other microservices registers. Eureka Clients are the independent microservices. We will see how to configure this in a microservice ecosystem.
I will be using Spring Boot to create a few microservices which will act as Eureka Clients and a Discovery Server which will be a Eureka Server. Here is the complete project structure:
Let’s Discuss the Eureka Discovery Server
This is the Eureka Server, and for that, we have to include a Eureka dependency in the project. Below is the pom.xml for Eureka Discovery Server.
Also, we need to update the properties file for this project to indicate that is a discovery server and not a client.
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
To bind the discovery application to a specific port and name the application, we need to add the following, as well:
server.port=8000
spring.application.name=DiscoveryServer
One last thing to do is to annotate the Spring Boot application to enable this as a Eureka Server. To do so, we need to add @EnableEurekaServer
.
Boot up the application we will see a UI provided by Eureka to list all the servers that get registered. But, at this point, we have none!
Now, let’s add a few microservices to the ecosystem and register them to the discovery server. For this, we also need to add the dependencies required in each service and register them to the server. We will see how in the details below.
I have created three simple microservices (microservice1, microservice2, microservice3) with Spring Boot and each one running on its own port (8002, 8003 and 8004).
As a client, it should register itself to the server, and that happens in the property file, as below.
The main application will be annotated with @EnableEurekaClient
in each microservice.
Boot up this application to run on port 8004 and it will automatically register itself to the discovery server. In a similar manner, I have created two more microservices and registered them on the discovery server.
We can see that three servers are running in the ecosystem and we can monitor the status of these servers, too.
This eases the monitoring of all the servers and their replicas in case we are using a load balancer.
I hope this will help you get started using Discovery Server and Clients using Eureka.
Reference the Git repository containing the codes for all the projects in this application!
Published at DZone with permission of Sovan Misra, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments