Skip to content

Commit 8fc09d7

Browse files
eye-guyu199195
andauthored
[type:fix] set dubbo method config in reference (#5944)
Co-authored-by: xiaoyu <[email protected]>
1 parent 5dcc2a3 commit 8fc09d7

File tree

7 files changed

+372
-12
lines changed

7 files changed

+372
-12
lines changed

shenyu-client/shenyu-client-dubbo/shenyu-client-apache-dubbo/src/main/java/org/apache/shenyu/client/apache/dubbo/ApacheDubboServiceBeanListener.java

+16-11
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919

2020
import org.apache.commons.lang3.StringUtils;
2121
import org.apache.dubbo.common.constants.CommonConstants;
22+
import org.apache.dubbo.config.MethodConfig;
2223
import org.apache.dubbo.config.spring.ServiceBean;
2324
import org.apache.shenyu.client.core.client.AbstractContextRefreshedEventListener;
2425
import org.apache.shenyu.client.core.constant.ShenyuClientConstants;
2526
import org.apache.shenyu.client.dubbo.common.annotation.ShenyuDubboClient;
2627
import org.apache.shenyu.client.dubbo.common.dto.DubboRpcExt;
28+
import org.apache.shenyu.client.dubbo.common.dto.DubboRpcMethodExt;
2729
import org.apache.shenyu.common.constant.Constants;
2830
import org.apache.shenyu.common.enums.ApiHttpMethodEnum;
2931
import org.apache.shenyu.common.enums.RpcTypeEnum;
@@ -44,8 +46,8 @@
4446

4547
import java.lang.annotation.Annotation;
4648
import java.lang.reflect.Method;
49+
import java.util.ArrayList;
4750
import java.util.Arrays;
48-
import java.util.Collections;
4951
import java.util.List;
5052
import java.util.Map;
5153
import java.util.Objects;
@@ -233,16 +235,19 @@ private String buildRpcExt(final ServiceBean<?> serviceBean, final String method
233235
.url("")
234236
.serialization(serviceBean.getSerialization())
235237
.build();
236-
// set method config: loadbalance,retries,timeout,sent
237-
Optional.ofNullable(serviceBean.getMethods()).orElse(Collections.emptyList()).stream()
238-
.filter(m -> methodName.equals(m.getName()))
239-
.findFirst()
240-
.ifPresent(methodConfig -> {
241-
Optional.ofNullable(methodConfig.getLoadbalance()).filter(StringUtils::isNotEmpty).ifPresent(build::setLoadbalance);
242-
Optional.ofNullable(methodConfig.getRetries()).ifPresent(build::setRetries);
243-
Optional.ofNullable(methodConfig.getTimeout()).ifPresent(build::setTimeout);
244-
Optional.ofNullable(methodConfig.getSent()).ifPresent(build::setSent);
245-
});
238+
// method config: loadbalance,retries,timeout,sent
239+
if (Objects.nonNull(serviceBean.getMethods())) {
240+
build.setMethods(new ArrayList<>());
241+
for (MethodConfig methodConfig : serviceBean.getMethods()) {
242+
DubboRpcMethodExt methodExt = new DubboRpcMethodExt();
243+
methodExt.setName(methodConfig.getName());
244+
methodExt.setLoadbalance(methodConfig.getLoadbalance());
245+
methodExt.setRetries(methodConfig.getRetries());
246+
methodExt.setTimeout(methodConfig.getTimeout());
247+
methodExt.setSent(methodConfig.getSent());
248+
build.getMethods().add(methodExt);
249+
}
250+
}
246251
return GsonUtils.getInstance().toJson(build);
247252
}
248253
}

shenyu-client/shenyu-client-dubbo/shenyu-client-dubbo-common/src/main/java/org/apache/shenyu/client/dubbo/common/dto/DubboRpcExt.java

+36
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.shenyu.client.dubbo.common.dto;
1919

2020
import java.io.Serializable;
21+
import java.util.List;
2122

2223
/**
2324
* The type Dubbo rpc ext.
@@ -46,6 +47,8 @@ public class DubboRpcExt implements Serializable {
4647

4748
private String serialization;
4849

50+
private List<DubboRpcMethodExt> methods;
51+
4952
/**
5053
* constructor without parameter.
5154
*/
@@ -267,6 +270,24 @@ public void setSerialization(final String serialization) {
267270
this.serialization = serialization;
268271
}
269272

273+
/**
274+
* get methods.
275+
*
276+
* @return methods
277+
*/
278+
public List<DubboRpcMethodExt> getMethods() {
279+
return methods;
280+
}
281+
282+
/**
283+
* set methods.
284+
*
285+
* @param methods methods
286+
*/
287+
public void setMethods(final List<DubboRpcMethodExt> methods) {
288+
this.methods = methods;
289+
}
290+
270291
@Override
271292
public String toString() {
272293
return "DubboRpcExt{"
@@ -280,6 +301,7 @@ public String toString() {
280301
+ ", cluster='" + cluster + '\''
281302
+ ", protocol='" + protocol + '\''
282303
+ ", serialization='" + serialization + '\''
304+
+ ", methods=" + methods + '\''
283305
+ '}';
284306
}
285307

@@ -317,6 +339,8 @@ public static final class Builder {
317339

318340
private String serialization;
319341

342+
private List<DubboRpcMethodExt> methods;
343+
320344
/**
321345
* constructor without parameter.
322346
*/
@@ -433,6 +457,17 @@ public Builder serialization(final String serialization) {
433457
return this;
434458
}
435459

460+
/**
461+
* set methods.
462+
*
463+
* @param methods methods
464+
* @return Builder
465+
*/
466+
public Builder methods(final List<DubboRpcMethodExt> methods) {
467+
this.methods = methods;
468+
return this;
469+
}
470+
436471
/**
437472
* build DubboRpcExt.
438473
*
@@ -450,6 +485,7 @@ public DubboRpcExt build() {
450485
dubboRpcExt.setCluster(cluster);
451486
dubboRpcExt.setProtocol(protocol);
452487
dubboRpcExt.setSerialization(serialization);
488+
dubboRpcExt.setMethods(methods);
453489
return dubboRpcExt;
454490
}
455491
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shenyu.client.dubbo.common.dto;
19+
20+
import java.io.Serializable;
21+
22+
public class DubboRpcMethodExt implements Serializable {
23+
24+
private static final long serialVersionUID = 1685981839659220568L;
25+
26+
private String name;
27+
28+
private String loadbalance;
29+
30+
private Integer retries;
31+
32+
private Integer timeout;
33+
34+
private Boolean sent;
35+
36+
/**
37+
* get name.
38+
*
39+
* @return name
40+
*/
41+
public String getName() {
42+
return name;
43+
}
44+
45+
/**
46+
* set name.
47+
*
48+
* @param name name
49+
*/
50+
public void setName(final String name) {
51+
this.name = name;
52+
}
53+
54+
/**
55+
* get loadbalance.
56+
*
57+
* @return loadbalance
58+
*/
59+
public String getLoadbalance() {
60+
return loadbalance;
61+
}
62+
63+
/**
64+
* set loadbalance.
65+
*
66+
* @param loadbalance loadbalance
67+
*/
68+
public void setLoadbalance(final String loadbalance) {
69+
this.loadbalance = loadbalance;
70+
}
71+
72+
/**
73+
* get retries.
74+
*
75+
* @return retries
76+
*/
77+
public Integer getRetries() {
78+
return retries;
79+
}
80+
81+
/**
82+
* set retries.
83+
*
84+
* @param retries retries
85+
*/
86+
public void setRetries(final Integer retries) {
87+
this.retries = retries;
88+
}
89+
90+
/**
91+
* get timeout.
92+
*
93+
* @return timeout
94+
*/
95+
public Integer getTimeout() {
96+
return timeout;
97+
}
98+
99+
/**
100+
* set timeout.
101+
*
102+
* @param timeout timeout
103+
*/
104+
public void setTimeout(final Integer timeout) {
105+
this.timeout = timeout;
106+
}
107+
108+
/**
109+
* get sent.
110+
*
111+
* @return sent
112+
*/
113+
public Boolean getSent() {
114+
return sent;
115+
}
116+
117+
/**
118+
* set sent.
119+
*
120+
* @param sent sent
121+
*/
122+
public void setSent(final Boolean sent) {
123+
this.sent = sent;
124+
}
125+
126+
@Override
127+
public String toString() {
128+
return "DubboRpcMethodExt{"
129+
+ "name='" + name + '\''
130+
+ ", loadbalance='" + loadbalance + '\''
131+
+ ", retries=" + retries
132+
+ ", timeout=" + timeout
133+
+ ", sent=" + sent
134+
+ '}';
135+
}
136+
}

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
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,20 @@
2121
import com.google.common.cache.CacheLoader;
2222
import com.google.common.cache.LoadingCache;
2323
import com.google.common.cache.RemovalListener;
24+
25+
import java.util.ArrayList;
2426
import java.util.HashMap;
2527
import java.util.Map;
2628
import java.util.Objects;
2729
import java.util.Optional;
2830
import java.util.concurrent.ExecutionException;
2931
import jakarta.annotation.Nonnull;
32+
import org.apache.commons.collections4.CollectionUtils;
3033
import org.apache.commons.lang3.StringUtils;
3134
import org.apache.dubbo.common.constants.CommonConstants;
3235
import org.apache.dubbo.config.ApplicationConfig;
3336
import org.apache.dubbo.config.ConsumerConfig;
37+
import org.apache.dubbo.config.MethodConfig;
3438
import org.apache.dubbo.config.ReferenceConfig;
3539
import org.apache.dubbo.config.RegistryConfig;
3640
import org.apache.dubbo.rpc.service.GenericService;
@@ -39,6 +43,7 @@
3943
import org.apache.shenyu.common.dto.convert.plugin.DubboRegisterConfig;
4044
import org.apache.shenyu.common.exception.ShenyuException;
4145
import org.apache.shenyu.plugin.dubbo.common.cache.DubboConfigCache;
46+
import org.apache.shenyu.plugin.dubbo.common.cache.DubboMethodParam;
4247
import org.apache.shenyu.plugin.dubbo.common.cache.DubboParam;
4348
import org.slf4j.Logger;
4449
import org.slf4j.LoggerFactory;
@@ -235,6 +240,22 @@ private ReferenceConfig<GenericService> buildReference(final MetaData metaData,
235240
Optional.ofNullable(dubboParam.getTimeout()).ifPresent(reference::setTimeout);
236241
Optional.ofNullable(dubboParam.getRetries()).ifPresent(reference::setRetries);
237242
Optional.ofNullable(dubboParam.getSent()).ifPresent(reference::setSent);
243+
// methods
244+
if (CollectionUtils.isNotEmpty(dubboParam.getMethods())) {
245+
reference.setMethods(new ArrayList<>());
246+
for (DubboMethodParam dubboMethodParam : dubboParam.getMethods()) {
247+
MethodConfig methodConfig = new MethodConfig();
248+
methodConfig.setName(dubboMethodParam.getName());
249+
methodConfig.setLoadbalance("gray");
250+
methodConfig.setRetries(dubboMethodParam.getRetries());
251+
methodConfig.setTimeout(dubboMethodParam.getTimeout());
252+
methodConfig.setSent(dubboMethodParam.getSent());
253+
Map<String, String> methodsParameters = new HashMap<>(1);
254+
methodsParameters.put(Constants.DUBBO_LOAD_BALANCE, dubboMethodParam.getLoadbalance());
255+
methodConfig.setParameters(methodsParameters);
256+
reference.getMethods().add(methodConfig);
257+
}
258+
}
238259
}
239260
if (StringUtils.isNotBlank(namespace)) {
240261
RegistryConfig registryConfig = new RegistryConfig();

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.dubbo.rpc.Invoker;
2727
import org.apache.dubbo.rpc.RpcException;
2828
import org.apache.dubbo.rpc.cluster.LoadBalance;
29+
import org.apache.dubbo.rpc.support.RpcUtils;
2930
import org.apache.shenyu.common.constant.Constants;
3031
import org.apache.shenyu.common.dto.convert.rule.impl.DubboRuleHandle;
3132
import org.apache.shenyu.common.dto.convert.selector.DubboUpstream;
@@ -89,7 +90,7 @@ public <T> Invoker<T> select(final List<Invoker<T>> invokers, final URL url, fin
8990
}
9091

9192
private <T> Invoker<T> dubboSelect(final List<Invoker<T>> invokers, final URL url, final Invocation invocation) {
92-
String loadBalance = Optional.ofNullable(url.getParameter(Constants.DUBBO_LOAD_BALANCE)).orElse(CommonConstants.DEFAULT_LOADBALANCE);
93+
String loadBalance = Optional.ofNullable(url.getMethodParameter(RpcUtils.getMethodName(invocation), Constants.DUBBO_LOAD_BALANCE)).orElse(CommonConstants.DEFAULT_LOADBALANCE);
9394
return ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(loadBalance).select(invokers, url, invocation);
9495
}
9596
}

0 commit comments

Comments
 (0)