Service Discovery is one of the key tenets of a microservice-based architecture. Trying to hand-configure each client or some form of convention can be difficult to do and can be brittle. Eureka is the Netflix Service Discovery Server and Client. The server can be configured and deployed to be highly available, with each server replicating state about the registered services to the others.
In this post, I will cover the common uses of the Spring Cloud Netflix Eureka. This post is based on Java 21, Spring Boot 3.5.3, and Spring Cloud 2025.0.0.
plugins { java id("org.springframework.boot") version "3.5.3" // Use the Spring Dependency Management plugin to manage Spring Cloud dependencies. id("io.spring.dependency-management") version "1.1.7" }
spring: application: name:eureka-server freemarker: template-loader-path:classpath:/templates/ prefer-file-system-access:false server: # Sets the listening port for the Eureka Server port:8761 eureka: instance: hostname:localhost client: # Tells the Config Server to not register with the Eureka service registerWithEureka:false # Tells the Config Server to not cache registry information locally fetchRegistry:false # the service URL serviceUrl: defaultZone:http://${eureka.instance.hostname}:${server.port}/eureka/ server: # Sets the initial time to wait before the server takes requests waitTimeInMsWhenSyncEmpty:5
plugins { java id("org.springframework.boot") version "3.5.3" // Use the Spring Dependency Management plugin to manage Spring Cloud dependencies. id("io.spring.dependency-management") version "1.1.7" }
spring: application: name:eureka-client eureka: instance: preferIpAddress:true instanceId:${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}} client: registerWithEureka:true fetchRegistry:true serviceUrl: defaultZone:http://localhost:8761/eureka/ healthcheck: enabled:true server: # configure the service use a random port number port:0 management: endpoints: web: exposure: include:health,info,beans
Eureka can be made even more resilient and available by running multiple instances and asking them to register with each other.
Eureka Server
1. Set basic Eureka server configurations in application.yml
spring: application: name:eureka-server freemarker: template-loader-path:classpath:/templates/ prefer-file-system-access:false eureka: client: # Tells the Config Server to not register with the Eureka service registerWithEureka:false # Tells the Config Server to not cache registry information locally fetchRegistry:false server: # Sets the initial time to wait before the server takes requests waitTimeInMsWhenSyncEmpty:5
The complete Eureka client configuration file application.yml:
spring: application: name:eureka-client eureka: instance: preferIpAddress:true instanceId:${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}} client: registerWithEureka:true fetchRegistry:true serviceUrl: defaultZone:http://peer1:8761/eureka/,http://peer2:8762/eureka/,http://peer3:8763/eureka/ healthcheck: enabled:true server: # configure the service to use a random port number port:0 management: endpoints: web: exposure: include:health,info,beans
References
[1] Spring Microservices in Action, Second Edition