DEV Community

Shubham Bhati
Shubham Bhati

Posted on

Service Discovery with Eureka and Spring Cloud: A Hands-On Tutorial

Spring Cloud Eureka Tutorial

Published 2026-08-01 by Shubham Bhati — Backend Engineer (Java 17, Spring Boot, Microservices).

Service Discovery with Eureka and Spring Cloud: A Hands-On Tutorial

Microservices architectures rely on services talking to each other, but hardcoding IP addresses or ports is a deployment, scaling and fault tolerance nightmare. When services scale out or crash, how do others find them? Service discovery solves this, letting services locate each other dynamically. This spring cloud eureka tutorial shows how to set up Spring Cloud Eureka for dynamic service registration and lookup. Ditch static configs and build resilient microservices.

1. Building Your Eureka Server

The Eureka Server acts as a central registry for all your microservices. It's the go-to place for services to register and discover each other. Setting one up with Spring Boot is simple: add the spring-cloud-starter-netflix-eureka-server dependency and enable it with an annotation.

For your pom.xml:

<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>
Enter fullscreen mode Exit fullscreen mode

In your main application class:

package com.example.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
Enter fullscreen mode Exit fullscreen mode

Finally, configure your application.yml to prevent the server from registering itself and fetching registry information:

server:
  port: 8761 # Default Eureka server port
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
Enter fullscreen mode Exit fullscreen mode

Production Note: For high availability, run multiple Eureka server instances that peer with each other. This ensures service discovery remains operational, crucial for low p99 latency on lookups.

2. Registering Services with Eureka Client

Client microservices need to register themselves with the Eureka server. This lets other services find product-service by its logical name.

To make a Spring Boot application a Eureka client, add the spring-cloud-starter-netflix-eureka-client dependency:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>
<!-- Dependency management section for Spring Cloud as before -->
Enter fullscreen mode Exit fullscreen mode

Annotate your main application class with @EnableEurekaClient (often optional, but improves clarity).

package com.example.productservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ProductServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProductServiceApplication.class, args);
    }
}
Enter fullscreen mode Exit fullscreen mode

Configure application.yml to point to the Eureka server and define the service's name:

spring:
  application:
    name: product-service
server:
  port: 8081 # Unique port for your service
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
Enter fullscreen mode Exit fullscreen mode

When product-service starts, it registers with the Eureka server. Check http://localhost:8761 to see product-service registered.

Production Note: Be mindful of client health checks. Eureka clients send heartbeats. If a client stops responding, Eureka will eventually de-register it. Ensure services have proper health endpoints and graceful shutdowns to prevent stale registrations. HikariCP connection pools can be impacted if services suddenly drop without de-registering.

3. Consuming Services with Eureka and Load Balancing

How does order-service find and call product-service? Spring Cloud integrates with Eureka for client-side load balancing. Using @LoadBalanced RestTemplate, you call services by their logical name.

Ensure your consuming service (e.g., order-service) has the Eureka client dependency and config.

Next, configure a @LoadBalanced RestTemplate bean:

package com.example.orderservice;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class OrderServiceConfig {

    @Bean
    @LoadBalanced // Essential for client-side load balancing via Eureka
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
Enter fullscreen mode Exit fullscreen mode

Now, in your service logic, inject this RestTemplate and use the service ID (product-service) directly in your URL:

package com.example.orderservice;

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class OrderService {

    @Autowired
    private RestTemplate restTemplate;

    public String getProductDetails(String productId) {
        // Eureka automatically resolves 'product-service' to an available instance
        String productUrl = "http://product-service/products/" + productId;
        return restTemplate.getForObject(productUrl, String.class);
    }
}
Enter fullscreen mode Exit fullscreen mode

Spring Cloud, backed by Eureka, automatically resolves http://product-service to a running instance and handles load balancing across multiple instances.

Production Note: While RestTemplate is shown, WebClient from Spring WebFlux is generally recommended for new projects due to its non-blocking nature, offering better resource utilization. Ensure proper timeouts are configured to prevent cascading failures.

Common Pitfalls

  • Eureka Server Downtime: If your Eureka server isn't highly available, new services can't register and existing clients can't refresh their registry. This halts discovery.
  • Stale Registrations: Clients fail to de-register (e.g., due to crashes), leading to Eureka holding "dead" instances. Clients might call non-existent services, causing errors. Tune Eureka's eviction policies and client heartbeats.
  • Caching Issues (Client-side): While good for performance, aggressive caching or slow refresh rates can delay clients seeing new or removed instances.
  • Network Partitioning (Split Brain): During network splits, Eureka servers can lose connection to each other and clients. Understand Eureka's "self-preservation mode" which prevents mass de-registration during network issues, but can sometimes hide real problems.

Conclusion

You've now built a foundational understanding of service discovery using Spring Cloud Eureka. We set up a Eureka server, registered a client service, and enabled dynamic, load-balanced communication between microservices. This setup is crucial for building scalable, resilient architectures where services can come and go without manual intervention. Adopt this pattern to simplify your deployments, improve fault tolerance, and focus on your application logic rather than hardcoded service addresses.


Shubham Bhati, Backend Engineer (Java 17, Spring Boot, Microservices)


Spring Cloud Eureka Tutorial in production

Further Reading


Written by **Shubham Bhati* — Backend Engineer at AlignBits LLC, specializing in Java 17, Spring Boot, microservices, and AI integration. Connect on LinkedIn, GitHub, or read more at shubh2-0.github.io.*

Top comments (0)