Microservices : Service Registration and Discover in Netflix Eureka

 

The importance of Service Register and Discovery

In the Microservices architecture, your application may consist of many number of microservices. each of them may be deployed in different servers and different ports.  The microservices may need to communicate with each other to execute some tasks or operations. For instance, one microservice may need to access the service endpoint  (REST endpoint) in some other microservice for application related operation.  “how do they communicate with each other?” 

If I ask you this question, you will directly tell me that one service can access the other service with the IP address and the port number of that service.

YES! you are correct. But keep this in mind that this approach (using ip address and port number) has following limitations.

  • It is not practical to know the IP address and port number of each microservice as there are multiple microservices available. Assume that there are hundred of microservices available. In this case, do you think that it is practical to know the ip address and port number of each service? It is not right?

 

  • Assume a situation where a microservice is migrated to (deployed in) some other server with different IP address and port. If we have used the IP address and port number in the source code for making RestClient requests, then we will have to change them in the source code and re-build and deploy the affected services. whenever the ip address or port number get changed, we have to do the same. Dont you think that it is a sort of bad coding and programming practice? Absolutely it is right? (The issue raised again in a situation where we have multiple environments like devqauat and production)

 

 

How do we overcome above problems and move forward?

The solution is “Service Registration and Discovery“.

The microservices should register themselves in the centralized location with an identifier and server details. This is known as “Service Registration“.  Each microservice should be able to look up the list of registered services in the centralized location and it is known as “Service Discovery

 

What are the implementation for the Service Registration and Discovery?

There are multiple implementations for the Service Registration and Discovery Server

  • Netflix Eureka
  • Consul
  • Zookeeper

In this article, we are going to discuss about Netflix Eureka

 

 

What is Eureka Server? How it works?

Eureka Server is an implementation for the “Service Registration and Discovery” pattern. Eureka Server keeps a track of registered microsevices and therefore It is known as registry of microservices (that are related to the application).

Each microservice should register with Eureka server by providing their details such as host name, ip address, port, and health indicators etc… Then the Eureka service will register each microservice with unique identifier known as serviceId. The other services can use this serviceId to access the particular service.

The Eureka server will maintain a list of registered microservices with their provided details. Eureka server expects continuos ping messages (known as heartbeats) from the registered microservices to verify they are alive (up and running).  If any service fails to send the heartbeat (ping message) continuously, it will be considered as a dead service and will be removed from the registry.  Therefore Eureka server will maintain only a registry of up and running microservices.

On the other hand, the microservices can register themselves as service discover clients with Eureka Server. The Eureka Server will allow the discover clients (microservices) to look up the other registered services and fetch their information. If any service needs to communicate with any other service, it can register with the Eureka server as a discover client and fetch the information of the targeted service (server address and port number etc…).  In this way, microservices can communicate with each other without maintaining the IP addresses and port numbers manually.

 

Lets do some codings …..

The full source related to this article can be found at GitHub.

Eureka Server

First we will set up the Eureka server.  Eureka Server is just another Spring Boot Application which has the Spring Cloud Netflix Eureka in its classpath dependencies.

Therefore create a spring boot application with Eureka dependency.

eureke-server

 

Then  add the @EnableEurekaServer annotation to the main Spring Boot Application configuration class (That is the class annotated with @SpringBootApplication annotation)

 

application.properties (Eureka Server)

 

This is the Eureka server application and it is a spring cloud managed service. Therefore it has the similar nature as other microservices. In microservice architecture, every microservice(spring cloud service) should be registered themselves with Eureka server to make them discoverable (through Eureka server).  Since this is the Eureka server, it does not need to register. So we have disabled the registration as follows. Otherwise the Eureka server will register with itself.

eureka.client.register-with-eureka=false

 

On the other hand, Eureka server does not need to fetch the information of the registered services from itself (from the registry available on itself). So we have disable that feature too.

eureka.client.fetch-registry=false

 

Go to the source directory of the Eureka server application and execute the folllowing command to run the application.

mvn spring-boot:run

 

The application will be up and run on port 8761 as configured.

Screen Shot 2018-03-15 at 11.39.24 PM.png

 

You can see that Eureka server does not have any registered services (No instances available).  Lets create a sample service and register it with Eureka server.

 

 

Student Service

Here we are going to create a simple application with few REST endpoints. We will register this application in the Eureka server as a Eureka Discoverable Client.

Create another spring boot application with following dependencies.

  • Eureka Discovery
  • Actuator
  • Web

 

Screen Shot 2018-03-12 at 3.03.32 PM.png

 

Note: here i am just adding only the selected source code. If you want to refer the full source code, please clone it from GitHub.

 

After creating the project, your pom.xml should contains the following dependencies.

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

 <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
 </dependency>

 

 

Why the Actuator dependency is added here?

Actuator contains the health indicators used by Eureka Server to determine the health of the service. (By default, Eureka server set the URL to the /info of the service to check the health)

 

@EnableDiscoveryClient annotation has been added in the main spring boot configuration class to make the service as a Discoverable client.

 

Sometimes you might have noticed that @EnableEurekaClient has been used instead of @EnableDiscoveryClient annotation. Lets look at their difference.

@EnableDiscoveryClient  annotation can work with any Discovery Client implementations which implements in your project ( Eureka, Consul, Zookeeper ) You can also use @EnableEurekaClient annotation but it works only with Eureka Discovery Client implementation.

@EnableDiscoveryClient lives in spring-cloud-commons and picks the implementation on the classpath. @EnableEurekaClient lives in spring-cloud-netflix and only works for Eureka.

 

application.properites (student-service)

 

spring.application.name = student-service

The service will be registered in the Eureka Server with name “student-service“. The other registered services will use this name as the service-id to access this service.

 

eureka.client.serviceUrl.defaultZone = http://localhost:8761/eureka/

This is the URL of the Eureka deployment. You have to mention location of the Eureka Server by using defaultZone propertyWith leaseRenewalIntervalInSeconds you can change the Registration time. Registration takes up to 30 s because that is the default client refresh time (It is recommend not to change leaseRenewalIntervalInSeconds and use the default value which is 30 sec).

 

The service can be up and run with following command.

mvn spring-boot:run

 

Then the service will be up and run on the port 8098 of your local machine. If you look at the StudentController.java, you can find more REST endpoints. Lets check whether the service is up and running with following endpoint.

 @GetMapping("/name")
 public String getControllerName()
 {
    return "StudentController - www.SpringBootDev.com";
 }

 

http://localhost:8098/name

Screen Shot 2018-03-17 at 10.59.02 AM.png

 

 

Is Application Registered in Eureka?

Now it is time to check whether the application is really registered in Eureka server.  Lets open the Eureka admin panel  (http://localhost:8761/) check whether it is already registered.

 

Screen Shot 2018-03-17 at 12.19.08 PM.png

 

You can see that it is registered with name “STUDENT-SERVICE”  and listed under the list of “Instances currently registered with Eureka“.

student-service” is the name provided by the spring application with property spring.application.name in application.properties.

 

Summary

In this article, we explored how to use the Eureka server for “Service Registration and Discovery“. We have created two spring applications (services). One is for Eureka server and the other one is for representing the real application (student-service).  student-service has been registered in the Eureka server a s discoverable client. This allows the other services to discover the service through Eureka server.

 

In the next article, we will look at how to use the serviceId and Eureka server to perform the routes mapping of Netflix Zuul Proxy. (Eureka ServiceId based Zuul routes mapping)

 

 

One thought on “Microservices : Service Registration and Discover in Netflix Eureka

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s