feat: support autoconfigure WeightedServiceInstanceListSupplier (#1163)
This commit is contained in:
@@ -935,7 +935,7 @@ By default, we try to read and parse the weight from the metadata map (the key i
|
||||
|
||||
If the weight is not specified in the metadata map, we default the weight of this instance to be 1.
|
||||
|
||||
You can use this sample configuration to set it up:
|
||||
You can configure it either by setting the value of `spring.cloud.loadbalancer.configurations` to `weighted` or by providing your own `ServiceInstanceListSupplier` bean, for example:
|
||||
|
||||
[[weighted-custom-loadbalancer-configuration]]
|
||||
[source,java,indent=0]
|
||||
|
||||
@@ -57,6 +57,7 @@ import org.springframework.web.reactive.function.client.WebClient;
|
||||
* @author Tim Ysewyn
|
||||
* @author BaoLin Zhu
|
||||
* @author changjin wei(魏昌进)
|
||||
* @author Zhuozhi Ji
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnDiscoveryEnabled
|
||||
@@ -133,6 +134,14 @@ public class LoadBalancerClientConfiguration {
|
||||
.build(context);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(ReactiveDiscoveryClient.class)
|
||||
@ConditionalOnMissingBean
|
||||
@Conditional(WeightedConfigurationCondition.class)
|
||||
public ServiceInstanceListSupplier weightedServiceInstanceListSupplier(ConfigurableApplicationContext context) {
|
||||
return ServiceInstanceListSupplier.builder().withDiscoveryClient().withWeighted().build(context);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@@ -189,6 +198,14 @@ public class LoadBalancerClientConfiguration {
|
||||
.build(context);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(DiscoveryClient.class)
|
||||
@ConditionalOnMissingBean
|
||||
@Conditional(WeightedConfigurationCondition.class)
|
||||
public ServiceInstanceListSupplier weightedServiceInstanceListSupplier(ConfigurableApplicationContext context) {
|
||||
return ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient().withWeighted().build(context);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@@ -324,4 +341,14 @@ public class LoadBalancerClientConfiguration {
|
||||
|
||||
}
|
||||
|
||||
static class WeightedConfigurationCondition implements Condition {
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
return LoadBalancerEnvironmentPropertyUtils.equalToForClientOrDefault(context.getEnvironment(),
|
||||
"configurations", "weighted");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ import org.springframework.cloud.loadbalancer.core.HealthCheckServiceInstanceLis
|
||||
import org.springframework.cloud.loadbalancer.core.RequestBasedStickySessionServiceInstanceListSupplier;
|
||||
import org.springframework.cloud.loadbalancer.core.RetryAwareServiceInstanceListSupplier;
|
||||
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
|
||||
import org.springframework.cloud.loadbalancer.core.WeightedServiceInstanceListSupplier;
|
||||
import org.springframework.cloud.loadbalancer.core.ZonePreferenceServiceInstanceListSupplier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -46,6 +47,7 @@ import static org.assertj.core.api.BDDAssertions.then;
|
||||
* Tests for {@link LoadBalancerClientConfiguration}.
|
||||
*
|
||||
* @author Olga Maciaszek-Sharma
|
||||
* @author Zhuozhi Ji
|
||||
*/
|
||||
class LoadBalancerClientConfigurationTests {
|
||||
|
||||
@@ -113,6 +115,18 @@ class LoadBalancerClientConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldInstantiateWeightedServiceInstanceListSupplier() {
|
||||
reactiveDiscoveryClientRunner.withUserConfiguration(TestConfig.class)
|
||||
.withPropertyValues("spring.cloud.loadbalancer.configurations=weighted").run(context -> {
|
||||
ServiceInstanceListSupplier supplier = context.getBean(ServiceInstanceListSupplier.class);
|
||||
then(supplier).isInstanceOf(WeightedServiceInstanceListSupplier.class);
|
||||
ServiceInstanceListSupplier delegate = ((DelegatingServiceInstanceListSupplier) supplier)
|
||||
.getDelegate();
|
||||
then(delegate).isInstanceOf(DiscoveryClientServiceInstanceListSupplier.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldInstantiateRequestBasedStickySessionServiceInstanceListSupplierTests() {
|
||||
reactiveDiscoveryClientRunner.withUserConfiguration(TestConfig.class)
|
||||
@@ -182,6 +196,17 @@ class LoadBalancerClientConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldInstantiateBlockingWeightedServiceInstanceListSupplier() {
|
||||
blockingDiscoveryClientRunner.withUserConfiguration(RestTemplateTestConfig.class)
|
||||
.withPropertyValues("spring.cloud.loadbalancer.configurations=weighted").run(context -> {
|
||||
ServiceInstanceListSupplier supplier = context.getBean(ServiceInstanceListSupplier.class);
|
||||
then(supplier).isInstanceOf(WeightedServiceInstanceListSupplier.class);
|
||||
then(((DelegatingServiceInstanceListSupplier) supplier).getDelegate())
|
||||
.isInstanceOf(DiscoveryClientServiceInstanceListSupplier.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class TestConfig {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user