How to Set Up a Discovery Server in Java (Spring Boot + Netflix Eureka)

Introduction

In a microservices architecture, each service often runs on a different server or container with dynamic IP addresses. Hardcoding these service addresses becomes unreliable. That’s where a Discovery Server — also known as a Service Registry — comes in.

A Discovery Server allows microservices to register themselves automatically and discover each other dynamically without manual configuration.

In the Java ecosystem, one of the most popular implementations is Netflix Eureka, integrated with Spring Boot through Spring Cloud Netflix.

This step-by-step guide will teach you how to set up a Discovery Server in Java using Spring Boot and Eureka Server.


🧩 What Is a Discovery Server?

A Discovery Server is a central registry that keeps track of all available microservices and their locations.

  • Each microservice registers itself with the Discovery Server.
  • Other services query the registry to find where a service is running.
  • The registry updates automatically as services go up or down.

Think of it as a “directory service” for your microservices — it tells clients where to find others.


⚙️ Prerequisites

Before you begin, make sure you have the following tools installed:

ToolVersion
Java17 or later
Spring Boot3.x or later
Maven3.6+
IDEIntelliJ IDEA / Eclipse / VS Code
InternetFor dependency downloads

🏗️ Step 1: Create a Spring Boot Project

You can use Spring Initializr (https://start.spring.io) to bootstrap your project.

Project Metadata:

  • Group: com.example
  • Artifact: discovery-server
  • Name: DiscoveryServer
  • Packaging: jar
  • Java: 17

Add Dependencies:

  • Spring Boot Actuator
  • Spring Web
  • Spring Cloud Netflix Eureka Server

Then click Generate → extract the project ZIP → open it in your IDE.


🧠 Step 2: Add Dependencies Manually (Optional)

If you didn’t add dependencies via Initializr, include them manually in your pom.xml:

<dependencies>
    <!-- Web dependency -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

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

    <!-- Actuator for monitoring -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2023.0.1</version> <!-- Use latest version -->
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Run this to download dependencies:

mvn clean install

🧩 Step 3: Enable Eureka Server

In your main class DiscoveryServerApplication.java, add the @EnableEurekaServer annotation:

package com.example.discoveryserver;

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

@SpringBootApplication
@EnableEurekaServer
public class DiscoveryServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DiscoveryServerApplication.class, args);
    }
}

This enables the Eureka Server functionality in your Spring Boot app.


⚙️ Step 4: Configure application.yml

Create src/main/resources/application.yml and configure your server:

server:
  port: 8761  # Default Eureka server port

spring:
  application:
    name: discovery-server

eureka:
  client:
    register-with-eureka: false  # Server doesn't register itself
    fetch-registry: false        # Server doesn’t fetch registry from others
  server:
    enable-self-preservation: false  # Disable self-preservation for dev

Explanation:

  • port 8761 → The Eureka dashboard runs here.
  • The discovery server won’t register itself as a client.
  • Self-preservation is turned off for faster refresh in local environments.

▶️ Step 5: Run the Discovery Server

You can run the application using:

From your IDE

Run DiscoveryServerApplication.java directly.

From Terminal

mvn spring-boot:run

Once it starts, visit:

👉 http://localhost:8761

You’ll see the Eureka Dashboard, which looks like this:

Eureka
No instances available

That means your server is up and waiting for microservices to register.


🧱 Step 6: Register a Microservice (Client)

Now, let’s create another service to register with the Discovery Server.

For example, create a User Service:

Add dependency:

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

Add config in application.yml:

server:
  port: 8081

spring:
  application:
    name: user-service

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

Run this service, then go to http://localhost:8761.

✅ You’ll see:

Instances currently registered with Eureka
USER-SERVICE

🧮 Step 7: Simulate Multiple Instances

You can simulate multiple running instances of the same service by launching it on different ports:

java -jar target/user-service.jar --server.port=8082
java -jar target/user-service.jar --server.port=8083

Each instance will automatically register with Eureka under the same service name.


🧠 Step 8: Eureka Dashboard Overview

Eureka provides a web-based dashboard showing:

  • Registered microservices
  • Instance health
  • Status updates
  • Lease renewals

It automatically removes dead instances if they stop sending heartbeats.


🛠️ Troubleshooting Common Issues

IssueSolution
Dashboard not loadingCheck if port 8761 is free
Client not registeringVerify defaultZone URL and firewall settings
Instances not visibleEnsure both server and client are running on the same network
CORS error in browserAdd a CorsConfiguration bean in your Eureka server

💡 Best Practices

  • ✅ Always use unique spring.application.name for each microservice.
  • 🔒 Secure your Eureka Server with Spring Security in production.
  • 🧩 Use Spring Cloud LoadBalancer for client-side load balancing.
  • ☁️ Deploy using Docker or Kubernetes for scalability.

🏁 Conclusion

You’ve successfully created and configured a Discovery Server in Java using Spring Boot and Netflix Eureka.
Your server now acts as a central registry where all microservices can dynamically register and discover each other.

This is a foundational step in building a scalable, fault-tolerant microservices architecture with Spring Cloud.


🔑 Key Takeaways

ConceptDescription
Discovery ServerCentral registry for microservices
Eureka ServerNetflix’s implementation of service discovery
Port 8761Default dashboard port
Spring Cloud NetflixSimplifies microservice integration
Dynamic RegistrationServices register and deregister automatically

✨ Next Steps

  • Add a Spring Cloud API Gateway for routing.
  • Integrate a Config Server for centralized configuration.
  • Use Zipkin or Sleuth for distributed tracing.

By following this guide, you’ve set up a fully functional Discovery Server in Java — a crucial building block for any Spring Boot microservice ecosystem.