You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by zh...@apache.org on 2022/06/03 14:32:26 UTC
[incubator-shenyu] branch master updated: add the logic of annotation on the splicing class for motan client; (#3479)
This is an automated email from the ASF dual-hosted git repository.
zhangzicheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 1e14a1704 add the logic of annotation on the splicing class for motan client; (#3479)
1e14a1704 is described below
commit 1e14a170436bef05c3d5b4e7507d8d2e88e97e84
Author: ChineseTony <ta...@163.com>
AuthorDate: Fri Jun 3 22:32:17 2022 +0800
add the logic of annotation on the splicing class for motan client; (#3479)
---
.../motan/MotanServiceBeanPostProcessor.java | 44 ++++++++++++++++++++--
.../motan/service/impl/MotanDemoServiceImpl.java | 3 +-
.../integrated/test/motan/MotanPluginTest.java | 2 +-
3 files changed, 43 insertions(+), 6 deletions(-)
diff --git a/shenyu-client/shenyu-client-motan/src/main/java/org/apache/shenyu/client/motan/MotanServiceBeanPostProcessor.java b/shenyu-client/shenyu-client-motan/src/main/java/org/apache/shenyu/client/motan/MotanServiceBeanPostProcessor.java
index 58e3ca87c..db0d7e8a4 100644
--- a/shenyu-client/shenyu-client-motan/src/main/java/org/apache/shenyu/client/motan/MotanServiceBeanPostProcessor.java
+++ b/shenyu-client/shenyu-client-motan/src/main/java/org/apache/shenyu/client/motan/MotanServiceBeanPostProcessor.java
@@ -39,6 +39,8 @@ import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
+import org.springframework.core.annotation.AnnotationUtils;
+import org.springframework.lang.NonNull;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Method;
@@ -59,6 +61,8 @@ public class MotanServiceBeanPostProcessor implements BeanPostProcessor, Applica
private static final String BASE_SERVICE_CONFIG = "baseServiceConfig";
+ private static final String PATH_SEPARATOR = "/";
+
private final LocalVariableTableParameterNameDiscoverer localVariableTableParameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
private final ShenyuClientRegisterEventPublisher publisher = ShenyuClientRegisterEventPublisher.getInstance();
@@ -116,21 +120,34 @@ public class MotanServiceBeanPostProcessor implements BeanPostProcessor, Applica
if (AopUtils.isAopProxy(bean)) {
clazz = AopUtils.getTargetClass(bean);
}
- Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(clazz);
+ String superPath = buildApiSuperPath(clazz);
MotanService service = clazz.getAnnotation(MotanService.class);
+ ShenyuMotanClient beanShenyuClient = AnnotationUtils.findAnnotation(clazz, ShenyuMotanClient.class);
+ if (superPath.contains("*")) {
+ Method[] methods = ReflectionUtils.getDeclaredMethods(clazz);
+ for (Method method : methods) {
+ if (Objects.nonNull(beanShenyuClient)) {
+ publisher.publishEvent(buildMetaDataDTO(clazz, service, beanShenyuClient, method,
+ buildRpcExt(method, timeout), superPath));
+ }
+ }
+ return;
+ }
+ Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(clazz);
for (Method method : methods) {
ShenyuMotanClient shenyuMotanClient = method.getAnnotation(ShenyuMotanClient.class);
if (Objects.nonNull(shenyuMotanClient)) {
publisher.publishEvent(buildMetaDataDTO(clazz, service,
- shenyuMotanClient, method, buildRpcExt(method, timeout)));
+ shenyuMotanClient, method, buildRpcExt(method, timeout), superPath));
}
}
}
private MetaDataRegisterDTO buildMetaDataDTO(final Class<?> clazz, final MotanService service,
- final ShenyuMotanClient shenyuMotanClient, final Method method, final String rpcExt) {
+ final ShenyuMotanClient shenyuMotanClient,
+ final Method method, final String rpcExt, final String superPath) {
String appName = this.appName;
- String path = this.contextPath + shenyuMotanClient.path();
+ String path = superPath.contains("*") ? pathJoin(contextPath, superPath.replace("*", ""), method.getName()) : pathJoin(contextPath, superPath, shenyuMotanClient.path());
String desc = shenyuMotanClient.desc();
String host = IpUtils.isCompleteHost(this.host) ? this.host : IpUtils.getHost(this.host);
int port = StringUtils.isBlank(this.port) ? -1 : Integer.parseInt(this.port);
@@ -187,6 +204,25 @@ public class MotanServiceBeanPostProcessor implements BeanPostProcessor, Applica
return GsonUtils.getInstance().toJson(buildList);
}
+ private String buildApiSuperPath(@NonNull final Class<?> clazz) {
+ ShenyuMotanClient shenyuMotanClient = AnnotationUtils.findAnnotation(clazz, ShenyuMotanClient.class);
+ if (Objects.nonNull(shenyuMotanClient) && StringUtils.isNotBlank(shenyuMotanClient.path())) {
+ return shenyuMotanClient.path();
+ }
+ return "";
+ }
+
+ private String pathJoin(@NonNull final String... path) {
+ StringBuilder result = new StringBuilder(PATH_SEPARATOR);
+ for (String p : path) {
+ if (!result.toString().endsWith(PATH_SEPARATOR)) {
+ result.append(PATH_SEPARATOR);
+ }
+ result.append(p.startsWith(PATH_SEPARATOR) ? p.replaceFirst(PATH_SEPARATOR, "") : p);
+ }
+ return result.toString();
+ }
+
@Override
public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
diff --git a/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-service/src/main/java/org/apache/shenyu/examples/motan/service/impl/MotanDemoServiceImpl.java b/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-service/src/main/java/org/apache/shenyu/examples/motan/service/impl/MotanDemoServiceImpl.java
index 52d3f5059..8c0bb388b 100644
--- a/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-service/src/main/java/org/apache/shenyu/examples/motan/service/impl/MotanDemoServiceImpl.java
+++ b/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-service/src/main/java/org/apache/shenyu/examples/motan/service/impl/MotanDemoServiceImpl.java
@@ -25,6 +25,7 @@ import org.apache.shenyu.examples.motan.service.MotanDemoService;
* Motan demo service.
*/
@MotanService(export = "demoMotan:8002")
+@ShenyuMotanClient(path = "/demo/**")
public class MotanDemoServiceImpl implements MotanDemoService {
@Override
@@ -35,7 +36,7 @@ public class MotanDemoServiceImpl implements MotanDemoService {
@Override
@ShenyuMotanClient(path = "/timeout")
- public String testTimeOut(final String timeout) {
+ public String testTimeOut(final String timeout) {
try {
Thread.sleep((long) (Double.parseDouble(timeout) * 1000));
} catch (InterruptedException e) {
diff --git a/shenyu-integrated-test/shenyu-integrated-test-motan/src/test/java/org/apache/shenyu/integrated/test/motan/MotanPluginTest.java b/shenyu-integrated-test/shenyu-integrated-test-motan/src/test/java/org/apache/shenyu/integrated/test/motan/MotanPluginTest.java
index 0868c1b3c..e510c68de 100644
--- a/shenyu-integrated-test/shenyu-integrated-test-motan/src/test/java/org/apache/shenyu/integrated/test/motan/MotanPluginTest.java
+++ b/shenyu-integrated-test/shenyu-integrated-test-motan/src/test/java/org/apache/shenyu/integrated/test/motan/MotanPluginTest.java
@@ -45,7 +45,7 @@ public class MotanPluginTest extends AbstractPluginDataInit {
MotanDTO request = new MotanDTO("shenyu");
Type returnType = new TypeToken<String>() {
}.getType();
- String response = HttpHelper.INSTANCE.postGateway("/motan/hello", request, returnType);
+ String response = HttpHelper.INSTANCE.postGateway("/motan/demo/hello", request, returnType);
assertEquals("hello shenyu", response);
}