Skip to content

Commit

Permalink
feat: support autoconfigure WeightedServiceInstanceListSupplier (#1163)
Browse files Browse the repository at this point in the history
  • Loading branch information
jizhuozhi committed Oct 27, 2022
1 parent 0a4ee74 commit aef6406
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/spring-cloud-commons.adoc
Expand Up @@ -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]
Expand Down
Expand Up @@ -57,6 +57,7 @@
* @author Tim Ysewyn
* @author BaoLin Zhu
* @author changjin wei(魏昌进)
* @author Zhuozhi Ji
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnDiscoveryEnabled
Expand Down Expand Up @@ -133,6 +134,14 @@ public ServiceInstanceListSupplier sameInstancePreferenceServiceInstanceListSupp
.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)
Expand Down Expand Up @@ -189,6 +198,14 @@ public ServiceInstanceListSupplier sameInstancePreferenceServiceInstanceListSupp
.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)
Expand Down Expand Up @@ -324,4 +341,14 @@ public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata)

}

static class WeightedConfigurationCondition implements Condition {

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return LoadBalancerEnvironmentPropertyUtils.equalToForClientOrDefault(context.getEnvironment(),
"configurations", "weighted");
}

}

}
Expand Up @@ -33,6 +33,7 @@
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;
Expand All @@ -46,6 +47,7 @@
* Tests for {@link LoadBalancerClientConfiguration}.
*
* @author Olga Maciaszek-Sharma
* @author Zhuozhi Ji
*/
class LoadBalancerClientConfigurationTests {

Expand Down Expand Up @@ -113,6 +115,18 @@ void shouldInstantiateHealthCheckServiceInstanceListSupplier() {
});
}

@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)
Expand Down Expand Up @@ -182,6 +196,17 @@ void shouldInstantiateBlockingHealthCheckServiceInstanceListSupplier() {
});
}

@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 {

Expand Down

0 comments on commit aef6406

Please sign in to comment.