Chapter 4. On service discovery
The chapter introduces service discovery as a core requirement in distributed and cloud-based systems, where applications must locate the physical address of services that may scale up, scale down, or move frequently. It explains that while simple approaches like property files, DNS, or traditional load balancers can work in static environments, they become fragile and expensive in dynamic microservice systems. Service discovery helps hide infrastructure changes from service consumers, supports horizontal scaling, and improves resiliency by removing unhealthy instances from circulation.
It then contrasts traditional centralized load balancing with a cloud-friendly discovery model. In the older approach, a client reaches a load balancer through a fixed DNS name, and the balancer routes traffic to one of a set of manually managed servers. The chapter argues that this model creates a single point of failure, scales poorly, is hard to update quickly, and adds operational complexity. A modern discovery mechanism instead favors high availability, peer-to-peer communication, dynamic load balancing, local caching, and fault tolerance so clients can continue working even if the discovery service is temporarily unavailable.
Finally, the chapter walks through implementing the pattern with Spring Cloud and Netflix Eureka, showing how to set up a Eureka server, register microservices with it, and query the registry. It describes the key configuration needed for services to register by application name and advertise their IP address, especially in containerized environments, and notes the startup delay before services become visible. It also demonstrates client-side lookup and load balancing through DiscoveryClient, a Ribbon-backed RestTemplate, and Feign, showing how clients can call services by logical name rather than hard-coded network locations while benefiting from caching and round-robin routing.
Figure 4.2. As service instances are added/removed, they will update the service discovery agent and become available to process user requests.
Figure 4.3. Client-side load balancing caches the location of the services so that the service client doesn’t have to contact service discovery on every call.
Figure 4.4. By implementing client-side caching and Eureka with the licensing and organization services, you can lessen the load on the Eureka servers and improve client stability if Eureka becomes unavailable.
Summary
- The service discovery pattern is used to abstract away the physical location of services.
- A service discovery engine such as Eureka can seamlessly add and remove service instances from an environment without the service clients being impacted.
- Client-side load balancing can provide an extra level of performance and resiliency by caching the physical location of a service on the client making the service call.
- Eureka is a Netflix project that when used with Spring Cloud, is easy to set up and configure.
- You used three different mechanisms in Spring Cloud, Netflix Eureka, and Netflix Ribbon to invoke a service. These mechanisms included
- Using a Spring Cloud service DiscoveryClient
- Using Spring Cloud and Ribbon-backed RestTemplate
- Using Spring Cloud and Netflix’s Feign client