Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 68

    Service Discovery / Eureka

    In a microservices deployment, instance addresses change as pods scale.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    In a microservices deployment, instance addresses change as pods scale. Service discovery answers: where is orders-service right now?

    Netflix Eureka is the classic Spring Cloud registry: services register, clients fetch the registry, and load-balanced calls use logical names. It still works with Spring Cloud Netflix in many estates — but honestly, on Kubernetes the platform DNS (orders-service.namespace.svc) often replaces Eureka entirely. Consul and cloud service meshes are other common alternatives.

    Learn Eureka for interviews and legacy Spring Cloud graphs; prefer kube-DNS or mesh discovery for greenfield TechLearningPRO clusters.

    This lesson covers both the Eureka pattern and when to skip it.

    Purpose of this lesson

    Register and discover Orders with Eureka while understanding Kubernetes DNS and Consul as modern alternatives.

    Understanding the topic

    A dynamic phone book for service instances.

    • Registry: Eureka server stores instance heartbeats.
    • Client: each Boot app registers spring.application.name.
    • Resolver: lb://orders-service via Spring Cloud LoadBalancer.
    • Health: failed heartbeats remove bad instances.
    • Modern alt: Kubernetes Service DNS, Consul, or mesh (Istio/Linkerd).

    Step-by-step explanation

    1. Run a Eureka server app with @EnableEurekaServer on port 8761.
    2. Point Orders and Inventory clients at defaultZone and set application names.
    3. Call inventory via lb://inventory-service from Orders.
    4. Contrast the same call using kube DNS without Eureka on a local kind/minikube sketch.

    Informative example

    Eureka server + Orders client registration.

    yaml
    // Discovery server
    @SpringBootApplication
    @EnableEurekaServer
    public class DiscoveryApp {
    public static void main(String[] args) {
    SpringApplication.run(DiscoveryApp.class, args);
    }
    }
    # application.yml (server)
    server.port: 8761
    eureka:
    client:
    register-with-eureka: false
    fetch-registry: false
    # Orders client
    spring:
    application:
    name: orders-service
    eureka:
    client:
    service-url:
    defaultZone: http://discovery:8761/eureka
    # Call by name (LoadBalancer)
    # WebClient/RestClient URI: lb://inventory-service/api/reservations

    Real-world use

    TechLearningPRO's first multi-service lab used Eureka on VMs. After moving to Kubernetes, duplicate discovery (Eureka + kube DNS) caused confusion and stale registries during rolling deploys. They retired Eureka and standardised on Service DNS plus Spring Cloud LoadBalancer for in-cluster calls — fewer moving parts.

    Security implications

    Open discovery endpoints are dangerous.

    • Authenticate or network-restrict Eureka dashboards and APIs.
    • Do not put registry credentials in source — use env placeholders.
    • Prefer mTLS/mesh policies for service identity in modern clusters.

    Best practices

    • Treat Eureka as legacy-friendly knowledge; prefer platform discovery on K8s.
    • Always couple discovery with health checks.
    • Use logical service names everywhere — never raw pod IPs in app config.

    Common mistakes

    • Running Eureka and ignoring Kubernetes DNS — two sources of truth.
    • Clients that do not deregister cleanly, routing to dead instances.
    • Hard-coding host ports in config after introducing discovery.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhat problem does service discovery solve?+

    Answer

    Locating healthy instances of a logical service when IPs and ports change dynamically.
    2IntermediateQuestionIs Eureka required on Kubernetes?+

    Answer

    Usually no — Kubernetes DNS and Services already provide discovery; Eureka is optional/legacy in many cloud-native stacks.
    3IntermediateQuestionWhat is lb:// in Spring Cloud?+

    Answer

    A scheme that triggers client-side load balancing against discovered instances for that service id.

    Hands-on exercise

    Lab. Start Eureka, register Orders and a fake inventory service, and resolve lb://inventory-service. Then write three bullet points on how the same lab would look with only Kubernetes Services.

    Summary

    Services need a way to find each other — Eureka or platform DNS. Next: put a single front door in front of them with an API gateway.

    Ready to mark this lesson complete?Track your journey across the entire course.