From c13c8bbb5bb569708b9ed99b5deffc1e956bcffd Mon Sep 17 00:00:00 2001 From: Benoit Kessler <98057704+BenoitKessler@users.noreply.github.com> Date: Tue, 13 Dec 2022 02:03:18 +1100 Subject: [PATCH] Add flexibility to zone preference in load balancing (#1174) --- .../core/ServiceInstanceListSupplierBuilder.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/core/ServiceInstanceListSupplierBuilder.java b/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/core/ServiceInstanceListSupplierBuilder.java index 20891426..6a19838e 100644 --- a/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/core/ServiceInstanceListSupplierBuilder.java +++ b/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/core/ServiceInstanceListSupplierBuilder.java @@ -198,6 +198,21 @@ public final class ServiceInstanceListSupplierBuilder { return this; } + /** + * Adds a {@link ZonePreferenceServiceInstanceListSupplier} to the + * {@link ServiceInstanceListSupplier} hierarchy. + * @param zoneName desired zone for zone preference + * @return the {@link ServiceInstanceListSupplierBuilder} object + */ + public ServiceInstanceListSupplierBuilder withZonePreference(String zoneName) { + DelegateCreator creator = (context, delegate) -> { + LoadBalancerZoneConfig zoneConfig = new LoadBalancerZoneConfig(zoneName); + return new ZonePreferenceServiceInstanceListSupplier(delegate, zoneConfig); + }; + this.creators.add(creator); + return this; + } + /** * Adds a {@link RequestBasedStickySessionServiceInstanceListSupplier} to the * {@link ServiceInstanceListSupplier} hierarchy.