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 8fc09d7499 [type:fix] set dubbo method config in reference (#5944)
8fc09d7499 is described below
commit 8fc09d7499288cc795d923513676e09e756961f5
Author: eye-gu <[email protected]>
AuthorDate: Thu Feb 27 16:14:11 2025 +0800
[type:fix] set dubbo method config in reference (#5944)
Co-authored-by: xiaoyu <[email protected]>
---
.../dubbo/ApacheDubboServiceBeanListener.java | 27 ++--
.../client/dubbo/common/dto/DubboRpcExt.java | 36 ++++++
.../client/dubbo/common/dto/DubboRpcMethodExt.java | 136 +++++++++++++++++++++
.../apache/dubbo/cache/ApacheDubboConfigCache.java | 21 ++++
.../dubbo/proxy/ApacheDubboGrayLoadBalance.java | 3 +-
.../dubbo/common/cache/DubboMethodParam.java | 136 +++++++++++++++++++++
.../plugin/dubbo/common/cache/DubboParam.java | 25 ++++
7 files changed, 372 insertions(+), 12 deletions(-)
diff --git
a/shenyu-client/shenyu-client-dubbo/shenyu-client-apache-dubbo/src/main/java/org/apache/shenyu/client/apache/dubbo/ApacheDubboServiceBeanListener.java
b/shenyu-client/shenyu-client-dubbo/shenyu-client-apache-dubbo/src/main/java/org/apache/shenyu/client/apache/dubbo/ApacheDubboServiceBeanListener.java
index 1f20a01a12..c537b04c3d 100644
---
a/shenyu-client/shenyu-client-dubbo/shenyu-client-apache-dubbo/src/main/java/org/apache/shenyu/client/apache/dubbo/ApacheDubboServiceBeanListener.java
+++
b/shenyu-client/shenyu-client-dubbo/shenyu-client-apache-dubbo/src/main/java/org/apache/shenyu/client/apache/dubbo/ApacheDubboServiceBeanListener.java
@@ -19,11 +19,13 @@ package org.apache.shenyu.client.apache.dubbo;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.common.constants.CommonConstants;
+import org.apache.dubbo.config.MethodConfig;
import org.apache.dubbo.config.spring.ServiceBean;
import
org.apache.shenyu.client.core.client.AbstractContextRefreshedEventListener;
import org.apache.shenyu.client.core.constant.ShenyuClientConstants;
import org.apache.shenyu.client.dubbo.common.annotation.ShenyuDubboClient;
import org.apache.shenyu.client.dubbo.common.dto.DubboRpcExt;
+import org.apache.shenyu.client.dubbo.common.dto.DubboRpcMethodExt;
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.enums.ApiHttpMethodEnum;
import org.apache.shenyu.common.enums.RpcTypeEnum;
@@ -44,8 +46,8 @@ import org.springframework.util.ReflectionUtils;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
+import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -233,16 +235,19 @@ public class ApacheDubboServiceBeanListener extends
AbstractContextRefreshedEven
.url("")
.serialization(serviceBean.getSerialization())
.build();
- // set method config: loadbalance,retries,timeout,sent
-
Optional.ofNullable(serviceBean.getMethods()).orElse(Collections.emptyList()).stream()
- .filter(m -> methodName.equals(m.getName()))
- .findFirst()
- .ifPresent(methodConfig -> {
-
Optional.ofNullable(methodConfig.getLoadbalance()).filter(StringUtils::isNotEmpty).ifPresent(build::setLoadbalance);
-
Optional.ofNullable(methodConfig.getRetries()).ifPresent(build::setRetries);
-
Optional.ofNullable(methodConfig.getTimeout()).ifPresent(build::setTimeout);
-
Optional.ofNullable(methodConfig.getSent()).ifPresent(build::setSent);
- });
+ // method config: loadbalance,retries,timeout,sent
+ if (Objects.nonNull(serviceBean.getMethods())) {
+ build.setMethods(new ArrayList<>());
+ for (MethodConfig methodConfig : serviceBean.getMethods()) {
+ DubboRpcMethodExt methodExt = new DubboRpcMethodExt();
+ methodExt.setName(methodConfig.getName());
+ methodExt.setLoadbalance(methodConfig.getLoadbalance());
+ methodExt.setRetries(methodConfig.getRetries());
+ methodExt.setTimeout(methodConfig.getTimeout());
+ methodExt.setSent(methodConfig.getSent());
+ build.getMethods().add(methodExt);
+ }
+ }
return GsonUtils.getInstance().toJson(build);
}
}
diff --git
a/shenyu-client/shenyu-client-dubbo/shenyu-client-dubbo-common/src/main/java/org/apache/shenyu/client/dubbo/common/dto/DubboRpcExt.java
b/shenyu-client/shenyu-client-dubbo/shenyu-client-dubbo-common/src/main/java/org/apache/shenyu/client/dubbo/common/dto/DubboRpcExt.java
index 3cc1b840ba..a583a7803b 100644
---
a/shenyu-client/shenyu-client-dubbo/shenyu-client-dubbo-common/src/main/java/org/apache/shenyu/client/dubbo/common/dto/DubboRpcExt.java
+++
b/shenyu-client/shenyu-client-dubbo/shenyu-client-dubbo-common/src/main/java/org/apache/shenyu/client/dubbo/common/dto/DubboRpcExt.java
@@ -18,6 +18,7 @@
package org.apache.shenyu.client.dubbo.common.dto;
import java.io.Serializable;
+import java.util.List;
/**
* The type Dubbo rpc ext.
@@ -46,6 +47,8 @@ public class DubboRpcExt implements Serializable {
private String serialization;
+ private List<DubboRpcMethodExt> methods;
+
/**
* constructor without parameter.
*/
@@ -267,6 +270,24 @@ public class DubboRpcExt implements Serializable {
this.serialization = serialization;
}
+ /**
+ * get methods.
+ *
+ * @return methods
+ */
+ public List<DubboRpcMethodExt> getMethods() {
+ return methods;
+ }
+
+ /**
+ * set methods.
+ *
+ * @param methods methods
+ */
+ public void setMethods(final List<DubboRpcMethodExt> methods) {
+ this.methods = methods;
+ }
+
@Override
public String toString() {
return "DubboRpcExt{"
@@ -280,6 +301,7 @@ public class DubboRpcExt implements Serializable {
+ ", cluster='" + cluster + '\''
+ ", protocol='" + protocol + '\''
+ ", serialization='" + serialization + '\''
+ + ", methods=" + methods + '\''
+ '}';
}
@@ -317,6 +339,8 @@ public class DubboRpcExt implements Serializable {
private String serialization;
+ private List<DubboRpcMethodExt> methods;
+
/**
* constructor without parameter.
*/
@@ -433,6 +457,17 @@ public class DubboRpcExt implements Serializable {
return this;
}
+ /**
+ * set methods.
+ *
+ * @param methods methods
+ * @return Builder
+ */
+ public Builder methods(final List<DubboRpcMethodExt> methods) {
+ this.methods = methods;
+ return this;
+ }
+
/**
* build DubboRpcExt.
*
@@ -450,6 +485,7 @@ public class DubboRpcExt implements Serializable {
dubboRpcExt.setCluster(cluster);
dubboRpcExt.setProtocol(protocol);
dubboRpcExt.setSerialization(serialization);
+ dubboRpcExt.setMethods(methods);
return dubboRpcExt;
}
}
diff --git
a/shenyu-client/shenyu-client-dubbo/shenyu-client-dubbo-common/src/main/java/org/apache/shenyu/client/dubbo/common/dto/DubboRpcMethodExt.java
b/shenyu-client/shenyu-client-dubbo/shenyu-client-dubbo-common/src/main/java/org/apache/shenyu/client/dubbo/common/dto/DubboRpcMethodExt.java
new file mode 100644
index 0000000000..2a628e227b
--- /dev/null
+++
b/shenyu-client/shenyu-client-dubbo/shenyu-client-dubbo-common/src/main/java/org/apache/shenyu/client/dubbo/common/dto/DubboRpcMethodExt.java
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.client.dubbo.common.dto;
+
+import java.io.Serializable;
+
+public class DubboRpcMethodExt implements Serializable {
+
+ private static final long serialVersionUID = 1685981839659220568L;
+
+ private String name;
+
+ private String loadbalance;
+
+ private Integer retries;
+
+ private Integer timeout;
+
+ private Boolean sent;
+
+ /**
+ * get name.
+ *
+ * @return name
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * set name.
+ *
+ * @param name name
+ */
+ public void setName(final String name) {
+ this.name = name;
+ }
+
+ /**
+ * get loadbalance.
+ *
+ * @return loadbalance
+ */
+ public String getLoadbalance() {
+ return loadbalance;
+ }
+
+ /**
+ * set loadbalance.
+ *
+ * @param loadbalance loadbalance
+ */
+ public void setLoadbalance(final String loadbalance) {
+ this.loadbalance = loadbalance;
+ }
+
+ /**
+ * get retries.
+ *
+ * @return retries
+ */
+ public Integer getRetries() {
+ return retries;
+ }
+
+ /**
+ * set retries.
+ *
+ * @param retries retries
+ */
+ public void setRetries(final Integer retries) {
+ this.retries = retries;
+ }
+
+ /**
+ * get timeout.
+ *
+ * @return timeout
+ */
+ public Integer getTimeout() {
+ return timeout;
+ }
+
+ /**
+ * set timeout.
+ *
+ * @param timeout timeout
+ */
+ public void setTimeout(final Integer timeout) {
+ this.timeout = timeout;
+ }
+
+ /**
+ * get sent.
+ *
+ * @return sent
+ */
+ public Boolean getSent() {
+ return sent;
+ }
+
+ /**
+ * set sent.
+ *
+ * @param sent sent
+ */
+ public void setSent(final Boolean sent) {
+ this.sent = sent;
+ }
+
+ @Override
+ public String toString() {
+ return "DubboRpcMethodExt{"
+ + "name='" + name + '\''
+ + ", loadbalance='" + loadbalance + '\''
+ + ", retries=" + retries
+ + ", timeout=" + timeout
+ + ", sent=" + sent
+ + '}';
+ }
+}
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 20d12d7aff..c65fa58968 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
@@ -21,16 +21,20 @@ import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener;
+
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import jakarta.annotation.Nonnull;
+import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ConsumerConfig;
+import org.apache.dubbo.config.MethodConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.service.GenericService;
@@ -39,6 +43,7 @@ import org.apache.shenyu.common.dto.MetaData;
import org.apache.shenyu.common.dto.convert.plugin.DubboRegisterConfig;
import org.apache.shenyu.common.exception.ShenyuException;
import org.apache.shenyu.plugin.dubbo.common.cache.DubboConfigCache;
+import org.apache.shenyu.plugin.dubbo.common.cache.DubboMethodParam;
import org.apache.shenyu.plugin.dubbo.common.cache.DubboParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -235,6 +240,22 @@ public final class ApacheDubboConfigCache extends
DubboConfigCache {
Optional.ofNullable(dubboParam.getTimeout()).ifPresent(reference::setTimeout);
Optional.ofNullable(dubboParam.getRetries()).ifPresent(reference::setRetries);
Optional.ofNullable(dubboParam.getSent()).ifPresent(reference::setSent);
+ // methods
+ if (CollectionUtils.isNotEmpty(dubboParam.getMethods())) {
+ reference.setMethods(new ArrayList<>());
+ for (DubboMethodParam dubboMethodParam :
dubboParam.getMethods()) {
+ MethodConfig methodConfig = new MethodConfig();
+ methodConfig.setName(dubboMethodParam.getName());
+ methodConfig.setLoadbalance("gray");
+ methodConfig.setRetries(dubboMethodParam.getRetries());
+ methodConfig.setTimeout(dubboMethodParam.getTimeout());
+ methodConfig.setSent(dubboMethodParam.getSent());
+ Map<String, String> methodsParameters = new HashMap<>(1);
+ methodsParameters.put(Constants.DUBBO_LOAD_BALANCE,
dubboMethodParam.getLoadbalance());
+ methodConfig.setParameters(methodsParameters);
+ reference.getMethods().add(methodConfig);
+ }
+ }
}
if (StringUtils.isNotBlank(namespace)) {
RegistryConfig registryConfig = new RegistryConfig();
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 53a819f358..bacbf901b2 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
@@ -26,6 +26,7 @@ import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.cluster.LoadBalance;
+import org.apache.dubbo.rpc.support.RpcUtils;
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.dto.convert.rule.impl.DubboRuleHandle;
import org.apache.shenyu.common.dto.convert.selector.DubboUpstream;
@@ -89,7 +90,7 @@ public class ApacheDubboGrayLoadBalance implements
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);
+ String loadBalance =
Optional.ofNullable(url.getMethodParameter(RpcUtils.getMethodName(invocation),
Constants.DUBBO_LOAD_BALANCE)).orElse(CommonConstants.DEFAULT_LOADBALANCE);
return
ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(loadBalance).select(invokers,
url, invocation);
}
}
diff --git
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-dubbo-common/src/main/java/org/apache/shenyu/plugin/dubbo/common/cache/DubboMethodParam.java
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-dubbo-common/src/main/java/org/apache/shenyu/plugin/dubbo/common/cache/DubboMethodParam.java
new file mode 100644
index 0000000000..813317eab2
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-dubbo-common/src/main/java/org/apache/shenyu/plugin/dubbo/common/cache/DubboMethodParam.java
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.dubbo.common.cache;
+
+public class DubboMethodParam {
+
+ /**
+ * name.
+ */
+ private String name;
+
+ /**
+ * loadbalance.
+ */
+ private String loadbalance;
+
+ /**
+ * retries.
+ */
+ private Integer retries;
+
+ /**
+ * timeout.
+ */
+ private Integer timeout;
+
+ /**
+ * sent.
+ */
+ private Boolean sent;
+
+ /**
+ * get name.
+ *
+ * @return name
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * set name.
+ *
+ * @param name name
+ */
+ public void setName(final String name) {
+ this.name = name;
+ }
+
+ /**
+ * get loadbalance.
+ *
+ * @return loadbalance
+ */
+ public String getLoadbalance() {
+ return loadbalance;
+ }
+
+ /**
+ * set loadbalance.
+ *
+ * @param loadbalance loadbalance
+ */
+ public void setLoadbalance(final String loadbalance) {
+ this.loadbalance = loadbalance;
+ }
+
+ /**
+ * get retries.
+ *
+ * @return retries
+ */
+ public Integer getRetries() {
+ return retries;
+ }
+
+ /**
+ * set retries.
+ *
+ * @param retries retries
+ */
+ public void setRetries(final Integer retries) {
+ this.retries = retries;
+ }
+
+ /**
+ * get timeout.
+ *
+ * @return timeout
+ */
+ public Integer getTimeout() {
+ return timeout;
+ }
+
+ /**
+ * set timeout.
+ *
+ * @param timeout timeout
+ */
+ public void setTimeout(final Integer timeout) {
+ this.timeout = timeout;
+ }
+
+ /**
+ * get sent.
+ *
+ * @return sent
+ */
+ public Boolean getSent() {
+ return sent;
+ }
+
+ /**
+ * set sent.
+ *
+ * @param sent sent
+ */
+ public void setSent(final Boolean sent) {
+ this.sent = sent;
+ }
+}
diff --git
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-dubbo-common/src/main/java/org/apache/shenyu/plugin/dubbo/common/cache/DubboParam.java
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-dubbo-common/src/main/java/org/apache/shenyu/plugin/dubbo/common/cache/DubboParam.java
index f3a9be0c80..89d71bd3ae 100644
---
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-dubbo-common/src/main/java/org/apache/shenyu/plugin/dubbo/common/cache/DubboParam.java
+++
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-dubbo-common/src/main/java/org/apache/shenyu/plugin/dubbo/common/cache/DubboParam.java
@@ -17,6 +17,8 @@
package org.apache.shenyu.plugin.dubbo.common.cache;
+import java.util.List;
+
/**
* DubboParam.
*/
@@ -72,6 +74,11 @@ public class DubboParam {
*/
private String serialization;
+ /**
+ * the methods.
+ */
+ private List<DubboMethodParam> methods;
+
/**
* Gets group.
*
@@ -249,4 +256,22 @@ public class DubboParam {
public void setSerialization(final String serialization) {
this.serialization = serialization;
}
+
+ /**
+ * get methods.
+ *
+ * @return methods
+ */
+ public List<DubboMethodParam> getMethods() {
+ return methods;
+ }
+
+ /**
+ * set methods.
+ *
+ * @param methods methods
+ */
+ public void setMethods(final List<DubboMethodParam> methods) {
+ this.methods = methods;
+ }
}