microservices discovery using Eureka server

microservices architecture java: Eureka Server

In the previous microservices articles, we learn how to configure and access Config Server this tutorial we learn what is Spring Cloud Eureka server ?
Why  microservices service registry is needed?
microservices communication : Eureka server/client communication?
How to configure Eureka server using Spring Cloud?

What is Eureka Server?

Eureka server is nothing but a service discovery pattern implementation, where every Microservice is registered and client Microservice looks up Eureka server to get a dependent Microservice to get the job done.

Eureka Server is a Netflix OSS  product and Spring cloud offers the declarative way to register and invoke service by Java annotation.

Why Eureka server/service registry is needed in microservices?

To understand why Eureka server needed in microservices architecture, lets start from simple
Understanding How one service calls another service REST endpoint for communication.

Say we need to call Employee payroll service to get payroll information for an Employee. The payroll service  which is deployed on localhost 8080 port and by passing employee primary key we got that information so we just call following

Where localhost/  is hostname/IP address and payroll is payroll service context, 1 is the Employee primary key.

But this is only possible when beforehand you know the hostname/IP addresses then you can configure your URL. So here Hostname or IP address is a constraint or a pain point.

If IP address of a  server/container is fixed then you can use that approach but
what happens when your IP addresses and hostname are unpredictable?

Nowadays on a Cloud platform, it is obvious that all the servers or containers use dynamic IPs for autoscaling. And the interesting thing is that in  Microservice architecture the key principle is your service can autoscaled as per load so Cloud platforms are ideal for Microservices.

What I try to say here is we can not predict the IP addresses of container/server beforehand so putting dependent services IP addresses in the config file is not a solution we need a more sophisticated technique to identify service, and Eureka server stepped in here.

Eureka server/client communication:

Every Microservice register itself in Eureka server when bootstrapped. Generally using the {ServiceId} it register into Eureka server or it can use hostname or any public IP(If those are Fixed). After registering, every 30 seconds it pings Eureka server to notify that the service itself is available. If Eureka server not getting any ping from a service for a quite long time this service is unregistered from Eureka server automatically and Eureka Server notifies the new state of the registry to all other services. I will write this mechanism elaborately in next Article.

Now one question may pop up our mind is then What is Eureka server itself?

Eureka server is nothing but an another Microservice which treats itself as a Eureka client.
What I mean by that is Eureka server has to be highly available as every service communicate it to discover other services. So it is recommended that it should not be a single point of failure.To overcome it we need multiple Eureka server instance runs behind a load balancer. Now when there are multiple Eureka servers so each server needs to have synchronized registry details so that every server knows about the current state of every Microservices register in Eureka server registry.

Eureka server communicates its peer server as a client and clone the updated state of registry so Eureka server acts itself as Client we can perform this by just configuring

eureka.client.serviceUrl.defaultZone property.

Eureka server works in  two modes.

Standalone : In local, we configure stand-alone mode where we have only one Eureka server i.e  localhost and same cloning property from itself.

Clustered : where we have multiple Eureka server each cloning it states from its peer.
Eureka server can have properties file and communicate with config server as other Microservices do.

Diagram of Eureka server Communication:

microservice architecture diagram Eureka server

Here I will show you a standalone setup of Eureka server.

Step 1: Create a project template from https://start.spring.io/ while creating the template choose the following modules

  1. Config Client.
  2. Actuator
  3. Eureka Server


Step 2:  open the downloaded code template and import it as Maven project in your IDE. The pom.xml looks like
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


    <description>Demo project for Spring Boot</description>

        <relativePath/> <!-- lookup parent from repository -->







Step 3: Now open the EmployeeEurekaServerApplication.java and put the annotation  @EnableEurekaServer on top of the class. By doing so this application now acts as Service Registry all other Microservices can register themselves by using this service URL we will discuss it in next article.

package com.example.EmployeeEurekaServer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

public class EmployeeEurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EmployeeEurekaServerApplication.class, args);

Step 4:Now change the application.properties to bootstrap.properties as it may consult with config server if it has any properties defined in config server. In bootstrap.properties file write the following lines


Unique name for Eureka server service
It consult with other Eureka server for sync the service registry as it is standalone mode I give the local server address.
In which port server will be bound.
This determines is this server register itself as client as I said earlier Eureka server is also act as client so that it  can sync the registry.The value false means prevent itself act as a client.
Not register itself in service registry

Now our Eureka server setup is completed. We will now run this project as java application . Now hit the following url

http://localhost:9091 you will see the following dashboard

Post a Comment