Q. Is there any reason why does LoadBalancerCacheManager Bean excluded from autowire candidates?
kworkbee opened this issue · 3 comments
Hi, I'm planning to implement a new feature so that LoadBalancer can switch to another zone if all instances within a particular zone fail to respond.
For acheiving this, the new ServiceInstanceListSupplier needs cache, which is why I wanted to inject LoadBalancerCacheManager
.
I checked that the Bean is currently excluded from Autowire Candidates, and I wonder if there is a reason why it was written like this.
I wonder if it is okay to exclude that option to achieve what I intend, or if not, what other alternatives can be used.
I'm not sure what you mean by it being excluded from Autowire Candidates. The caching should work out of the box. If you're experiencing any issues with it, please provide a minimal, complete, verifiable example that reproduces the issue.
In LoadBalancerCacheAutoConfiguration
, LoadBalancerCacheManager
beans seem to be registered excluded from the autowire candidates.
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ Caffeine.class, CaffeineCacheManager.class })
protected static class CaffeineLoadBalancerCacheManagerConfiguration {
// Here
@Bean(autowireCandidate = false)
@ConditionalOnMissingBean
LoadBalancerCacheManager caffeineLoadBalancerCacheManager(LoadBalancerCacheProperties cacheProperties) {
return new CaffeineBasedLoadBalancerCacheManager(cacheProperties);
}
}
@Configuration(proxyBeanMethods = false)
@Conditional(OnCaffeineCacheMissingCondition.class)
@ConditionalOnClass(ConcurrentMapWithTimedEviction.class)
protected static class DefaultLoadBalancerCacheManagerConfiguration {
// Here
@Bean(autowireCandidate = false)
@ConditionalOnMissingBean
LoadBalancerCacheManager defaultLoadBalancerCacheManager(LoadBalancerCacheProperties cacheProperties) {
return new DefaultLoadBalancerCacheManager(cacheProperties);
}
}
Because of this configuration, the newly registered Bean cannot be injected with LoadBalancerCacheManager
.
@Bean
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "zone-failover-awareness")
// LoadBalancerCacheManager cannot be injected
LoadBalancerCacheDataManager zoneFailoverAwarenessLoadBalancerCacheManager(LoadBalancerCacheManager cacheManager) {
return new ZoneFailoverAwarenessLoadBalancerCacheDataManager(cacheManager);
}
If I include beans excluded from the autowire candidates again, the existing tests below will fail.
These tests exactly show why this is done - so that the LoadBalancerCache does not override cache managers configured by users or taken up from other autoconfigurations.