How ServiceInstanceListSuppliers
Connects to Eureka
🔗 Overview
In a Spring Cloud microservices architecture using Eureka and Spring Cloud LoadBalancer, service discovery and load balancing involve the following flow:
- The Eureka Client registers services with the Eureka Server.
- The client keeps a local cache of the registry information from the server.
- Requests made via
RestTemplate
,WebClient
, or Feign (annotated with@LoadBalanced
) trigger Spring Cloud LoadBalancer. - Spring Cloud LoadBalancer calls a
ServiceInstanceListSupplier
to get the list of available service instances.
🧩 Role of ServiceInstanceListSuppliers
ServiceInstanceListSuppliers
provides the list of service instances to Spring Cloud LoadBalancer. When Eureka is used, the implementation is:
org.springframework.cloud.netflix.eureka.EurekaServiceInstanceListSupplier
This class connects LoadBalancer to the Eureka registry using the DiscoveryClient.
🔄 Flow of Interaction
- Request is made to a service.
- LoadBalancer intercepts and asks for service instances.
ServiceInstanceListSupplier.get()
is invoked.EurekaServiceInstanceListSupplier
uses Eureka’sDiscoveryClient
to fetch instances.- Instances are returned from the local cache.
📦 Code Example
@Override
public Flux<List<ServiceInstance>> get() {
return Flux.defer(() -> {
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
return Flux.just(instances);
});
}
⚙️ Auto-Configuration
- If
spring-cloud-starter-netflix-eureka-client
is on the classpath, - And
@EnableDiscoveryClient
is used, - Then
EurekaServiceInstanceListSupplier
is auto-configured as the default implementation.
✅ Summary
Component | Role |
---|---|
Eureka Server | Holds the registry of available services |
Eureka Client (DiscoveryClient) | Syncs with server and caches the registry |
ServiceInstanceListSupplier | Supplies instances to the LoadBalancer |
EurekaServiceInstanceListSupplier | Bridges LoadBalancer and Eureka using DiscoveryClient |
Note: The LoadBalancer queries Eureka’s local cache, not the server directly, to reduce latency and improve resilience.