spring-cloud-alibaba icon indicating copy to clipboard operation
spring-cloud-alibaba copied to clipboard

The load balancer in version 2021.0.6.2 does not prioritize using services from the current cluster for RPC calls.

Open HDHXTM opened this issue 5 months ago • 12 comments

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: Image 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

HDHXTM avatar Jul 14 '25 07:07 HDHXTM

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.Image Image 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

HDHXTM avatar Jul 15 '25 00:07 HDHXTM

This issue has been open 30 days with no activity. This will be closed in 7 days.

github-actions[bot] avatar Aug 14 '25 18:08 github-actions[bot]

@zhangbinhub

HDHXTM avatar Aug 15 '25 06:08 HDHXTM

It doesn't seem to be an SBA issue

zhangbinhub avatar Aug 15 '25 08:08 zhangbinhub

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?

Image

HDHXTM avatar Aug 28 '25 05:08 HDHXTM

This issue has been open 30 days with no activity. This will be closed in 7 days.

github-actions[bot] avatar Sep 27 '25 18:09 github-actions[bot]

1

HDHXTM avatar Sep 28 '25 01:09 HDHXTM

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.

uuuyuqi avatar Sep 30 '25 10:09 uuuyuqi

This issue has been open 30 days with no activity. This will be closed in 7 days.

github-actions[bot] avatar Nov 09 '25 18:11 github-actions[bot]

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.

Image I'm sure I've set spring.cloud.loadbalancer.nacos.enabled=true.

HDHXTM avatar Nov 10 '25 00:11 HDHXTM

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.

Image

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.

Image 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. Please fix this bug as soon as possible.

HDHXTM avatar Nov 10 '25 02:11 HDHXTM

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.

Image 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.

Image 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.

HDHXTM avatar Nov 10 '25 02:11 HDHXTM

This issue has been open 30 days with no activity. This will be closed in 7 days.

github-actions[bot] avatar Dec 11 '25 18:12 github-actions[bot]

没人管了,就这样吧,给他们留个坑

HDHXTM avatar Dec 12 '25 00:12 HDHXTM