Why is the bean 'reactorServiceInstanceLoadBalancer' instantiated just when the method 'getInstance' is called?

huangapple go评论84阅读模式
英文:

Why is the bean 'reactorServiceInstanceLoadBalancer' instantiated just when the method 'getInstance' is called?

问题

我尝试通过实现 ReactorServiceInstanceLoadBalancer 接口来定义一个自定义的负载均衡器,以替换由 RoundRobinLoadBalancer 定义的默认负载均衡器。

但是它并没有生效。

我发现在应用程序启动时,并没有实例化在类 'LoadBalancerClientConfiguration' 中定义的原始 bean,而是在调用 'LoadBalancerClientFactory.getInstance' 时实例化,构造函数使用了一个 StandardEnvironment 的 bean 进行自动装配,然而在我的配置中定义的 bean 则是在应用程序启动时实例化,使用了一个 StandardReactiveWebEnvironment 的 bean 进行自动装配。

非常困惑!
我的英文不是很好。感谢您阅读完整的描述!
以下是我的代码:

@Slf4j
public class CustomLoadBalancer implements ReactorServiceInstanceLoadBalancer {
    // ...细节被省略
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnDiscoveryEnabled
public class CustomLoadBalancerClientConfiguration {
    @Bean
    public ReactorLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer(Environment environment, LoadBalancerClientFactory loadBalancerClientFactory, ServiceInstanceChooser serviceInstanceChooser) {
        String name = loadBalancerClientFactory.getName(environment);
        return new CustomLoadBalancer(serviceInstanceChooser, loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
    }
}
英文:

Im trying to define a custom LoadBalancer by implementation the interface ReactorServiceInstanceLoadBalancer to replace the default load balancer defined by RoundRobinLoadBalancer.

But it doesn't work.

I found that the original bean defined in class 'LoadBalancerClientConfiguration' isn't instantiated when the application startup,but instantiated when 'LoadBalancerClientFactory.getInstance' is called, and the contructor is autowired with a bean of StandardEnvironment, while the bean defined in my configuration is instantiated when the application startup, and autowired with a bean of StandardReactiveWebEnvironment.

Very confused!
My english is not so good.Thank you for reading the whole description!
Here is my code below:

    `@Slf4j
public class CustomLoadBalancer implements ReactorServiceInstanceLoadBalancer {
    // ...detail omitted
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnDiscoveryEnabled
public class CustomLoadBalancerClientConfiguration {
    @Bean
    public ReactorLoadBalancer&lt;ServiceInstance&gt; reactorServiceInstanceLoadBalancer(Environment environment, LoadBalancerClientFactory loadBalancerClientFactory, ServiceInstanceChooser serviceInstanceChooser) {
        String name = loadBalancerClientFactory.getName(environment);
        return new CustomLoadBalancer(serviceInstanceChooser, loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
    }
}

答案1

得分: 2

一个独立的上下文,包括 ReactorServiceInstanceLoadBalancer 实例,会根据所请求的 serviceId 进行创建。可以自行提供实现,无论是默认实现还是针对特定服务的实现。有关如何为 Spring Cloud LoadBalancer 提供自定义配置,请参阅文档

英文:

A separate context, including the ReactorServiceInstanceLoadBalancer instance is created per requested serviceId. It is possible, to provide your own implementation, both default or for a specific service. Please see the documentation on how to provide custom configuration for Spring Cloud LoadBalancer.

huangapple
  • 本文由 发表于 2020年10月21日 15:04:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64458301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定