June 21, 2024
Implementing Client-Side Service Discovery with Eureka

Client-Side Service Discovery With Eureka

In this series, we would build demonstrate how Eureka works. We would have a number of microservices and the use Eureka as a Service Registry.

  1. Introduction
  2. Our Microservice Architecture
  3. How it Works
  4. Build the Servers
  5. Build the ProductWeb
  6. Build the Eureka Server
  7. Do some Testing, Access Eureka Dashboard


1. Introduction

A Service Registry is used in Microservices as a registry of all the services that is part of the architecture. So in a way, a Service Registry is like a phone book of microservices. So what happens is that each services registers itself with the Service Registry, telling the registry, where it is located (like hostname, port, nodename etc).

A benefit of Service Registry like Eureka is that the microservices can find other services using a logical service name rather than the actual hostname:port.

In Eureka, there is mapping of each client’s logical name to actual physical address of the clients. In this way, when the physical address changes, the services are insulated from this changes.


2. Our Architecture

The architecture for this microservice would be a little modification of the earlier one we did with Ribbon Load Balancer. See the diagram below:

Microservices Design With Eureka Service Registry
Microservices Design With Eureka Service Registry

3. How it Works
We have three microservices: ProductWeb, Product Server1 and ProductServer

These three microservices are Eureka Clients and they must register with the Eureka Server(made up of the Load Balancer¬† and the Service Registry). I left out the Angular UI app for now since it’s not relevant to this discussion.

Note that the Eureka Server is in itself   microservice. So we actually have four microservices!

Let’s now follow the procedure to build the four services in Spring Boot.


4. Build the Servers (ProductServer1&2)

Using IntelliJ, create a new application. Ensure to add the Eureka Discovery Client dependency. Also the MongoDB dependency.

Go ahead to create the following

Product: just a normal POJO

ProductRepository: an interface that extends the MongoRepository. Remember to annotate with @RepositoryResource and provide the collectionResourceRel and path attributes

ProductController: exposes the methods using the repository

InitializationComponent: this file contains the method that populates the initial data.

Then annotate the main application class with the @EnableEurekaClient annotation.

Finally add the port and application name to the application.properties file like so:



5. Build the ProductWeb(ProductWeb)

Build the spring application. Add the Eureka Discovery client dependency as well.

Annotate the main application class with the @EnableEurekaClient annotation.

Since this service is going to be making request using RestTemplate, we need to create a RestTemplate bean that we would use in the controller. To to that, add this method to the main application class.

public RestTemplate getRestTemplate() {
	return new RestTemplate();

The @LoadBalanced annotation add a simple round-robin load balancer to the service.

Create the following files:

Product: same as above. You may have to remove the @id annotation

ProductService: an interface that contains definition of the methods to expose

public interface ProductService {

    public List<Product> getAllProducts();

    public Product getProduct(String productId);

Notice that int he ProductService, we have used object instead of Product. The reason is that the RestTemplate actually returns a list of objects

ProductRestController: implementation of the ProductService. This controller would use the RestTemplate to make calls to the ProductServer.

Here is the content of the ProductRestController file

public class ProductRestController implements ProductService {

    private RestTemplate restTemplate;

    String serverUrl = "http://productserver/products";

    public List<Product> getAllProducts() {
        Product[] products =  restTemplate.getForObject(serverUrl, Product[].class);
        return Arrays.asList(products);

    public Product getProduct(String productId) {
        Product product = restTemplate.getForObject(serverUrl + "/" + productId, Product.class);
        return product;


Finally, update the application.properties file with the name of the service (productweb) as well as the port (8090)


6. Build the Eureka Server(EurekaServer)

This is the microservice that would play the role of the Eureka Server. So you need to add the Eureka Server dependency when creating this app.

Annotate the main application class with the @EurekaServer annotation.

Then go ahead to add the following code to the application.properties file. This effectively tell Eureka not to register itself with itself! Also the default port of 8761 is where the clients would look for Eureka registry by default



7. Do some Testing, Access Eureka Dashboard

Fire up all the services. Check the console window and see how each service registers itself with Eureka.

Then make a request to http://localhost:9090/products

The ProductWeb microservice receives this request, and then uses Eureka to find the server and get a response.

Also play around by building a second server and see how it load balances. Also try to run two instances of the same server but in different ports.

To see the Eureka Dashboard, access http://localhost:8761. the dashboard opens as shown below:

Eureka Dashboard
Eureka Dashboard



0 0 votes
Article Rating
Notify of
1 Comment
Newest Most Voted
Inline Feedbacks
View all comments

[…] As you already know, in a microservices architecture, the microservices would have to register with a Service Registry such as Eureka Server. We implemented this here. […]