Skip to content

Commit

Permalink
feat(lb): deterministic subsetting algorithm to limit the number of i…
Browse files Browse the repository at this point in the history
…nstances
  • Loading branch information
jizhuozhi committed Nov 9, 2023
1 parent 75d4691 commit 6ac12e2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
*
* @author Olga Maciaszek-Sharma
* @author Gandhimathi Velusamy
* @author Zhuozhi Ji
* @since 2.2.1
*/
public class LoadBalancerProperties {
Expand Down Expand Up @@ -88,7 +89,8 @@ public class LoadBalancerProperties {
private boolean callGetWithRequestOnDelegates = true;

/**
* Properties for <code>SubsetServiceInstanceListSupplier</code>.
* Properties for
* {@link org.springframework.cloud.loadbalancer.core.SubsetServiceInstanceListSupplier}.
*/
private Subset subset = new Subset();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ public ServiceInstanceListSupplier weightedServiceInstanceListSupplier(Configura
.build(context);
}

@Bean
@ConditionalOnBean(ReactiveDiscoveryClient.class)
@ConditionalOnMissingBean
@Conditional(SubsetConfigurationCondition.class)
public ServiceInstanceListSupplier subsetServiceInstanceListSupplier(ConfigurableApplicationContext context) {
return ServiceInstanceListSupplier.builder().withDiscoveryClient().withSubset().withCaching()
.build(context);
}

}

@Configuration(proxyBeanMethods = false)
Expand Down Expand Up @@ -353,4 +362,14 @@ public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata)

}

static class SubsetConfigurationCondition implements Condition {

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

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,7 @@ public SubsetServiceInstanceListSupplier(ServiceInstanceListSupplier delegate, P
ReactiveLoadBalancer.Factory<ServiceInstance> factory) {
super(delegate);
LoadBalancerProperties properties = factory.getProperties(getServiceId());
String instanceId = properties.getSubset().getInstanceId();
if (StringUtils.hasText(instanceId)) {
this.instanceId = resolver.resolvePlaceholders(properties.getSubset().getInstanceId());
}
else {
this.instanceId = IdUtils.getDefaultInstanceId(resolver);
}
this.instanceId = resolveInstanceId(properties, resolver);
this.size = properties.getSubset().getSize();
}

Expand All @@ -80,6 +74,14 @@ public Flux<List<ServiceInstance>> get() {
});
}

private static String resolveInstanceId(LoadBalancerProperties properties, PropertyResolver resolver) {
String instanceId = properties.getSubset().getInstanceId();
if (StringUtils.hasText(instanceId)) {
return resolver.resolvePlaceholders(properties.getSubset().getInstanceId());
}
return IdUtils.getDefaultInstanceId(resolver);
}

public String getInstanceId() {
return instanceId;
}
Expand Down

0 comments on commit 6ac12e2

Please sign in to comment.