Spring Cloud: Setting up Eureka Server

Seyed Sahil
3 min readOct 24, 2021

About Eureka

Eureka is a naming service or discovery service offered by Spring Cloud and it enables both service discovery as well as service registration.

In a real-world scenario, there will be different microservices that will be communicating with each other and each of them can have N number of instances running.

If we take a closer look, maintaining or record keeping the details about these microservices will be a hard process. Say in the real world, there is a chance that some microservice instances can fail and scaling and other activities can happen.

Eureka Server helps us with this problem. It stores the details of microservices like its URL, server details, port number, IP address, etc. and simplifies the process of service discovery

Whenever a new microservice is deployed, they will contact the Eureka server and do a self-registration. Other consuming microservices can communicate with the Eureka server to get the details related to the registered microservices and can call the offered APIs.

Communication Diagram

Note: I will be using Spring Tool Suite 4 IDE for development purposes. It's free and you can get it from their website.

Creating Eureka Server

New Project

  1. Create a new Spring Boot Starter project
  2. Select Eureka Server dependency and click on Finish
Dependency Selection

If you open the pom.xml In the file, you will see the following dependencies. Eureka Server dependency is part of Spring Cloud and that is why the cloud dependencies are added to the project.

eureka-server pom.xml

Enabling Server

Open the application main class (the default one created once the project is set up) and add @EnableEurekaServer annotation. This will let spring boot configure your application as Eureka Server.

EurekaServerApplication.java

Configuring the Eureka Server

The default port used by Eureka Server is 8761 and so we have to specify this in application.properties and tell Spring Boot to use this instead of setting the default port 8080.

Along with this, we have to add two more properties to the application.properties and tell Eureka Server to stop doing a self-registration and the registry fetch operation.

Property eureka.client.register-with-eureka is for decision making - It indicates whether or not this instance should register its information with the Eureka server for discovery by others. Since we are creating Eureka Server, we have to set this property false, as the default value for this is always true.

Now the second property eureka.client.fetch-registry is used for registry information fetch from Eureka Server. As we are developing Eureka Server the server itself doesn't need to fetch the registry. The default value is true. So we have to set this to false.

Server configuration

That’s it — we are done creating our Discovery Service and it is time to start the service 🙂

Once the server is up and running, go to the below URL, we will see the Eureka Server home page.

http://localhost:8761/
Eureka Server Home Page

Downloading the Source Code

Are you looking for a finished project?

The source code is available on my GitHub 🙂

Open Spring Tool Suite and Import it as a Maven project.

How to build and register microservices in Eureka Server? we will see it in the next article

Thank You for Reading

Seyed Sahil

Originally published at http://sydlabz.wordpress.com on October 24, 2021.

--

--

Seyed Sahil

Coding Since 2011, Software Engineer, Game Developer, Artist, Photographer. Passionate about Security and Web Technologies. Favourites — C, Java, Javascript.