From 9849d9b23db6c0ee4e0b0b43ed7520a68c029fdb 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 c5e6ee21..d0a8ecdc 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 @@ -224,6 +224,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.