Service discovery with Netflix Eureka

SivaKumar
3 min readAug 10, 2020

--

In this article we will be discussing about service discovery in microservices and one simple client side discovery using Netflix Eureka.
First what is service discovery? When we deploy an application in containerised platforms the clients who wants to call/invoke these services should know the location details of the application like IP address,port, etc details.We can give these details to the application’s team but there is one problem. When you deploy the application in cloud/kubernetes based platform , application’s instance/host details change dynamically. And also when you scale up/down the application the instances change dynamically. So it is not possible to have constant location details here.

So to solve this problem we can use the Netflix Eureka. Eureka is REST service that is used to locate the service’s instances by the clients. Services register to the Eureka registry(Eureka server). So when the applications are up , services register themselves with Eureka server and every 30 seconds they send heartbeats to the registry to inform that it is up. If no heartbeat sent within 90 seconds the service gets de-registered from the registry.

So the clients whoever need the instance/IP/hostnames, can query registry and get it.

Below i have an application on how to create Eureka server and how service discovery really happens.

Part 1: Eureka Server application
We will create application using Springboot because Eureka is part of Spring Cloud Netflix. Spring Cloud Netflix is a bundle where Spring features(autoconfiguration,etcc) are integrated with the Netflix OSS components.

To write simple Eureka server we use Spring Initializr web console to add dependencies and generate project skelton.
Dependency : Search for Eureka and create the project.<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>

When you import the project directories in to the Intellij , we need to annotate the Springboot main class with annotation “@EnableEurekaServer” .

After this we need to add the below properties in /resources/application.properties folder.

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

When you do the mvn install and mvn spring-boot:run the application will be up and running, Now just do the localhost:8761 from browser. We will see the Eureka server is up and running but no instances will be registered with the registry as given below.

As we see above there are no instances registered with the Eureka server.

Full example application code can be found in the below github link.
https://github.com/shivakumarksk/eurekaserver

Part 2: Eureka client application
Here we will create client application which will be registered with the above eureka registry server. Same as above we will use Spring Initializr to generate the project skelton.

Dependency : Search for Eureka Discovery Client and create project.
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

Annotate the spring boot main class with “@EnableDiscoveryClient”

Add bootstrap.properties with application name as your wish.

Add eureka registry server endpoint in the application.properties file.

Now run the eureka server application first mvn spring-boot:run. The server will get started on 8761 as given in properties file.

Then start client application ,start from different terminal instead of using Intellij terminal. mvn spring-boot:run

As we observe above client is registered with registry.We can see that from Eureka web console as well.

Here highlighted part is the our client app. When you bring down client it will show alert kind of message that client de-registered with the registry.

Client application example code can be found in the below github link.
https://github.com/shivakumarksk/eurekaclient

This is all about Service registry with Netflix Eureka. :)

--

--

No responses yet