Service Discovery using Spring Eureka

In the last blog we talked about what microservices is. In this blog we will learn about how services are created and communicate (Service Discovery) using Eureka.


How do services communicate?

After creating a few services it’s important that they communicate. You obviously cannot hardcode the URL. With cloud based deployment, the service instances have dynamic network locations. 

As the URL would be hardcoded it will always point to that service instance, scaling would also not make sense. So we need a way for microservices to discover each other. This is where service discovery comes into the picture.

Consider service discovery as a central server which has information about the location of services. Each microservice registers itself with the Service Discovery Server (SDS). The SDS keeps checking the services for their heart beat so if any service goes down, it is automatically de-registered from SDS.

There are mainly two types of service discovery :

  1. Client side service discovery:

    Here the client queries the service registry (SDS) and gets the location of the service. Then calls the API with the location.

  2. Service side service discovery:

    Here the client calls the service via load-balancer, the load balancer then queries the service registry and routes the request to the appropriate service.


Creating a Service Discovery

In this blog we will focus on the first type – Client Side 

We will be creating services using Spring Boot and will use Spring Eureka Server for service discovery.

Since this tutorial is more focused on microservices we would not be focusing much on functionality of the application, we will keep it very simplistic. 

We will create 2 simple services and 1 discovery service using Spring Initializr :

  1. Person-Info Service:

    We will expose one API in this service that will return personal-info against a name. Note: For the sake of an example, person name will be unique.

  2. Employee-info Service:

    We will expose one API in this service which will get employee information against an employee ID and internally query personal-info service to get personal info of that employee.

  3. Service-Discovery:

    This service is for service discovery we won’t be writing any code in this service.


In this example, first we will register (deploying these services would register them to eureka-service) person-info service and employee-info service in discovery service ( shown by a dashed line ), then we will call employee-info service API to get Employee information, which will internally call Person-info service to get personal information of employee using discovery service.

NOTE : We need to create eureka-server (deploy discovery-service) before deploying other services.


Step 1 : Create Service-Discovery Service 

Create a Spring Initializr project with Eureka server dependency as shown in the image below.



Add the following config to





Step 2: Create person-info service

Create a Spring Initializr project with Eureka Discovery Client dependency together with web.

The sample code for this tutorial will be available on github.



Add below properties in


eureka.client.serviceUrl.defaultZone  = http://localhost:3030/eureka

eureka.client.instance.preferIpAddress = true = person-service


Step 3 : Similarly create employee-info service



Here, we are calling person-info service using RestTemplate and if you look carefully the URL for person-info service is not like http://localhost:3032/person instead it is like http://person-service/person.

“Person-service” is the name we specified in “”


Similarly, add following properties to


eureka.client.serviceUrl.defaultZone  = http://localhost:3030/eureka

eureka.client.instance.preferIpAddress = true = employee-service


Now let’s start the Eureka Server.

Check Eureka Server at https://localhost:3030. If it’s working, you will see a page as shown in the image below.


We can see there are no services registered as of now. Let’s start the other two services and reload the page.

Now, you will see two services registered as shown in the image below.


Now, lets call employee-service at http://localhost:3031/employee/1.

You will see the response as shown below.


  "name": "Andy",

  "department": "Parks and Recreations",

  "personalData": {

  "dob": "03/09/1994",

  "age": 25,

  "address": "Minnesota"




In this blog we learnt how to register services on Eureka Server and how to communicate with other services using Eureka because of which you don’t have to hard code the URL.

Hope this and the previous blog helped you understand microservices architecture and create application using microservices architecture.

This project is available at


Banner Image : Link