The load balancer in version 2021.0.6.2 does not prioritize using services from the current cluster for RPC calls.
Which Component LoadBalancer
Describe the bug
After upgrading from version 2021.0.5 to 2021.0.6.2, during RPC calls, the system no longer prioritizes services within the current cluster for RPC invocation.
I have two clusters: yun and local:
When spring.cloud.loadbalancer.nacos.enabled=true is configured in the configuration file,
in version 2021.0.5.0 of spring-cloud-alibaba-dependencies, during RPC calls, the method com.alibaba.cloud.nacos.loadbalancer.NacosLoadBalancer#getInstanceResponse will be used to prioritize returning instances that match the current cluster name.
However, in version 2021.0.6.2 of spring-cloud-alibaba-dependencies, this method is no longer invoked. I only know that now incorrect cluster instances are being used, causing RPC exceptions: feign.RetryableException: Connect to 192.168.0.11:9830 [/192.168.0.11] failed: connect timed out executing POST http://service-resource/resource/classroom/listStuCourseInfo at feign.FeignException.errorExecuting(FeignException.java:268) at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:131) at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:91) at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:100)
Simplest demo
Expected behavior Continue to follow the load balancing logic of version 2021.0.5, prioritizing the return of instances that match the current cluster name.
Additional context spring-cloud-alibaba-dependencies 2021.0.6.2 spring-cloud-dependencies 2021.0.5 spring-boot-starter-parent 2.7.13 java 1.8
The reason is that when Spring evaluates the @ConditionalOnLoadBalancerNacos condition in org.springframework.boot.autoconfigure.condition.OnPropertyCondition.Spec#collectProperties, it uses org.springframework.core.env.StandardEnvironment instead of org.springframework.cloud.bootstrap.BootstrapApplicationListener. As a result, the beans defined in com.alibaba.cloud.nacos.loadbalancer.NacosLoadBalancerClientConfiguration are not registered.
Since it reads from org.springframework.core.env.StandardEnvironment, the current workaround is to start the application using the following command:
java -jar -Dspring.cloud.loadbalancer.nacos.enabled=true xxx.jar
This issue has been open 30 days with no activity. This will be closed in 7 days.
@zhangbinhub
It doesn't seem to be an SBA issue
It doesn't seem to be an SBA issue
Can you remove the @ConditionalOnLoadBalancerNacos annotation if you don't know why Spring reads configurations from org.springframework.core.env.StandardEnvironment?
This issue has been open 30 days with no activity. This will be closed in 7 days.
1
Hello @HDHXTM . I took a look at the code, and the issue is related to changes in the activation conditions for the @ConditionalOnLoadBalancerNacos annotation. It has been changed from @ConditionalOnProperty(value = "spring.cloud.loadbalancer.nacos.enabled", matchIfMissing = true) to @ConditionalOnProperty(value = "spring.cloud.loadbalancer.nacos.enabled", havingValue = "true"). This means that to use NacosLoadBalancer now, you must explicitly configure spring.cloud.loadbalancer.nacos.enabled=true. Based on the commit messages, this is not an unintended behavior but a deliberate change. My personal guess is that it might be for better integration with the Spring Cloud ecosystem. In summary, in future versions, you'll need to manually configure spring.cloud.loadbalancer.nacos.enabled=true to use NacosLoadBalancer.
This issue has been open 30 days with no activity. This will be closed in 7 days.
Hello @HDHXTM . I took a look at the code, and the issue is related to changes in the activation conditions for the @ConditionalOnLoadBalancerNacos annotation. It has been changed from
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.nacos.enabled", matchIfMissing = true)to@ConditionalOnProperty(value = "spring.cloud.loadbalancer.nacos.enabled", havingValue = "true"). This means that to use NacosLoadBalancer now, you must explicitly configure spring.cloud.loadbalancer.nacos.enabled=true. Based on the commit messages, this is not an unintended behavior but a deliberate change. My personal guess is that it might be for better integration with the Spring Cloud ecosystem. In summary, in future versions, you'll need to manually configure spring.cloud.loadbalancer.nacos.enabled=true to use NacosLoadBalancer.
demo.zip @uuuyuqi Please try running this demo. The property spring.cloud.loadbalancer.nacos.enabled=true has already been configured in bootstrap.properties. When you run it directly, you'll notice in org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient#choose that it uses org.springframework.cloud.loadbalancer.core.RoundRobinLoadBalancer instead of com.alibaba.cloud.nacos.loadbalancer.NacosLoadBalancer.
However, if you set the environment variable spring.cloud.loadbalancer.nacos.enabled=true in IntelliJ IDEA, it correctly uses com.alibaba.cloud.nacos.loadbalancer.NacosLoadBalancer at that point.
demo.zip @uuuyuqi Please try running this demo. The property spring.cloud.loadbalancer.nacos.enabled=true has already been configured in bootstrap.properties. When you run it directly, you'll notice in org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient#choose that it uses org.springframework.cloud.loadbalancer.core.RoundRobinLoadBalancer instead of com.alibaba.cloud.nacos.loadbalancer.NacosLoadBalancer.
However, if you set the environment variable spring.cloud.loadbalancer.nacos.enabled=true in IntelliJ IDEA, it correctly uses com.alibaba.cloud.nacos.loadbalancer.NacosLoadBalancer at that point.
Only by using com.alibaba.cloud.nacos.loadbalancer.NacosLoadBalancer can services with the same cluster name be prioritized, which is necessary to avoid the issue I'm encountering.
This issue has been open 30 days with no activity. This will be closed in 7 days.
没人管了,就这样吧,给他们留个坑
However, if you set the environment variable spring.cloud.loadbalancer.nacos.enabled=true in IntelliJ IDEA, it correctly uses com.alibaba.cloud.nacos.loadbalancer.NacosLoadBalancer at that point.
Only by using com.alibaba.cloud.nacos.loadbalancer.NacosLoadBalancer can services with the same cluster name be prioritized, which is necessary to avoid the issue I'm encountering.