This is an automated email from the ASF dual-hosted git repository.

xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new a85e9ee5bb [type:fix] Use the loadbalance configuration from metadata  
for Dubbo (#5806)
a85e9ee5bb is described below

commit a85e9ee5bb2f78a9427c93384998831ace8d242d
Author: eye-gu <[email protected]>
AuthorDate: Tue Nov 26 09:48:02 2024 +0800

    [type:fix] Use the loadbalance configuration from metadata  for Dubbo 
(#5806)
    
    Co-authored-by: xiaoyu <[email protected]>
    Co-authored-by: loongs-zhang <[email protected]>
---
 .../java/org/apache/shenyu/common/constant/Constants.java |  5 +++++
 .../plugin/apache/dubbo/cache/ApacheDubboConfigCache.java |  3 +++
 .../apache/dubbo/proxy/ApacheDubboGrayLoadBalance.java    | 15 +++++++++------
 3 files changed, 17 insertions(+), 6 deletions(-)

diff --git 
a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java 
b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java
index 1fd835151a..794c625e12 100644
--- 
a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java
+++ 
b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java
@@ -552,6 +552,11 @@ public interface Constants {
      * dubbo remote address.
      */
     String DUBBO_REMOTE_ADDRESS = "dubboRemoteAddress";
+
+    /**
+     * dubbo load balance from meta data.
+     */
+    String DUBBO_LOAD_BALANCE = "dubboLoadBalance";
     
     /**
      * dubbo group.
diff --git 
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApacheDubboConfigCache.java
 
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApacheDubboConfigCache.java
index 329d545688..1aaf772eef 100644
--- 
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApacheDubboConfigCache.java
+++ 
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApacheDubboConfigCache.java
@@ -223,6 +223,9 @@ public final class ApacheDubboConfigCache extends 
DubboConfigCache {
             if (StringUtils.isNoneBlank(dubboParam.getCluster())) {
                 reference.setCluster(dubboParam.getCluster());
             }
+            if (StringUtils.isNoneBlank(dubboParam.getLoadbalance())) {
+                reference.getParameters().put(Constants.DUBBO_LOAD_BALANCE, 
dubboParam.getLoadbalance());
+            }
             // set dubbo sub protocol
             
Optional.ofNullable(dubboParam.getProtocol()).ifPresent(reference::setProtocol);
             
Optional.ofNullable(dubboParam.getTimeout()).ifPresent(reference::setTimeout);
diff --git 
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/proxy/ApacheDubboGrayLoadBalance.java
 
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/proxy/ApacheDubboGrayLoadBalance.java
index b958f047ac..53a819f358 100644
--- 
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/proxy/ApacheDubboGrayLoadBalance.java
+++ 
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/proxy/ApacheDubboGrayLoadBalance.java
@@ -20,6 +20,7 @@ package org.apache.shenyu.plugin.apache.dubbo.proxy;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.constants.CommonConstants;
 import org.apache.dubbo.common.extension.ExtensionLoader;
 import org.apache.dubbo.rpc.Invocation;
 import org.apache.dubbo.rpc.Invoker;
@@ -35,6 +36,7 @@ import 
org.apache.shenyu.plugin.apache.dubbo.handler.ApacheDubboPluginDataHandle
 
 import java.util.List;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.stream.Collectors;
 
 /**
@@ -53,10 +55,10 @@ public class ApacheDubboGrayLoadBalance implements 
LoadBalance {
         if (CollectionUtils.isNotEmpty(dubboUpstreams)) {
             Upstream upstream = 
LoadBalancerFactory.selector(UpstreamCacheManager.getInstance().findUpstreamListBySelectorId(shenyuSelectorId),
 dubboRuleHandle.getLoadBalance(), remoteAddressIp);
             if (Objects.isNull(upstream)) {
-                return select(invokers, url, invocation, 
dubboRuleHandle.getLoadBalance());
+                return dubboSelect(invokers, url, invocation);
             }
             if (StringUtils.isBlank(upstream.getUrl()) && 
StringUtils.isBlank(upstream.getGroup()) && 
StringUtils.isBlank(upstream.getVersion())) {
-                return select(invokers, url, invocation, 
dubboRuleHandle.getLoadBalance());
+                return dubboSelect(invokers, url, invocation);
             }
             // url is the first level, then is group, the version is the 
lowest.
             final List<Invoker<T>> invokerGrays = 
invokers.stream().filter(each -> {
@@ -79,14 +81,15 @@ public class ApacheDubboGrayLoadBalance implements 
LoadBalance {
                 return true;
             }).collect(Collectors.toList());
             if (CollectionUtils.isEmpty(invokerGrays)) {
-                return select(invokers, url, invocation, 
dubboRuleHandle.getLoadBalance());
+                return dubboSelect(invokers, url, invocation);
             }
-            return select(invokerGrays, url, invocation, 
dubboRuleHandle.getLoadBalance());
+            return dubboSelect(invokerGrays, url, invocation);
         }
-        return select(invokers, url, invocation, 
dubboRuleHandle.getLoadBalance());
+        return dubboSelect(invokers, url, invocation);
     }
     
-    private <T> Invoker<T> select(final List<Invoker<T>> invokers, final URL 
url, final Invocation invocation, final String loadBalance) {
+    private <T> Invoker<T> dubboSelect(final List<Invoker<T>> invokers, final 
URL url, final Invocation invocation) {
+        String loadBalance = 
Optional.ofNullable(url.getParameter(Constants.DUBBO_LOAD_BALANCE)).orElse(CommonConstants.DEFAULT_LOADBALANCE);
         return 
ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(loadBalance).select(invokers,
 url, invocation);
     }
 }

Reply via email to