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/src/main/asciidoc/spring-cloud-commons.adoc
+61-12Lines changed: 61 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -831,19 +831,20 @@ that retrieves available instances from Service Discovery using a <<discovery-cl
831
831
832
832
=== Spring Cloud LoadBalancer integrations
833
833
834
-
In order to make it easy to use Spring Cloud LoadBalancer, we provide `ReactorLoadBalancerExchangeFilterFunction` that can be used
835
-
with `WebClient` and `BlockingLoadBalancerClient` that works with `RestTemplate`. You can see more information and examples of usage
836
-
in the following sections:
834
+
In order to make it easy to use Spring Cloud LoadBalancer, we provide `ReactorLoadBalancerExchangeFilterFunction` that can be used with `WebClient` and `BlockingLoadBalancerClient` that works with `RestTemplate`.
835
+
You can see more information and examples of usage in the following sections:
837
836
838
837
* <<rest-template-loadbalancer-client,Spring RestTemplate as a Load Balancer Client>>
839
838
* <<webclinet-loadbalancer-client, Spring WebClient as a Load Balancer Client>>
840
839
* <<webflux-with-reactive-loadbalancer,Spring WebFlux WebClient with `ReactorLoadBalancerExchangeFilterFunction`>>
841
840
841
+
[[loadbalancer-caching]]
842
842
=== Spring Cloud LoadBalancer Caching
843
843
844
844
Apart from the basic `ServiceInstanceListSupplier` implementation that retrieves instances via `DiscoveryClient` each time it has to choose an instance, we provide two caching implementations.
If you have `com.github.ben-manes.caffeine:caffeine` in the classpath, Caffeine-based implementation will be used.
848
849
See the <<loadbalancer-cache-configuration, LoadBalancerCacheConfiguration>> section for information on how to configure it.
849
850
@@ -871,25 +872,66 @@ The default setup includes `ttl` set to 30 seconds and the default `initialCapac
871
872
You can also altogether disable loadBalancer caching by setting the value of `spring.cloud.loadbalancer.cache.enabled`
872
873
to `false`.
873
874
874
-
WARNING: Although the basic, non-cached, implementation is useful for prototyping and testing, it's much less efficient
875
-
than the cached versions, so we recommend always using the cached version in production.
875
+
WARNING: Although the basic, non-cached, implementation is useful for prototyping and testing, it's much less efficient than the cached versions, so we recommend always using the cached version in production.
876
+
877
+
=== Zone-Based Load-Balancing
878
+
879
+
To enable zone-based load-balancing, we provide the `ZonePreferenceServiceInstanceListSupplier`.
880
+
We use `DiscoveryClient`-specific `zone` configuration (for example, `eureka.instance.metadata-map.zone`) to pick the zone that the client tries to filter available service instances for.
881
+
882
+
NOTE: You can also override `DiscoveryClient`-specific zone setup by setting the value of `spring.cloud.loadbalancer.zone` property.
883
+
884
+
NOTE: To determine the zone of a retrieved `ServiceInstance`, we check the value under the `"zone"` key in its metadata map.
885
+
886
+
The `ZonePreferenceServiceInstanceListSupplier` filters retrieved instances and only returns the ones within the same zone.
887
+
If the zone is `null` or there are no instances within the same zone, it returns all the retrieved instances.
888
+
889
+
In order to use the zone-based load-balancing approach, you will have to instantiate a `ZonePreferenceServiceInstanceListSupplier` bean in a <<custom-loadbalancer-configuration,custom configuration>>.
890
+
891
+
We use delegates to work with `ServiceInstanceListSupplier` beans.
892
+
We suggest passing a `DiscoveryClientServiceInstanceListSupplier` delegate in the constructor of `ZonePreferenceServiceInstanceListSupplier` and, in turn, wrapping the latter with a `CachingServiceInstanceListSupplier` to leverage <<loadbalancer-caching, LoadBalancer caching mechanism>>.
893
+
894
+
You could use this sample configuration to set it up:
895
+
896
+
[[zoned-based-custom-loadbalancer-configuration]]
897
+
[source,java,indent=0]
898
+
----
899
+
public class CustomLoadBalancerConfiguration {
900
+
901
+
@Bean
902
+
public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(
WARNING: If you have both Ribbon and Spring Cloud LoadBalancer int the classpath, in order to maintain
889
-
backward compatibility, Ribbon-based implementations will be used by default. In order
890
-
to switch to using Spring Cloud LoadBalancer under the hood,
891
-
make sure you set the property `spring.cloud.loadbalancer.ribbon.enabled` to `false`.
931
+
WARNING: If you have both Ribbon and Spring Cloud LoadBalancer int the classpath, in order to maintain backward compatibility, Ribbon-based implementations will be used by default.
932
+
In order to switch to using Spring Cloud LoadBalancer under the hood, make sure you set the property `spring.cloud.loadbalancer.ribbon.enabled` to `false`.
892
933
934
+
[[custom-loadbalancer-configuration]]
893
935
=== Passing Your Own Spring Cloud LoadBalancer Configuration
894
936
895
937
You can also use the `@LoadBalancerClient` annotation to pass your own load-balancer client configuration, passing the name of the load-balancer client and the configuration class, as follows:
@@ -898,7 +940,7 @@ You can also use the `@LoadBalancerClient` annotation to pass your own load-bala
@@ -910,6 +952,13 @@ public class MyConfiguration {
910
952
----
911
953
====
912
954
955
+
You can use this feature to instantiate different implementations of `ServiceInstanceListSupplier` or `ReactorLoadBalancer`,
956
+
either written by you, or provided by us as alternatives (for example `ZonePreferenceServiceInstanceListSupplier`) to override the default setup.
957
+
958
+
You can see an example of a custom cofiguration <<zoned-based-custom-loadbalancer-configuration,here>>.
959
+
960
+
NOTE: The annotation `value` arguments (`stores` in the example above) specifies the service id of the service that we should send the requests to with the given custom configuration.
961
+
913
962
You can also pass multiple configurations (for more than one load-balancer client) through the `@LoadBalancerClients` annotation, as the following example shows:
0 commit comments