Service Discovery with Spring Cloud Eureka

Introduction

As microservices grow in number, keeping track of service locations becomes a real challenge. Hardcoding URLs is fragile, especially when services scale, restart, or move to new instances. That’s why service discovery is essential in distributed systems. With Spring Cloud Eureka, services can register themselves automatically and discover each other without manual configuration. In this post, you’ll learn how Eureka works, how to set it up, and how to integrate it into your Spring microservices.

What Is Service Discovery?

Service discovery is the ability for services to find each other automatically. Instead of relying on fixed IP addresses or environment variables, services query a registry to locate the correct instance.

Why It Matters

  • Services scale up or down dynamically
  • IP addresses change often
  • Load balancing needs current instance information
  • New versions roll out continuously

Therefore, a discovery system ensures the ecosystem remains stable even when everything is changing.

What Is Spring Cloud Eureka?

Spring Cloud Eureka is a Netflix OSS service registry that allows services to register themselves and discover other services. It follows a client-server model:

  • Eureka Server: Central registry storing service instances
  • Eureka Client: Application connecting to the server to register and fetch lookup data

Because Eureka integrates directly with Spring Cloud, it provides seamless service registration and load balancing.

Setting Up a Eureka Server

Start by creating a Spring Boot project with the Eureka Server dependency.

Add Dependency

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

Enable the Server

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApp { }

Configure the Application

server:
  port: 8761

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false

Visit:

http://localhost:8761

You’ll see the Eureka dashboard where services will appear as they register.

Registering a Service as a Eureka Client

Now set up a microservice to register itself automatically.

Add Dependency

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

Enable the Client

@SpringBootApplication
@EnableEurekaClient
public class UserServiceApp { }

Configure the Client

spring:
  application:
    name: user-service

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

After the service starts, it will appear on the Eureka dashboard.

Discovering Other Services

Consumers can use Eureka to find other services dynamically.

Using Spring’s RestTemplate with Load Balancing

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
    return new RestTemplate();
}

Then call another service simply by name:

String response = restTemplate.getForObject(
        "http://order-service/api/orders",
        String.class
);

Eureka automatically resolves the instance for you.

Using WebClient with Eureka

If you’re using WebFlux:

@Bean
@LoadBalanced
public WebClient.Builder webClientBuilder() {
    return WebClient.builder();
}

Now call another service reactively:

Mono<String> data = webClientBuilder.build()
    .get()
    .uri("http://inventory-service/items")
    .retrieve()
    .bodyToMono(String.class);

Eureka + Spring Cloud Gateway

Combine Eureka with Spring Cloud Gateway to route traffic automatically by service name:

spring:
  cloud:
    gateway:
      routes:
        - id: user-service
          uri: lb://user-service
          predicates:
            - Path=/users/**

As a result, your gateway stays dynamic and resilient even when services change.

Health Checks and Heartbeats

Eureka uses heartbeat signals to monitor service health.

  • If a service fails to send heartbeats, it is removed
  • This keeps routing accurate and fresh
  • Avoids sending traffic to unhealthy instances

For better reliability, combine Eureka with Spring Boot Actuator health endpoints.

When to Use Eureka

Use Eureka if:

  • You’re building Spring-based microservices
  • You want tight integration with Spring Cloud Gateway
  • Your services scale dynamically
  • You need simple, centralized service discovery

However, if you’re running Kubernetes, built-in DNS-based discovery often replaces Eureka.

Final Thoughts

Service discovery is fundamental for modern microservices, and Spring Cloud Eureka offers a simple, reliable way to register and locate services automatically. It eliminates hardcoded URLs, improves resilience, and integrates well with the Spring Cloud ecosystem. Start with a basic Eureka server, register your services, and use load-balanced RestTemplate or WebClient to call them. To continue improving your Spring Cloud architecture, read API Routing with Spring Cloud Gateway. For official references, explore the Spring Cloud Netflix documentation.

Leave a Comment

Scroll to Top