Service Discovery
Implement service discovery with Spring Cloud Eureka — enabling microservices to find each other dynamically without hardcoded URLs.
Last reviewed: January 2025
🔍 What Is It?
Service discovery is the mechanism by which microservices locate each other.
Components:
- →Eureka Server — the service registry (a Spring Boot app with @EnableEurekaServer)
- →Eureka Client — each microservice registers itself (spring-cloud-starter-netflix-eureka-client)
- →Load Balancer — Spring Cloud LoadBalancer distributes calls across instances
Registration process:
- Service starts, registers with Eureka (name, IP, port, health URL)
- Sends heartbeats every 30 seconds to stay registered
- If heartbeats stop, Eureka removes the instance after 90 seconds
- Clients cache the registry locally — don't query Eureka on every call
🎯 Why Do We Need It?
In Kubernetes or cloud environments, service IP addresses change when pods restart. Hardcoding IPs is impossible. Service discovery provides stable logical names (order-service) that resolve to current instances dynamically. It also enables load balancing across multiple instances.
🏗️ What Happens in Real Projects?
In Kubernetes, you often use Kubernetes Service DNS instead of Eureka (Kubernetes has built-in service discovery). Eureka is more relevant in non-Kubernetes environments or when you need client-side load balancing with more control.
⚙️ How It Works
Services use lb://service-name URIs. Spring Cloud LoadBalancer intercepts these, queries the service registry for available instances, and selects one (round-robin by default). The actual HTTP call goes to the selected instance's IP and port.
💻 Example
Eureka Server and Client Setup — Setting up service registry and client registration.
🤔 Common Confusions
❓ Eureka vs Kubernetes Service Discovery — which to use?
If running on Kubernetes, use Kubernetes Service DNS (service-name.namespace.svc.cluster.local). Kubernetes has built-in service discovery and load balancing. Eureka adds complexity without benefit in a Kubernetes environment. Use Eureka for non-Kubernetes deployments or when you need client-side load balancing features.
🚨 Common Production Issues
Eureka server single point of failure — deploy in cluster mode with multiple replicas
Stale registry entries after service crashes — tune lease expiry settings
Services not deregistering on shutdown — ensure graceful shutdown is configured
✅ Best Practices
Run Eureka in cluster mode (minimum 2 instances) for high availability
Set spring.application.name consistently — it's your service's identity
Configure health check URL so Eureka knows if the service is actually healthy
On Kubernetes, prefer Kubernetes native service discovery over Eureka
🏛️ Architect's Note
Modern cloud-native deployments on Kubernetes use Kubernetes Service DNS for service discovery natively. Spring Cloud Eureka is more relevant for traditional VM-based or Docker Compose deployments. If you're building for Kubernetes from the start, you can skip Eureka and use Kubernetes Services directly — simpler and more reliable.
💬 Frequently Asked Questions
🎯 Key Takeaways
🔗 Related Topics
🎯 Practice this topic in interviews
↗ Where this topic fits
Spring Boot Technology Guide
Return to the Spring Boot learning hub.
Backend Engineer Career Path
A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
Sources
Sources / References
Use these Spring Boot references to verify the concept in official docs and continue into the most relevant SplashRide pages.
Official documentation
Related SplashRide pages
SplashRide Spring Boot learning path
Browse the full Spring Boot topic collection and learning order on SplashRide.
Spring Boot Interview Prep
Practice Spring Boot interview questions and scenario-based answers.
Backend Engineer Career Path
A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
API Gateway
Implement an API Gateway with Spring Cloud Gateway — the single entry point for all microservices that handles routing, authentication, rate limiting, and cross-cutting concerns.
Config Server
Centralize configuration for all microservices with Spring Cloud Config Server — manage environment-specific properties from a Git repository without redeployment.