Spring Microservices – Service Registry – Netflix Eureka

Spring Microservices – Service Registry – Netflix Eureka

Service Registry is core of Netflix Spring framework and helps to manage various Microservices to be discvered and used . Lets start with following problem statements that may help you to understand the context better.

  • Microservice exposes remote API such as HTTP/REST which is implemented at particular host and port.
  • Running instances of microservices may changes dynamically due to availability , scalability design of underlying infrastrucutre. Virtual machines and containers are usually assigned a dynamic IP address and instances which may be killed/released and re-created at any point of time.

Naturally for Consumers of those Microservices, this is really hard to keep track of corresponding end points and live instances  without having any proper mechanism. Here, the service registry plays a major role to keep information about how to dispatch requests to avaialble microservice instances under the hood.

Underlying Concept – How Eureka works ?

Service instances are registered with the service registry on startup and de-registered on shutdown. Client of the service and/or routers query the service registry to find the available instances of a service.

Eureka is one of well accepted microservice service regitry now adays tried ,tested and introduced by Netflix team. Eureka supports dynamic service registration and service discovery. When a new service is added, Eureka automatically adds in the service registry and when the service goes down, eureka removes it from its registry. Dynamic service discovery comes in the picture when a client queries the registry with service-id and gets the required metadata required to invoke the service. So service discovery is a lookup service where the critical service’s metadata like host, port. This information is stored in Eureka when a service registers first time.

Service instances send heartbeat signals to Eureka periodically. When a microservice is bootstrapped , it presents its existence along with binding metadata information to Eureka. After registration the microservice sends ping requests to the registry in every 30 seconds and if the service fails to send the heartbeat information in few attempts, Eureka will remove it from its list. Eureka servers maintain latest service information in memory.

Multiple Eureka server runs simultaneously to satisfy high availability architecture and for improved performance. Eureka servers communicate with each other to share microservices states.

Consumers maintain a local cache of registry for faster access. The registry information is propagated to all clients. If a registry state changes , that delta propagates to the clients.Eureka clients fetch the service registry from the Eureka server so it can discover other services and communicate with them. After every 30 seconds, this service registry information gets updated by receiving delta updates from the Eureka server.Eureka is zone aware and when a client requests service with service-id , eureka returns service instances running in the same zone / location  where the requester is hosted.

Ribbon client (we will explain ribbon in coming blogs) performs a client side load balancing between the instances. Communication pattern between Eureka and microservices is JSON over REST.

enough background , let us jump into implementation.

Creating a Eureka Server

a>The below dependency should be added to pom.xml

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-eureka-server</artifactId>

</dependency>

b>The below annotation should be added to the spring cloud application

@EnableEurekaServer

c>property file configurations:

server.port : Server port where eureka runs

eureka.client.serviceUrl.defaultzone : List of other eureka servers , with which this server communicates

example:

server.port=8761

eureka.client.service-url.defaultZone= http://localhost:8762/eureka/

Registering with Eureka Server

a>The below dependency should be added to pom.xml

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-eureka-server</artifactId>

</dependency>

b>The below annotation should be added to the spring cloud application

@EnableDiscoveryClient / @EnableEurekaClient – This annotation automatically registers the microservice (application-name , host , port)with eureka server.

c>property file configurations:

server.port : Server port where service  runs

eureka.client.serviceUrl.defaultzone : Url of eureka server.

You can eplore further from following github project url.

Eureka Server example project : https://github.com/debajyoti1d1mukherjee/ctt-springboot/tree/master/ServiceRegistry

Eureka Client example project : https://github.com/debajyoti1d1mukherjee/ctt-springboot/tree/master/CreateCase

Other service registries that come under spring cloud hood are consul , zookeeper but eureka is the most popular one.  There are fundamental difference between these technologies and we will try to address those aspect in our upcoming blogs.

Don’t forget to comeback and follow our next blog. Happy reading friends  … 😊

 

Author: Debajyoti Mukherjee

Debajyoti Mukherjee has more than 11 years if IT experience in application design , application development. He has worked in large MNCs and has worked for eminent clients. He is proficient in custom development , cloud platform , microservice based application migration.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.