Chapter 10.2☕ 16 min read

Service Discovery with Eureka

If <code>payment-service</code> runs on port 8082 today, but moves to port 8085 tomorrow, how does <code>user-service</code> find it? You shouldn't hardcode IPs. <strong>Eureka</strong> is a phonebook for microservices.

01The Concept: Service Registry

The Hyderabad JustDial Analogy:

If you want to find a good Biryani place in Hyderabad, you don't memorize the restaurant's address. You search "Biryani" on JustDial. JustDial tells you the current address. If the restaurant moves, they update JustDial.

In Spring Cloud, Eureka Server is JustDial. Every microservice registers itself with Eureka on startup ("Hi, I am payment-service, I am at IP 192.168.1.5:8082"). When user-service needs to call payment-service, it asks Eureka for the current IP address.

02Technical Explanation
  1. Eureka Server: A standalone Spring Boot app that holds the registry.
  2. @EnableEurekaServer: Annotation that turns a Spring Boot app into the JustDial phonebook.
  3. @EnableEurekaClient: Annotation that tells a microservice to register itself with Eureka.
  4. Spring Cloud LoadBalancer: Replaces the dead Netflix Ribbon. It automatically picks one IP if a service has multiple instances running.
03Full Working Code: The Eureka Server

1. pom.xml (Eureka Server)

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

2. application.yml (Eureka Server)

server:
port: 8761 # Default Eureka port

spring:
application:
name: eureka-server

eureka:
client:
register-with-eureka: false # It doesn't need to register with itself
fetch-registry: false

3. Main Class (EurekaServerApplication.java)

package com.devinhyderabad;

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

@SpringBootApplication
@EnableEurekaServer // 1. Turns this app into the phonebook
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

If you run this and go to http://localhost:8761, you will see the Eureka Dashboard. Any microservice that starts with @EnableEurekaClient will appear in this list!

04Why It Matters

Interview Question: "Why do we need Eureka instead of just hardcoding URLs in application.properties?"

Answer: In cloud environments (AWS/Docker), IPs and ports are dynamic. They change every time a container restarts. Hardcoding IPs would require rebuilding and redeploying the app every time. Eureka provides dynamic discovery, allowing services to find each other instantly.

05Interview Note

Enterprise Note: Netflix Eureka is AP (Available and Partition Tolerant) according to the CAP theorem. It favors availability over consistency. If the network breaks, Eureka will serve old/stale addresses rather than refusing to respond.

Key Takeaways

  • ✅ Eureka Server acts as a phonebook — microservices register themselves and discover others
  • ✅ @EnableEurekaServer turns a Spring Boot app into the Eureka registry server
  • ✅ @EnableEurekaClient makes a microservice register itself with Eureka on startup
  • ✅ Spring Cloud LoadBalancer replaces the deprecated Netflix Ribbon for client-side load balancing