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)