AJinkya Ranade (Aj)
7 min readNov 7, 2018

Spring cloud Netflix Part-1 (Service Registry)
The magic behind Micro-Service Architecture

HELLO, FRIEND’S 😃 😃

Today I am going to explore one of the most interesting concepts in Microservices architecture Is the Service Registry with the help of OSS Netflix. The Team of Netflix are designed various components for building strong distributed Microservice architecture ‘Eureka’ is one of them.
Before going to explore Eureka first try to understand What is Spring cloud Netflix❓

Spring cloud Netflix

I have brake Spring cloud Netflix into two parts for simplifying concept.
👉 Spring cloud is built on Spring Boot by providing the bunch of projects/libraries that help us to enhance our Microservice application.
we can also configure this feature for a more customized view.
👉Netflix is sub-project of Spring cloud & It provides us spring base approach to integrates various components of SSO Netflix to achieve Flexibility & Scalability for developing a Distributed System.

List Of Netflix Components

Service Registry

It Is a key part of the service discovery. It’s maintained cat-log of all services which are registered as a Service discovery with Eureka.Netflix eureka depends upon four important terminologies likes Server , Client ,Service & Instance. let’s try to explore each terminology for a better understanding.

  1. Eureka Server : All microservice are registered with Eureka Server.
    Eureka server is also acting as a client & participating in service discovery to communicate with other Eureka server.
  2. Eureka Client : Eureka client is always registered with services then service becomes available for service registry.
    The Client is sending heartbeats to the service registry after every 30 seconds through the rest call to keep their registration up to date as well as maintains registry (information) in the cache to avoid unnecessary called to service registry for finding the physical address of another service.
  3. Service: It is nothing more than Spring-boot application with Rest controller.
  4. Instance: The Instance/s of Microservice ,which are running on the server or Zone.

� Heartbeats

DiscoveryIdentity-Name: DefaultServer
DiscoveryIdentity-Version: 1.0
DiscoveryIdentity-Id: 127.0.1.1
Accept-Encoding: gzip
Transfer-Encoding: chunked
Host: uk-east-zone:8760
Connection: Keep-Alive
User-Agent: Java-EurekaClient-Replication/v1.9.3
{
"replicationList": [
{
"appName": "CUSTOMER-API",
"id": "192.168.**.**:customer-api:8882",
"lastDirtyTimestamp": 1540320621958,
"status": "UP",
"action": "Heartbeat"
}
]
}

Lets move over from a theoretical point and try to implement Server terminology with simple examples .

Example overview

👉 Create a Eureka Server for service Registry.
👉 Create a multiple instance of Eureka Server & register them with each other .
👉 Self-preservation mode in Eureka server

Quick Star

I would recommend to use Spring IO to creating Spring-Boot Application & dependency management. You can also clone eureka server & client from my git repository as well but I would recommend to go step by step for better understanding.

1️⃣ Create Eureka Server ( Service Registry )

  • we can easily create Eureka Server using Spring-boot application. so first thing you have to do is create a simple spring boot application using Spring Initializer or you can also use STS or Eclipse IDE for create an application (project → new →other → spring-boot → spring starter project) but key point here is you need to add eureka-server dependency to the class path ( in pom.xml ) to treat application as Eureka server.
<!-- Eureka dependency -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
  • Now we have to covert our microservice Into Eureka Server by just adding @EnableEurekaServer annotation at a Class level where the main method exists
  • In this step ,we need to add two important properties RegisterWithEureka & FetchRegistry under . yml file which are present inside src/main/resources folder.

⭐️ eureka.client.RegisterWithEureka

RegisterWithEureka is a child node of Client . Eureka server is also act as a Client by default so Server will trying to register itself as a client with other Eureka server but we need to disable this feature because It technically not a client by using eureka.client.RegisterWithEureka property we can restrict them from the registry.

⭐️ eureka.client.FetchRegistry

Eureka Server also fetches the Information of other services but in our case, the server is already holding information of other services/Client. we have to disabled this feature for now.

📚 Point To Remember

  1. I would recommend to use single .yml file for configured multiple profiles.
  2. when I explore microservices with Azure then I will exploring bootstrap .yml In more details .
application.yml

Wow 😃 !! our Eureka Server is ready for lunching. you can see interface of eureka sever on http://localhost:8760 port .

2️⃣ Create Replica of Eureka Server

In distributed System, If we stuck with only Eureka server means there is only one point of contact for service discovery. If there is some problem with the server or it does not exist in the zone the clients are failover & application can’t work as a distributed manner. To avoid this failover, I have simply create another replica of Eureka server & register with each other.

How to create a replica of Eureka Server

  • In our example, we have a .yml file that is configured for only one replica (profile). I have to use same .yml file to configured another replica of Eureka server so all you need to do to make it work is added following properties inside the .yml file.

⭐️ eureka.instance.defaultZone

with the help of defaultZone properties we can multiple register eureka server.
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
{eureka.instance.hostname}
Its determined IP address of Eureka server or locations of Eureka server.
{server.port} It’s determine in which port Eureka Server running.

application.yml

📚Point To Remember

you can also give comma separated list of peers in case of multiple peers are configured
defaultZone:http://192.168.3.106:8760/eureka/,http://192.168.3.107:8761/eureka/
OR
defaultZone: http://uk-west-zone:8760/eureka/,http://uk-east-zone:8761/eureka/

  • Now we have to create a Run Configuration for each profile to run multiple Instances of Eureka server on local machine.
    Click on New configurations ( Run → Run Configuration → Java Application →New Configuration) button. inside the run configuration panel you need to set Name and VM Arguments parameters .

� Example
VM Args
: -Dspring.profiles.active=${spring.profiles}(as per .yml file).
Name : Zone1

😃 Now its simple as run Application but don’t forget to choice run configuration while running application.

DS Replicas sections is showing the no of active replicas are available for current Eureka server.
localhost:8760 have 1 replica & is pointing to zone2 (point to localhost:8761)
localhost:8761 have 1 replica & is point to zone1 (point to localhost:8760)

INTERFACE OF EUREKA SERVER

📚 Point To Remember
If you are ruining multiples replica on server/machine then you need to add below two lines inside your hosts file to run multiple replicas on same machine.
127.0.0.1 uk-east-zone
127.0.0.1 uk-west-zone

Location of hosts file (Linux) : etc/.hosts
Location of hosts file (Windows) : C:\windows\system32\drivers \etc\hosts
Format : {IP_Address of Server} {eureka.client.instance.hostName}

3️⃣ what is self-preservation mode in Eureka server❓

In our example, we have created one Eureka server & one replica. Consider they are hosted on two different zones like UK-east-Zone & UK-west-Zone.
suppose no of the end user is increasing rapidly from East UK. UK-east-zone server starts receiving heavy traffic & gets down some time. All of the operations that are performed on UK-east-Zone server which is aromatically transfer to another replica of Eureka server ( DS Replica) . once server are comes up then Its try to get all Information from the UK-west-zone or other registered zones. base on receiving information Eureka server calculate renewal value internally & match with our configured renewal value once its match then server stop expiring client instances to maintain registry information.Its called as self-preservation mode & mainly use for network portions between Eureka client and server.

�Default value Of Renewal Threshold

eureka.server.renewalPercentThreshold: 0.8
eureka.server.enableSelfRreservation: true

�Calculation
Clients are sending their heartbeat at a fixed rate of 1 every 30s.
I have register two clients with Eureka sever.
last min (client-1,two request in 1m) 2 + (client-2, two request in 1m)+1=5

If Eureka server revived renewal count less than 5 its automatically enter into self-preservation mode.

�Step to Reproduce scenario

👉added renewalPercentThreshold & enableSelfRreservation properites in your property file.
👉 Run Multiple Eureka server.
👉Register two Client With Eureka.
👉 check Renews threshold & Renews (last min) on eureka interface.
👉 manually stop any one client for some time and check Eureka interface.

Self Preservation Mode Error message

Eureka Server

High level diagram of Eureka Server

That’s It for Today but we aren’t done yet !! next I will explore service Discovery using Eureka & Exception Handling.😃 😄
Thank you for Reading please 👏 👏 if you found it helpful.

AJinkya Ranade (Aj)
AJinkya Ranade (Aj)

No responses yet