Spring Cloud @EnableEurekaServer Annotation | Build a Service Registry

 ðŸš€ Introduction: What is @EnableEurekaServer in Spring Cloud?

The @EnableEurekaServer annotation in Spring Cloud is used to create an Eureka Server, which acts as a service registry for microservices. This enables service discovery, allowing microservices to dynamically register themselves and communicate with each other.

Key Features of @EnableEurekaServer:
 ✔ Centralized service registry for microservices.
 ✔ Enables dynamic service registration and discovery.
 ✔ Works with Spring Cloud Netflix Eureka.
 ✔ Provides load balancing and failover support with Eureka clients.

📌 In this guide, you’ll learn:
 ✅ How @EnableEurekaServer works in Spring Cloud.
 ✅ How to set up an Eureka Server.
 ✅ How microservices register and communicate using Eureka.

1️⃣ Setting Up a Eureka Server in Spring Boot

To create an Eureka Server, follow these steps:

1. Create a Spring Boot Application using Spring Initializr

2. Add Dependencies in pom.xml

Add this Eureka Server dependency to the pom.xml file:

<dependencies>
<!-- Spring Boot Starter for Eureka Server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>

This dependency provides the Eureka Server implementation.

3. Enable the Eureka Server using @EnableEurekaServer.

@SpringBootApplication
@EnableEurekaServer // Enables Eureka Server
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

This starts the Eureka Server, allowing microservices to register.

4. Configure application.yml for Eureka Server

📌 Disable self-registration and enable the Eureka dashboard.

server:
port: 8761 # Default port for Eureka Server

eureka:
client:
register-with-eureka: false # Eureka server does not register itself
fetch-registry: false # No need to fetch registry
server:
enable-self-preservation: true # Prevents services from being removed during network issues

Eureka Server runs on port 8761 and does not register itself.

5. Run the Eureka Server

  • Start the Spring Boot application.
  • Open http://localhost:8761 in a browser.
  • You should see the Eureka Server dashboard.

2️⃣ Registering a Microservice with Eureka Server

Once the Eureka Server is running, microservices can register themselves. We have a ProductService and want to register as a client for the Eureka Server. Follow the below steps:

1. Add Eureka Client Dependency in Microservice (pom.xml)

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

This enables the microservice to register with Eureka.

2. Enable Eureka Client in the Microservice

@SpringBootApplication
@EnableDiscoveryClient // Enables Eureka client for service discovery
public class ProductServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ProductServiceApplication.class, args);
}
}

This registers the microservice with the Eureka Server.

3. Configure application.yml for Eureka Client

server:
port: 8081 # Microservice runs on port 8081

spring:
application:
name: product-service # Service name for discovery

eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/ # Eureka Server URL
instance:
prefer-ip-address: true

The microservice registers with Eureka at http://localhost:8761/eureka/.

4. Verify Service Registration

  • Start the Eureka Server and ProductService.
  • Open http://localhost:8761 and check if product-service appears in the registry.

📌 Example Eureka Dashboard with Registered Service:

Application   | Instances  | Status
----------------------------------

product-service | 1 | UP

The product-service microservice is now discoverable by other services.

3️⃣ Discovering and Calling Services Using Eureka

Microservices registered in Eureka can communicate with each other without knowing exact URLs.

1. Add Load Balancer Dependency (pom.xml)

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

This enables client-side load balancing.

2. Use @LoadBalanced to Call Another Service

📌 Example: OrderService Calls ProductService

@RestController
@RequestMapping("/api/orders")
public class OrderController {

private final RestTemplate restTemplate;

public OrderController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

@GetMapping("/{id}")
public String getOrder(@PathVariable Long id) {
// Call product-service using Eureka service name
String product = restTemplate.getForObject("http://product-service/api/products/" + id, String.class);
return "Order ID: " + id + ", Product: " + product;
}

@Bean
@LoadBalanced // Enables Eureka-based service discovery
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

Instead of calling http://localhost:8081/api/products/{id}, Eureka resolves http://product-service/api/products/{id} dynamically.

4️⃣ Configuring Eureka Server for High Availability

To avoid single points of failure, run multiple Eureka servers.

📌 Example: Setting Up a Cluster of Eureka Servers

1. application.yml for Eureka Server 1 (Port 8761)

server:
port: 8761eureka:
client:
service-url:
defaultZone: http://localhost:8762/eureka/

2. application.yml for Eureka Server 2 (Port 8762)

server:
port: 8762

eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/

Each Eureka Server registers with the other, creating a cluster.

🎯 Summary: Best Practices for Using @EnableEurekaServer

Run Eureka Server separately from business microservices.
 ✅ Use multiple Eureka servers for high availability (defaultZone).
 ✅ Ensure microservices have unique spring.application.name values.
 ✅ Use @LoadBalanced to make service calls without hardcoded URLs.
 ✅ Set prefer-ip-address: true for better discovery in cloud environments.

🚀 Following these best practices ensures scalable, reliable service discovery in microservices architectures!

📢 Share this guide with developers to help them master @EnableEurekaServer in Spring Cloud! ðŸš€

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare