Hackforge Academy

Category: spring_boot

Service Discovery in micro services

Published on 22 Aug 2026

Explanation

Service Discovery allows microservices to find the location of other services dynamically instead of hardcoding IP addresses and ports. In the Spring Cloud ecosystem, Eureka is commonly used as a service registry.

Code:

Without Service Discovery

Order Service
     │
     └──→ http://localhost:8082/payment

Problem:
What happens if the Payment Service
moves to another server or port?

Explanation

Eureka Server acts as a service registry. Microservices register themselves with Eureka when they start. Eureka keeps track of the available service instances.

Code:

                 Eureka Server
                Service Registry
                     │
        ┌────────────┼────────────┐
        ▼            ▼            ▼
   USER-SERVICE  ORDER-SERVICE  PAYMENT-SERVICE
     :8081          :8082          :8083

Explanation

A Spring Boot microservice becomes an Eureka client by registering with the Eureka Server. The service provides its application name and connection details through configuration.

Code:

spring:
  application:
    name: ORDER-SERVICE

  cloud:
    discovery:
      enabled: true

  eureka:
    client:
      service-url:
        defaultZone: http://localhost:8761/eureka/

Explanation

When a service starts, it registers with Eureka. Eureka stores information such as the service name, host, port, and availability status. Other services can then discover it using its registered service name.

Code:

ORDER-SERVICE
      │
      │ Register
      ▼
EUREKA SERVER
      │
      │ Registry
      │
      ├── USER-SERVICE :8081
      ├── ORDER-SERVICE :8082
      └── PAYMENT-SERVICE :8083

Explanation

Eureka supports multiple instances of the same service. This allows services to scale horizontally. A client can discover multiple instances and use load balancing to distribute requests.

Code:

             Eureka
                │
       PAYMENT-SERVICE
        /      |      \
       ↓       ↓       ↓
    :8083    :8084    :8085

Multiple instances
of the same service

Explanation

The main benefit of Eureka is that services do not need to know the physical location of other services. They communicate using logical service names, making the architecture more flexible and easier to scale.

Code:

Order Service
      │
      │ PAYMENT-SERVICE
      ▼
   Eureka
      │
      ▼
Payment Service Instance

No hardcoded IP address required.

🚀 Learn Spring Boot with real-world projects

💡 Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
⚛️

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community