Implementing Service Registration and Discovery with Spring Cloud Eureka

Before diving into the implementation, it is helpful to understand why microservices emerged and how they compare to traditional architectures. Previously, monolithic applications were the standard. These applications typically package all features into a single WAR file. Even a minor code change requires a full redeployment. Testing becomes cumbersome because modules are tightly coupled; a bug like a memory leak or infinite loop in one module can crash the entire application. Furthermore, scaling a monolith usually means deploying the whole application even if only one specific function (like reading data) needs more resources, leading to waste. Monoliths also lock you into a specific technology stack, making it difficult to introduce new frameworks later.

Microservices shift the focus from a single application to individual business capabilities. Each capability is deployed independently, allowing for better decoupling. If a specific API experiences high traffic, you can scale just that service. If a node fails, others can take over, improving fault tolerance. However, microservices introduce complexity: managing numerous distributed services requires robust service governance, distributed tracing to debug call chains (e.g., Service A calls B, which calls C), and detailed monitoring to know when and what to scale.

To manage these distributed services, we need a "registry" or a marketplace. Imagine a marketplace where vendors (service providers) register their shops. Buyers (service consumers) don't need to know the home address of every vendor; they just go to the marketplace. Eureka acts as this marketplace. It provides a centralized platform where services register themselves, and clients discover them without hardcoding network locations.

1. Setting Up the Eureka Server

Creating a service registry is straightforward with Spring Boot.

1.1 Dependency Configuration

When creating a Spring Boot project, include the Eureka Server dependency. Here is the necessary Maven configuration:

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

1.2 Enable Server Annotation

Annotate the main application class to activate the Eureka Server functionality.

package com.registry.center;

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

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

1.3 Application Properties

By default, the registry tries to register itself as a client. For a standalone setup, we must disable this client-side behavior.

spring.application.name=eureka-registry
server.port=8088

# Disable self-registration
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

# The URL where instances register
eureka.client.service-url.defaultZone=http://localhost:${server.port}/eureka/

Key Properties:

  • eureka.client.register-with-eureka: Set to false to prevent the server from registering itself.
  • eureka.client.fetch-registry: Set to false as the server doesn't need to fetch registry info from itself.
  • eureka.client.service-url.defaultZone: The address for service registration and query.

Once started, navigate to http://localhost:8088/ to view the Eureka dashboard. Initially, no instances will be listed.

2. Building a High-Availability Eureka Cluster

For production, a single registry is a single point of failure. We configure a peer-to-peer cluster. Here, we set up two nodes on ports 8000 and 8001 that point to each other.

application-peer1.properties:

spring.application.name=eureka-cluster
server.port=8000
eureka.client.service-url.defaultZone=http://localhost:8001/eureka/

application-peer2.properties:

spring.application.name=eureka-cluster
server.port=8001
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/

Package the application using Maven (mvn clean package) and run the jar with specific profiles:

java -jar cluster-build-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar cluster-build-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2

Visiting either http://localhost:8000/ or http://localhost:8001/ should show the nodes registered as replicas of one another.

3. Registering an Eureka Client (Service Provider)

Now, let's create a service that registers itself with the cluster.

3.1 Client Dependencies

We need the Eureka Client starter and the Web starter to expose REST endpoints.

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

3.2 Enable Discovery

Use @EnableDiscoveryClient to allow the application to be discovered.

package com.service.provider;

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

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

3.3 Client Configuration

Configure the client to register with both nodes of the Eureka cluster.

spring.application.name=order-processing-service
server.port=9000
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/,http://localhost:8001/eureka/

3.4 Exposing an Endpoint

Create a simple controller to verify the service is running.

package com.service.provider;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    @GetMapping("/greet")
    public String greetUser(@RequestParam String user) {
        return "Greetings " + user + ", welcome to the service!";
    }
}

Upon starting this application, the Eureka dashboard at http://localhost:8000/ will display order-processing-service in the list of registered instances.

Tags: Spring Cloud Eureka Service Discovery microservices java

Posted on Wed, 23 Sep 2026 16:23:16 +0000 by ashbai