Skip to content

Commit 3ce149a

Browse files
committed
Improve documentation for Load Balancing with Eureka and Spring Cloud LoadBalancer
1 parent 95441ae commit 3ce149a

1 file changed

Lines changed: 126 additions & 7 deletions

File tree

docs/modules/ROOT/pages/spring-cloud-netflix.adoc

Lines changed: 126 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -389,15 +389,134 @@ the ability to refresh Eureka clients. To do this set `eureka.client.refresh.en
389389

390390
=== Using Eureka with Spring Cloud LoadBalancer
391391

392-
We offer support for the Spring Cloud LoadBalancer `ZonePreferenceServiceInstanceListSupplier`.
393-
The `zone` value from the Eureka instance metadata (`eureka.instance.metadataMap.zone`) is used for setting the
394-
value of `spring-cloud-loadbalancer-zone` property that is used to filter service instances by zone.
392+
Spring Cloud Netflix integrates with Spring Cloud LoadBalancer to provide
393+
client-side load balancing for services registered with Eureka.
395394

396-
If that is missing and if the `spring.cloud.loadbalancer.eureka.approximateZoneFromHostname` flag is set to `true`,
397-
it can use the domain name from the server hostname as a proxy for the zone.
395+
When a client makes a request to another service using a logical service ID,
396+
Spring Cloud LoadBalancer selects an appropriate service instance based on
397+
available instances retrieved from Eureka.
398398

399-
If there is no other source of zone data, then a guess is made, based on the client configuration (as opposed to the instance configuration).
400-
We take `eureka.client.availabilityZones`, which is a map from region name to a list of zones, and pull out the first zone for the instance's own region (that is, the `eureka.client.region`, which defaults to "us-east-1", for compatibility with native Netflix).
399+
==== How It Works
400+
401+
The load balancing process follows these steps:
402+
403+
1. A client makes a request using a service ID (for example, `http://user-service`).
404+
2. The `DiscoveryClient` retrieves all available instances of the service from Eureka.
405+
3. Spring Cloud LoadBalancer selects one instance using a load balancing strategy.
406+
4. The request is routed to the selected service instance.
407+
408+
This mechanism enables service discovery and client-side load balancing across multiple instances.
409+
410+
==== Configuration Example
411+
412+
The following example shows a minimal configuration for using Eureka with Spring Cloud LoadBalancer:
413+
414+
.application.yml
415+
[source,yaml]
416+
----
417+
spring:
418+
application:
419+
name: user-service
420+
cloud:
421+
loadbalancer:
422+
ribbon:
423+
enabled: false
424+
425+
eureka:
426+
client:
427+
serviceUrl:
428+
defaultZone: http://localhost:8761/eureka/
429+
----
430+
431+
NOTE: Ribbon is deprecated and replaced by Spring Cloud LoadBalancer.
432+
433+
==== Using Load-Balanced Clients
434+
435+
Spring Cloud LoadBalancer can be used with `RestTemplate` or `WebClient`.
436+
437+
.Example using RestTemplate
438+
[source,java,indent=0]
439+
----
440+
@Bean
441+
@LoadBalanced
442+
public RestTemplate restTemplate() {
443+
return new RestTemplate();
444+
}
445+
----
446+
447+
.Example using WebClient
448+
[source,java,indent=0]
449+
----
450+
@Bean
451+
@LoadBalanced
452+
public WebClient.Builder webClientBuilder() {
453+
return WebClient.builder();
454+
}
455+
----
456+
457+
With these configurations, requests made using a service name are automatically resolved
458+
to an actual service instance registered in Eureka.
459+
460+
==== Zone-Based Load Balancing
461+
462+
Spring Cloud LoadBalancer supports zone-based routing by using metadata provided
463+
by Eureka instances.
464+
465+
To define a zone for a service instance, use the following configuration:
466+
467+
.application.yml
468+
[source,yaml]
469+
----
470+
eureka:
471+
instance:
472+
metadataMap:
473+
zone: zone1
474+
----
475+
476+
When multiple instances exist across zones, the load balancer can prioritize instances
477+
in the same zone as the client.
478+
479+
The `ZonePreferenceServiceInstanceListSupplier` is used to filter instances based on zone.
480+
481+
==== Zone Resolution
482+
483+
The zone used by the load balancer can be determined in multiple ways:
484+
485+
- Explicitly via `eureka.instance.metadataMap.zone`
486+
- From the hostname if the following property is enabled:
487+
488+
[source]
489+
----
490+
spring.cloud.loadbalancer.eureka.approximateZoneFromHostname=true
491+
----
492+
493+
When this property is enabled, the system attempts to infer the zone from the domain name
494+
of the service instance hostname.
495+
496+
If no zone information is available, the system falls back to the client configuration,
497+
such as `eureka.client.availabilityZones`.
498+
499+
==== Troubleshooting
500+
501+
If load balancing does not behave as expected, consider the following:
502+
503+
- Ensure that all service instances are properly registered in Eureka
504+
- Verify that `metadataMap.zone` is correctly configured
505+
- Check that the Eureka client is able to fetch registry data
506+
- Enable debug logs to inspect load balancer decisions
507+
508+
==== Best Practices
509+
510+
- Always define zone metadata for service instances in multi-zone deployments
511+
- Use consistent naming conventions for zones
512+
- Avoid relying solely on hostname-based zone approximation in production
513+
- Monitor service instance health and registration status
514+
515+
==== Summary
516+
517+
Spring Cloud LoadBalancer, when combined with Eureka, provides a flexible and dynamic
518+
approach to client-side load balancing. It enables services to discover and communicate
519+
with each other efficiently while distributing traffic across available instances.
401520

402521
=== AOT and Native Image Support
403522

0 commit comments

Comments
 (0)