DEV Community

Cover image for Part 1 : Eureka Service Registry | Spring cloud
Aniket Mote
Aniket Mote

Posted on

Part 1 : Eureka Service Registry | Spring cloud

This blog mark the beginning of a new series focused entirely on the Microservice design pattern.

In this series we will see how Microservice design pattern are used to overcome the challenges in the distriubuted environment.


🫠 Challenges :-

  1. How to locate service when host changes(IP address) as service get updated or scaled in cloud environment. [Eureka server]

  2. How to identify available service instance to make the request [Load balancer]

  3. How to identify certain information about the microservice [Actuators]

  4. How to identify latency causing service in request life cycle [Distributed Tracking]

  5. How to provide single point of entry for authentication & authorization [API Gateway]

  6. How to make external configuration for common properties [Config server]

  7. How to avoid cascading or repeated failure [Circuit Breaker]

  8. How to perform analysis of log [Centralized logging]

  9. How to secure spring application [Spring security & OAuth 2.0]


πŸ“Œ What This Part Covers:

  • Why service discovery is mandatory in production
  • Why a single Eureka server is not enough
  • How microservices register themselves using logical names
  • How this eliminates hardcoded IPs and ports

What Is a Service Registry?

A Service Registry is a centralized system that keeps track of all running microservice instances in a distributed system.

Instead of services communicating using fixed IP addresses and ports, they:

  • Register themselves with the registry
  • Discover other services dynamically using logical names

πŸ“Œ In Spring Cloud, Eureka acts as the Service Registry.


Why Start with Eureka?

Before gateway, security or scaling:

Services must be able to find each other reliably.

Eureka server is service discovery and registration design implementation.


Why Service Discovery Is Mandatory in Production

In real production systems:

  • Services scale dynamically
  • Instances come and go
  • IPs and ports must never be hardcoded
  • Load balancing must be automatic
  • Failure of one node must not break the system

Hardcoding endpoints like:

http://localhost:8080/orders

❌ breaks scaling

❌ breaks failover

❌ tightly couples services


## High level Flow


πŸš€ Example Flow: Order Service ↔ User Service

We’ll use:

  1. user-service
  2. order-service
  3. Eureka Server

❌ Step 1: Problem (Without Service Discovery)

Hardcoded communication

Order Service calling User Service:

@RestController
public class OrderController {

    @GetMapping("/order")
    public String getOrder() {
        RestTemplate restTemplate = new RestTemplate();

        // ❌ Hardcoded URL
        String user = restTemplate.getForObject(
            "http://localhost:8081/user", String.class
        );

        return "Order created for " + user;
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… Step 2: Introduce Eureka Server

1. Add Dependency

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

2. Enable Eureka Server

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
Enter fullscreen mode Exit fullscreen mode

What does @EnableEurekaServer do?

  • Turns a Spring Boot application into a Service Registry
  • Accepts service registrations from microservices
  • Tracks heartbeats to detect unhealthy instances

3. πŸ”§ Eureka Server (application.properties)

server.port=8761

spring.application.name=eureka-server

# Disable self-registration
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Step 3: Register Services to Eureka

In user-service
Dependency

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode

User-service : application.properties

spring.application.name=USER-SERVICE
server.port=8081

# Register to Eureka
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
Enter fullscreen mode Exit fullscreen mode

In order-service

spring.application.name=ORDER-SERVICE
server.port=8082

# Register to Eureka
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
Enter fullscreen mode Exit fullscreen mode

πŸ” Step 4: Eureka in Action

USER-SERVICE registers itself
ORDER-SERVICE registers itself
Eureka keeps track of all instances


πŸ”₯ Step 5: Service Discovery (No Hardcoding)

Use Service Name Instead of URL

@RestController
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/order")
    public String getOrder() {

        // βœ… Using service name instead of hardcoded URL
        **String user = restTemplate.getForObject(
            "http://USER-SERVICE/user", String.class
        );**

        return "Order created for " + user;
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 6: Add Load Balancing

Config

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
    return new RestTemplate();
}
Enter fullscreen mode Exit fullscreen mode

Why @LoadBalanced ?

@LoadBalanced enables:

  • Service name resolution
  • Instance lookup from Eureka
  • Client-side load balancing

πŸ‘‰ Now if multiple USER-SERVICE instances exist:

Eureka + LoadBalancer distributes traffic automatically


πŸ§ͺ Step 7: Scale It

Run:

USER-SERVICE β†’ port 8081
USER-SERVICE β†’ port 8083 (second instance)

πŸ‘‰ Eureka will show:

USER-SERVICE (2 instances)

πŸ‘‰ Calls from ORDER-SERVICE will:

Automatically load balance 🎯


Why This Architecture Is Production-Grade

  • No hardcoded IPs or ports
  • Automatic scaling
  • High availability
  • Fault tolerance
  • Clean separation of concerns
  • Gateway + Nginx hide internal topology

🧠 Final Flow Summary

1. Client β†’ ORDER-SERVICE
2. ORDER-SERVICE β†’ Eureka (Where is USER-SERVICE?)
3. Eureka β†’ Returns available instances
4. ORDER-SERVICE β†’ Calls USER-SERVICE
5. Response β†’ Client
Enter fullscreen mode Exit fullscreen mode

Summary

  • Eureka provides service discovery
  • @EnableEurekaServer creates the registry
  • @LoadBalanced RestTemplate enables dynamic routing
  • Services communicate using logical names
  • Scaling requires no code changes

Top comments (0)