Microservice Registration and Discovery with Eureka

In the previous post we learned how to create microservice and how multiple microservices can communicate with each other.

In the last section(Issue Section) of previous post i have mentioned one issue where we have hard coded other microservices urls like http://localhost:8081 or http://localhost:8082 and in real time environment it may vary.

The typical solution of above problem is we need to use Eureka Service Discovery.

What is Eureka

Eureka is a REST based service used to middle tier load balancing and to handle fail-over. It is primarily used in AWS cloud for service discovery.

Boot will provide a starter for eureka which comes under cloud.springframework umbrella.

Let’s see how to achieve it in microservice based architecture.

You can clone repository from below git.
GitHub Repo: https://github.com/yogeshmprajapati/kode12-spring-boot.git

To demonstrate whole scenario i have create 2 different microservices.

  1. EurekaServer
  2. EurekaClient


Git Module: spring-boot-eureka-server
Runs on: Port 8761

The main task of this application is to

  • Start eureka server.
  • Discovery of other clients.
  • Balancing of incoming request.
  • Periodically check heartbeat of registered services.

We will see required code change in detail.


You need to add below tags in your pom.


Class with main method to run an application.

Here, we have applied @EnableEurekaServer annotation on class to enable eureka server, Now eureka engine will able to perform its task.


Property eureka.client.register-with-eureka is set to false because we are preventing current eureka instance to be register with itself, it tries to register with itself if this property is ignored or set to true.

Run an application

That’s It, we can run an application using class having main method.
Hit url http://localhost:8761/ in your favorite browser.


It shows information about current eureka instance and other statistics.
Here Instances currently registered with Eureka section shows No instances available, it means no client registered with server yet.


Git Module: spring-boot-eureka-client
Runs on: Port 8080

The main task of this microservice is

  • Register itself with server.
  • Sample call via eureka server

We will see required code change in detail.


You need to add below tags in your pom.


CLIENT-SERVICE is a unique name to current instance which is used by Eureka server to identify instance.


Controller to test web services.

Here i have created 2 web services

Will returns Test:

This service will actually shows use of balancing and how it resolve url using Eureka server.

The notifiable thing here is we have used client-service as a host name in http://client-service/test url, technically there is no url available with host name client-service but RestTemplate will resolve it using Eureka Server as current instance is registered with server with name client-service.

So internally it resolve client-service with actual host name and then do the rest of work.


Class with main method to run an application.

2 new annotation used here

@EnableEurekaClient: This annotation will declare current instance eureka client and also make eureka client eligible for discovery.

@LoadBalanced: This annotation will intercept all requests made by RestTemplate to resolve actual end points.

Run an application

Let’s run the application.
Once client application is up and running, it register itself with server.
Server shows registered clients in Instances currently registered with Eureka section which is shown in image below.


Here it shows Application: CLIENT-SERVICE which is configured via application.properties.

At last hit the method which actually use this mechanism.
Url: http://localhost:8080/do-test

And here we go!.

Share current post by copy: https://goo.gl/t2G1DG
Happy Learning!

Yogesh P