May 5, 2023

How to Configure Spring Boot with Eureka

Configuring Spring Boot with Netflix Eureka service discovery

Service discovery is one of the first problems you'll run into once an application grows past a single monolith. Netflix Eureka is a popular solution for this in the Spring ecosystem, and getting it running with Spring Boot takes just six steps.

Step 1: Add the Eureka Server Dependency

Start by adding the Netflix Eureka starter to your Eureka server project's pom.xml.

Step 2: Configure the Server Properties

In the Eureka server's application.properties, set the application name, the port, and disable the client's self-registration and self-fetching — since this instance is the registry, it doesn't need to register with or fetch from itself:

spring.application.name=eureka-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

Step 3: Enable the Eureka Server

Annotate the main application class with @EnableEurekaServer alongside the usual@SpringBootApplication.

Step 4: Create a Client Project and Add the Eureka Client Dependency

For each service that should register itself with Eureka, create a new client project and add thespring-cloud-starter-netflix-eureka-client dependency to its pom.xml.

Step 5: Configure the Client Properties

In the client's application.properties, specify the service name, a port, and the Eureka server's URL:

spring.application.name=my-service
server.port=8080
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

Step 6: Register with Eureka

Finally, annotate the client's main application class with @EnableDiscoveryClient to complete registration.

The Result

Once everything is wired up, the Eureka dashboard becomes available at the server's base URL, showing every service currently registered within your microservices architecture.