You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/modules/ROOT/pages/spring-cloud-netflix.adoc
+126-7Lines changed: 126 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -389,15 +389,134 @@ the ability to refresh Eureka clients. To do this set `eureka.client.refresh.en
389
389
390
390
=== Using Eureka with Spring Cloud LoadBalancer
391
391
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.
395
394
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.
398
398
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:
0 commit comments