Skip to content

Why the bean of 'SameInstancePreferenceServiceInstanceListSupplier' use 'withCaching()' method #1249

@ReionChan

Description

@ReionChan

The bean of SameInstancePreferenceServiceInstanceListSupplier in the LoadBalancerClientConfiguration:

@Bean
@ConditionalOnBean(DiscoveryClient.class)
@ConditionalOnMissingBean
@Conditional(SameInstancePreferenceConfigurationCondition.class)
public ServiceInstanceListSupplier sameInstancePreferenceServiceInstanceListSupplier(
		ConfigurableApplicationContext context) {
        
         // *** Config to use cache by calling method withCaching(), Why? ***
	return ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient().withSameInstancePreference()
			.withCaching().build(context);
}

When the SameInstancePreferenceServiceInstanceListSupplier is delegated to CachingServiceInstanceListSupplier,
the method 'selectedServiceInstance(ServiceInstance)' which is defined in SelectedInstanceCallback and implemented by SameInstancePreferenceServiceInstanceListSupplier has no chance to be call in the RoundRobinLoadBalancer or RandomLoadBalancer:

public Mono<Response<ServiceInstance>> choose(Request request) {
	ServiceInstanceListSupplier supplier = serviceInstanceListSupplierProvider
		.getIfAvailable(NoopServiceInstanceListSupplier::new);
	return supplier.get(request).next()
		.map(serviceInstances -> processInstanceResponse(supplier, serviceInstances));
}

private Response<ServiceInstance> processInstanceResponse(ServiceInstanceListSupplier supplier,
	List<ServiceInstance> serviceInstances) {
	Response<ServiceInstance> serviceInstanceResponse = getInstanceResponse(serviceInstances);

 	// *** The type of supplier is CachingServiceInstanceListSupplier so it will skip this if statement ***
	if (supplier instanceof SelectedInstanceCallback && serviceInstanceResponse.hasServer()) {
		((SelectedInstanceCallback) supplier).selectedServiceInstance(serviceInstanceResponse.getServer());
	}
	return serviceInstanceResponse;
}

This will result in the field named previouslyReturnedInstance in SameInstancePreferenceServiceInstanceListSupplier not being assigned a value, then the feature of SAME INSTANCE PREFERENCE would not working.

So is it necessary to add withCaching() in the LoadBalancerClientConfiguration to define the 'SameInstancePreferenceServiceInstanceListSupplier' bean?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions