Eureka is a service discovery component originally developed and open-sourced by Netflix. It plays a critical role in microservices-based systems, where applications are decomposed into small, independently deployable services that must dynamically locate and communicate with one another.
Core Mechanisms of Eureka
- Service Registration: Upon startup, each microservice registers itself with the Eureka Server by submitting metadata such as service name, instance ID, host IP, and port number.
- Heartbeat Renewal: Registered instances periodically send heartbeats (default every 30 seconds) to confirm they are still active. If the Eureka Server misses multiple consecutive heartbeats, it marks the instance as unavailable and eventually removes it from the registry.
- Service Lookup: Clients query the Eureka Server to retrieve a list of available instances for a given service. This enables dynamic routing and supports client-side load balancing.
- Deregistration: When a service shuts down gracefully, it sends a deregistration request. Otherwise, the Eureka Server automatically evicts stale entries after heartbeat expiration.
Key Features
- High Availability: Eureka Servers can be deployed in a peer-to-peer cluster, replicating registry data across nodes for fault tolerance.
- Self-Preservation Mode: During network instability or high failure rates, Eureka enters self-preservation mode to retain existing registrations and avoid cascading failures.
- REST-Based Interface: The server exposes RESTful endpoints for registration, discovery, and administrative operations.
Integration with Spring Boot
To integrate Eureka into a Spring Boot application, include the spring-cloud-starter-netflix-eureka-client dependency and configure the connection to the Eureka Server:
spring:
application:
name: order-service
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
This configuration registers the service named order-service with a Eureka Server running locally on port 8761.
Advanced Capabilities
Self-Preservation Behavior: In scenarios where a large number of clients fail to renew their leases (e.g., due to network partition), Eureka stops expiring instances to maintain system stability. This behavior prevents premature removal of healthy services during transient outages.
Clustered Deployment: Multiple Eureka Server instances replicate their registries bidirectionally. A client registering with any node makes its information visible across the entire cluster.
Health Monitoring: Beyond heartbeats, Eureka can optionally perform HTTP-based health checks via a configurable endpoint (e.g., /actuator/health) to validate service liveness.
Custom Metadata: Services may attach arbitrary key-value metadata during registration (e.g., version, region, or feature flags), which consumers can use for intelligent routing decisions.
Configuration Best Practices
Eureka Server Setup: When running a standalone Eureka Server for development, disable self-registration and registry fetching:
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
server:
wait-time-in-ms-when-sync-empty: 0
Production Client Configuration: In production, specify multiple Eureka Server URLs for redundancy:
eureka:
client:
service-url:
defaultZone: http://eureka-node1:8761/eureka/,http://eureka-node2:8761/eureka/
Instance Identification: Customize the instance ID using properties like ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} to ensure uniqueness in clustered environments.
Health Check Endpoint: Explicitly define a health check path if your service exposes one, allowing Eureka to assess actual service health beyond mere heartbeat presence.