About Eureka

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

In a real world scenario, there will be different microservices which 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 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 simplify 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 purpose. Its 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 file you will see the following dependencies. Eureka Server dependency is part of Spring Cloud and that is why the the cloud dependencies are added to the project

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
eureka-server pom.xml

Enabling Server

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
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 eureka server for discovery by others. Since we are creating Eureka Server, we have to set this property to 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.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

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 in 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 next article

Thank You for Reading

Seyed Sahil

Advertisement