Skip to main content

Overview

The Service Registry is built with Netflix Eureka and provides service discovery capabilities for the Sistema de Ventas microservices architecture. It enables dynamic service location, load balancing, and failover without hardcoding service URLs.

Key Features

  • Service Registration: Automatic registration of microservice instances
  • Health Monitoring: Periodic heartbeat checks for service availability
  • Dynamic Discovery: Real-time service instance lookup
  • Load Balancing: Client-side load balancing support
  • Self-Preservation: Protection against network partition scenarios

Architecture

Server Configuration

Eureka Server Settings

jea-registry-service.yml
server:
  port: 8090
  
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
Configuration Details:
  • registerWithEureka: false - Server doesn’t register itself
  • fetchRegistry: false - Server doesn’t fetch registry from other instances
  • Port 8090 is the default Eureka server port

Application Properties

application.yml
spring:
  application:
    name: jea-registry-service
  profiles:
    active: development
  config:
    import: optional:configserver:http://root:123456@localhost:7070

Implementation

Main Application Class

JeaRegistryServerApplication.java
package com.example.jearegistryserver;

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

@SpringBootApplication
@EnableEurekaServer
public class JeaRegistryServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(JeaRegistryServerApplication.class, args);
    }
}
The @EnableEurekaServer annotation activates the Eureka Server functionality.

Maven Dependencies

pom.xml
<properties>
    <java.version>17</java.version>
    <spring-cloud.version>2025.0.0-RC1</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <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>

Client Configuration

Registering Services with Eureka

Microservices connect to Eureka using the following configuration pattern:
Example: jea-gateway-service.yml
eureka:
  client:
    serviceUrl:
      defaultZone: ${EUREKA_URI:http://localhost:8090/eureka}
  instance:
    instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
  • defaultZone: URL of the Eureka server
  • EUREKA_URI: Environment variable for deployment flexibility
  • instance-id: Unique identifier using service name and random value

Registered Services

The following services register with Eureka:
Service NamePurposeTypical Port
jea-gateway-serviceAPI Gateway8085
jea-auth-serviceAuthentication8091
jea-catalogo-serviceProduct Catalog8081
jea-cliente-serviceCustomer Management8082
jea-venta-serviceSales8083
jea-pedido-serviceOrders8084
jea-compra-servicePurchases-
jea-proveedor-serviceSuppliers-
jea-inventario-serviceInventory-
jea-pagos-servicePayments-

Service Discovery Flow

Eureka Dashboard

Eureka provides a web-based dashboard for monitoring registered services.

Accessing the Dashboard

http://localhost:8090

Dashboard Information

The dashboard displays:
  • System Status: Environment, data center availability
  • DS Replicas: Registered replica nodes
  • Instances Currently Registered: All active service instances
  • General Info: Memory usage, lease configuration
  • Instance Info: Service name, IP, status, health check URL
Eureka Dashboard showing registered services

Load Balancing

Client-Side Load Balancing

Services use the lb:// protocol for load-balanced calls:
Example: Gateway Route
- id: jea-catalogo-service
  uri: lb://jea-catalogo-service
  predicates:
    - Path=/categoria/**, /producto/**
The lb:// prefix triggers:
  1. Query Eureka for all jea-catalogo-service instances
  2. Apply load balancing algorithm (default: Round Robin)
  3. Route request to selected instance

Load-Balanced WebClient

WebClientConfig.java
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;

@Configuration
public class WebClientConfig {
    @Bean
    @LoadBalanced
    public WebClient.Builder builder() {
        return WebClient.builder();
    }
}
The @LoadBalanced annotation integrates Eureka service discovery with WebClient.

Health Monitoring

Heartbeat Mechanism

  • Services send heartbeat every 30 seconds (default)
  • Eureka expects heartbeat within 90 seconds (default lease duration)
  • After 90 seconds without heartbeat, instance is evicted

Self-Preservation Mode

Eureka enters self-preservation mode when more than 15% of services fail to renew leases. In this mode, Eureka won’t evict instances even if heartbeats stop.
This protects against false positives during network partitions.

Running the Service Registry

Prerequisites

  1. Java 17+ installed
  2. Maven 3.6+ installed
  3. Config Server running (optional, for centralized configuration)

Startup Commands

# Using Maven
mvn spring-boot:run

# Using JAR
java -jar target/jea-registry-server-0.0.1-SNAPSHOT.jar

Verification

# Check Eureka health
curl http://localhost:8090/actuator/health

# View registered instances (XML)
curl http://localhost:8090/eureka/apps

# View specific service instances
curl http://localhost:8090/eureka/apps/JEA-GATEWAY-SERVICE

High Availability Setup

For production environments, deploy multiple Eureka instances:
eureka-peer1.yml
server:
  port: 8090
  
eureka:
  instance:
    hostname: eureka-peer1
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://eureka-peer2:8091/eureka/
eureka-peer2.yml
server:
  port: 8091
  
eureka:
  instance:
    hostname: eureka-peer2
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://eureka-peer1:8090/eureka/

Security Considerations

Enable basic authentication for Eureka:
spring:
  security:
    user:
      name: admin
      password: ${EUREKA_PASSWORD}
Clients must provide credentials:
eureka:
  client:
    serviceUrl:
      defaultZone: http://admin:${EUREKA_PASSWORD}@localhost:8090/eureka
  • Run Eureka in a private network
  • Use HTTPS for communication
  • Implement firewall rules
  • Restrict access to dashboard

Monitoring and Metrics

Actuator Endpoints

Enable Spring Boot Actuator for monitoring:
management:
  endpoints:
    web:
      exposure:
        include: health,info,metrics,eureka
Available endpoints:
  • /actuator/health - Health status
  • /actuator/metrics - Application metrics
  • /actuator/eureka - Eureka-specific metrics

Key Metrics to Monitor

  • Number of registered instances
  • Heartbeat renewal rate
  • Registry size
  • Self-preservation mode status
  • Instance registration/eviction events

Troubleshooting

Service Not Registering

Causes:
  • Incorrect Eureka URL
  • Network connectivity issues
  • Service not starting properly
Solution: Check service logs and verify eureka.client.serviceUrl.defaultZone

Instance Evicted

Causes:
  • Heartbeat not received
  • Service crashed
  • Network latency
Solution: Increase lease duration or fix service stability

Self-Preservation Mode

Causes:
  • Network partition
  • Many services restarting
Solution: Wait for services to stabilize or disable in dev:
eureka.server.enable-self-preservation: false

Slow Discovery

Causes:
  • Cache refresh intervals
  • Network latency
Solution: Reduce cache refresh interval:
eureka.client.registryFetchIntervalSeconds: 5

Best Practices

1

Use Unique Instance IDs

Configure dynamic instance IDs to support multiple instances:
eureka:
  instance:
    instance-id: ${spring.application.name}:${random.value}
2

Configure Health Checks

Implement proper health check endpoints:
eureka:
  instance:
    health-check-url: http://localhost:8081/actuator/health
3

Set Reasonable Timeouts

Balance responsiveness with stability:
eureka:
  instance:
    lease-renewal-interval-in-seconds: 30
    lease-expiration-duration-in-seconds: 90
4

Enable Metadata

Add custom metadata for advanced routing:
eureka:
  instance:
    metadata-map:
      zone: us-east-1a
      version: 1.0

Build docs developers (and LLMs) love