Browse Source

feat: support autoconfigure WeightedServiceInstanceListSupplier (#1163)

pull/1165/head
jizhuozhi 2 years ago committed by GitHub
parent
commit
aef64061be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      docs/src/main/asciidoc/spring-cloud-commons.adoc
  2. 27
      spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/annotation/LoadBalancerClientConfiguration.java
  3. 25
      spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/annotation/LoadBalancerClientConfigurationTests.java

2
docs/src/main/asciidoc/spring-cloud-commons.adoc

@ -935,7 +935,7 @@ By default, we try to read and parse the weight from the metadata map (the key i @@ -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]

27
spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/annotation/LoadBalancerClientConfiguration.java

@ -57,6 +57,7 @@ import org.springframework.web.reactive.function.client.WebClient; @@ -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 { @@ -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 { @@ -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 { @@ -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");
}
}
}

25
spring-cloud-loadbalancer/src/test/java/org/springframework/cloud/loadbalancer/annotation/LoadBalancerClientConfigurationTests.java

@ -33,6 +33,7 @@ import org.springframework.cloud.loadbalancer.core.HealthCheckServiceInstanceLis @@ -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; @@ -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 { @@ -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 { @@ -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 {

Loading…
Cancel
Save