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 1b4266c84 [ISSUE 3407] sub task1 add the logic of annotation on the
splicing class for apache dubbo client (#3423)
1b4266c84 is described below
commit 1b4266c84ff519f4383484d37586f6a9cac9f7ab
Author: renzhuyan <[email protected]>
AuthorDate: Thu May 26 22:51:07 2022 +0800
[ISSUE 3407] sub task1 add the logic of annotation on the splicing class
for apache dubbo client (#3423)
* Add the logic of annotation on the splicing class for apache dubbo client
---
.../dubbo/ApacheDubboServiceBeanListener.java | 46 ++++++++-
.../impl/DubboClassMultiParamServiceImpl.java | 96 +++++++++++++++++++
.../annotation/impl/DubboClassTestServiceImpl.java | 62 ++++++++++++
.../api/service/DubboClassMultiParamService.java | 105 +++++++++++++++++++++
.../dubbo/api/service/DubboClassTestService.java | 59 ++++++++++++
5 files changed, 363 insertions(+), 5 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 f520cf05a..c284eb4ef 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
@@ -37,6 +37,8 @@ import org.springframework.aop.support.AopUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.util.ReflectionUtils;
+import org.springframework.core.annotation.AnnotationUtils;
+import org.springframework.lang.NonNull;
import java.lang.reflect.Method;
import java.util.Arrays;
@@ -58,6 +60,11 @@ public class ApacheDubboServiceBeanListener implements
ApplicationListener<Conte
private static final String DEFAULT_CLUSTER = "failover";
+ /**
+ * api path separator.
+ */
+ private static final String PATH_SEPARATOR = "/";
+
private static final Boolean DEFAULT_SENT = Boolean.FALSE;
private ShenyuClientRegisterEventPublisher publisher =
ShenyuClientRegisterEventPublisher.getInstance();
@@ -118,18 +125,47 @@ public class ApacheDubboServiceBeanListener implements
ApplicationListener<Conte
if (AopUtils.isAopProxy(refProxy)) {
clazz = AopUtils.getTargetClass(refProxy);
}
+ final ShenyuDubboClient beanShenyuClient =
AnnotationUtils.findAnnotation(clazz, ShenyuDubboClient.class);
+ final String superPath = buildApiSuperPath(clazz, beanShenyuClient);
+ if (superPath.contains("*")) {
+ Method[] methods = ReflectionUtils.getDeclaredMethods(clazz);
+ for (Method method : methods) {
+ if (Objects.nonNull(beanShenyuClient)) {
+ publisher.publishEvent(buildMetaDataDTO(serviceBean,
beanShenyuClient, method, superPath));
+ }
+ }
+ return;
+ }
Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(clazz);
for (Method method : methods) {
- ShenyuDubboClient shenyuDubboClient =
method.getAnnotation(ShenyuDubboClient.class);
- if (Objects.nonNull(shenyuDubboClient)) {
- publisher.publishEvent(buildMetaDataDTO(serviceBean,
shenyuDubboClient, method));
+ ShenyuDubboClient methodShenyuClient =
method.getAnnotation(ShenyuDubboClient.class);
+ if (Objects.nonNull(methodShenyuClient)) {
+ publisher.publishEvent(buildMetaDataDTO(serviceBean,
methodShenyuClient, method, superPath));
+ }
+ }
+ }
+
+ private String buildApiSuperPath(@NonNull final Class<?> clazz, final
ShenyuDubboClient shenyuDubboClient) {
+ if (Objects.nonNull(shenyuDubboClient) &&
!StringUtils.isBlank(shenyuDubboClient.path())) {
+ return shenyuDubboClient.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();
}
- private MetaDataRegisterDTO buildMetaDataDTO(final ServiceBean<?>
serviceBean, final ShenyuDubboClient shenyuDubboClient, final Method method) {
+ private MetaDataRegisterDTO buildMetaDataDTO(final ServiceBean<?>
serviceBean, final ShenyuDubboClient shenyuDubboClient, final Method method,
final String superPath) {
String appName = buildAppName(serviceBean);
- String path = contextPath + shenyuDubboClient.path();
+ String path = superPath.contains("*") ? pathJoin(contextPath,
superPath.replace("*", ""), method.getName()) : pathJoin(contextPath,
superPath, shenyuDubboClient.path());
String desc = shenyuDubboClient.desc();
String serviceName = serviceBean.getInterface();
String configRuleName = shenyuDubboClient.ruleName();
diff --git
a/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service-annotation/src/main/java/org/apache/shenyu/examples/apache/dubbo/service/annotation/impl/DubboClassMultiParamServiceImpl.java
b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service-annotation/src/main/java/org/apache/shenyu/examples/apache/dubbo/service/annotation/impl/DubboClassMultiParamServiceImpl.java
new file mode 100644
index 000000000..ba43a3061
--- /dev/null
+++
b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service-annotation/src/main/java/org/apache/shenyu/examples/apache/dubbo/service/annotation/impl/DubboClassMultiParamServiceImpl.java
@@ -0,0 +1,96 @@
+/*
+ * 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.examples.apache.dubbo.service.annotation.impl;
+
+import org.apache.dubbo.config.annotation.Service;
+import org.apache.shenyu.client.dubbo.common.annotation.ShenyuDubboClient;
+import org.apache.shenyu.examples.dubbo.api.entity.ComplexBeanTest;
+import org.apache.shenyu.examples.dubbo.api.entity.DubboTest;
+import
org.apache.shenyu.examples.dubbo.api.service.DubboClassMultiParamService;
+import org.springframework.lang.NonNull;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/**
+ * The type Dubbo multi param service.
+ */
+@ShenyuDubboClient(path = "/demo")
+@Service
+public class DubboClassMultiParamServiceImpl implements
DubboClassMultiParamService {
+
+ @Override
+ @ShenyuDubboClient(path = "/findByIdsAndName", desc = "findByIdsAndName")
+ public DubboTest findByIdsAndName(final List<Integer> ids, final String
name) {
+ return new DubboTest(ids.toString(), "hello world shenyu apache dubbo
param findByIdsAndName :" + name);
+ }
+
+ @Override
+ @ShenyuDubboClient(path = "/findByArrayIdsAndName", desc =
"findByArrayIdsAndName")
+ public DubboTest findByArrayIdsAndName(final Integer[] ids, final String
name) {
+ return new DubboTest(Arrays.toString(ids), "hello world shenyu apache
dubbo param findByArrayIdsAndName :" + name);
+ }
+
+ @Override
+ @ShenyuDubboClient(path = "/findByStringArray", desc = "findByStringArray")
+ public DubboTest findByStringArray(final String[] ids) {
+ return new DubboTest(Arrays.toString(ids), "hello world shenyu apache
dubbo param findByStringArray");
+ }
+
+ @Override
+ @ShenyuDubboClient(path = "/findByListId", desc = "findByListId")
+ public DubboTest findByListId(final List<String> ids) {
+ return new DubboTest(ids.toString(), "hello world shenyu apache dubbo
param findByListId");
+ }
+
+ @Override
+ @ShenyuDubboClient(path = "/batchSave", desc = "batchSave")
+ public DubboTest batchSave(final List<DubboTest> dubboTestList) {
+ return new DubboTest(join(dubboTestList, DubboTest::getId),
+ "hello world shenyu apache dubbo param batchSave :" +
join(dubboTestList, DubboTest::getName));
+ }
+
+ @Override
+ @ShenyuDubboClient(path = "/batchSaveAndNameAndId", desc =
"batchSaveAndNameAndId")
+ public DubboTest batchSaveAndNameAndId(final List<DubboTest>
dubboTestList, final String id, final String name) {
+ return new DubboTest(id, "hello world shenyu apache dubbo param
batchSaveAndNameAndId :"
+ + name + ":" + join(dubboTestList, DubboTest::getName));
+ }
+
+ @Override
+ @ShenyuDubboClient(path = "/saveComplexBeanTest", desc =
"saveComplexBeanTest")
+ public DubboTest saveComplexBeanTest(final ComplexBeanTest
complexBeanTest) {
+ return new DubboTest(complexBeanTest.getIdLists().toString(),
+ "hello world shenyu apache dubbo param saveComplexBeanTest :"
+ complexBeanTest.getDubboTest().getName());
+ }
+
+ @Override
+ @ShenyuDubboClient(path = "/saveComplexBeanTestAndName", desc =
"saveComplexBeanTestAndName")
+ public DubboTest saveComplexBeanTestAndName(final ComplexBeanTest
complexBeanTest, final String name) {
+ return new DubboTest(complexBeanTest.getIdLists().toString(),
+ "hello world shenyu alibaba dubbo param
saveComplexBeanTestAndName :" + complexBeanTest.getDubboTest().getName() + "-"
+ name);
+ }
+
+ private <T> String join(final @NonNull List<T> list, final Function<T,
String> mapper) {
+ return list.stream()
+ .map(mapper)
+ .collect(Collectors.joining("-"));
+ }
+}
diff --git
a/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service-annotation/src/main/java/org/apache/shenyu/examples/apache/dubbo/service/annotation/impl/DubboClassTestServiceImpl.java
b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service-annotation/src/main/java/org/apache/shenyu/examples/apache/dubbo/service/annotation/impl/DubboClassTestServiceImpl.java
new file mode 100644
index 000000000..a738d52ec
--- /dev/null
+++
b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service-annotation/src/main/java/org/apache/shenyu/examples/apache/dubbo/service/annotation/impl/DubboClassTestServiceImpl.java
@@ -0,0 +1,62 @@
+/*
+ * 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.examples.apache.dubbo.service.annotation.impl;
+
+import org.apache.dubbo.config.annotation.Service;
+import org.apache.dubbo.rpc.RpcContext;
+import org.apache.shenyu.client.dubbo.common.annotation.ShenyuDubboClient;
+import org.apache.shenyu.common.utils.GsonUtils;
+import org.apache.shenyu.examples.dubbo.api.entity.DubboTest;
+import org.apache.shenyu.examples.dubbo.api.entity.ListResp;
+import org.apache.shenyu.examples.dubbo.api.service.DubboClassTestService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collections;
+import java.util.Random;
+
+/**
+ * The type Dubbo service.
+ */
+@ShenyuDubboClient(path = "/demo/**", desc = "class init")
+@Service
+public class DubboClassTestServiceImpl implements DubboClassTestService {
+
+ private static final Logger LOGGER =
LoggerFactory.getLogger(DubboClassTestServiceImpl.class);
+
+ @Override
+ public DubboTest findById(final String id) {
+
LOGGER.info(GsonUtils.getInstance().toJson(RpcContext.getContext().getAttachments()));
+ return new DubboTest(id, "hello world shenyu Apache, findById");
+ }
+
+ @Override
+ public DubboTest findAll() {
+ return new DubboTest(String.valueOf(new Random().nextInt()), "hello
world shenyu Apache, findAll");
+ }
+
+ @Override
+ public DubboTest insert(final DubboTest dubboTest) {
+ dubboTest.setName("hello world shenyu Apache Dubbo: " +
dubboTest.getName());
+ return dubboTest;
+ }
+
+ @Override
+ public ListResp findList() {
+ return new ListResp(1, Collections.singletonList(new DubboTest("1",
"test")));
+ }
+}
diff --git
a/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-dubbo-api/src/main/java/org/apache/shenyu/examples/dubbo/api/service/DubboClassMultiParamService.java
b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-dubbo-api/src/main/java/org/apache/shenyu/examples/dubbo/api/service/DubboClassMultiParamService.java
new file mode 100644
index 000000000..33133837a
--- /dev/null
+++
b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-dubbo-api/src/main/java/org/apache/shenyu/examples/dubbo/api/service/DubboClassMultiParamService.java
@@ -0,0 +1,105 @@
+/*
+ * 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.examples.dubbo.api.service;
+
+import org.apache.shenyu.examples.dubbo.api.entity.ComplexBeanTest;
+import org.apache.shenyu.examples.dubbo.api.entity.DubboTest;
+
+import java.util.List;
+
+/**
+ * The interface Dubbo class multi param service.
+ */
+public interface DubboClassMultiParamService {
+
+ /**
+ * Find by ids and name dubbo test.
+ * body: {"ids":["1232","456"],"name":"hello world"}
+ *
+ * @param ids the ids
+ * @param name the name
+ * @return the dubbo test
+ */
+ DubboTest findByIdsAndName(List<Integer> ids, String name);
+
+ /**
+ * Find by array ids and name dubbo test.
+ * body :{"ids":[123,4561],"name":"hello world"}
+ *
+ * @param ids the ids
+ * @param name the name
+ * @return the dubbo test
+ */
+ DubboTest findByArrayIdsAndName(Integer[] ids, String name);
+
+ /**
+ * Find by string array dubbo test.
+ * body :{"ids":["1232","456"]}
+ *
+ * @param ids the ids
+ * @return the dubbo test
+ */
+ DubboTest findByStringArray(String[] ids);
+
+ /**
+ * Find by list id dubbo test.
+ * body :{"ids":["1232","456"]}
+ *
+ * @param ids the ids
+ * @return the dubbo test
+ */
+ DubboTest findByListId(List<String> ids);
+
+ /**
+ * Batch save dubbo test.
+ * body
:{"dubboTestList":[{"id":"123","name":"xiaoyu"},{"id":"456","name":"myth"}]}
+ *
+ * @param dubboTestList the dubbo test list
+ * @return the dubbo test
+ */
+ DubboTest batchSave(List<DubboTest> dubboTestList);
+
+ /**
+ * Batch save and name and id dubbo test.
+ * body:
{"dubboTestList":[{"id":"123","name":"xiaoyu"},{"id":"456","name":"myth"}],"id":"789","name":"ttt"}
+ *
+ * @param dubboTestList the dubbo test list
+ * @param id the id
+ * @param name the name
+ * @return the dubbo test
+ */
+ DubboTest batchSaveAndNameAndId(List<DubboTest> dubboTestList, String id,
String name);
+
+ /**
+ * Save complex bean test dubbo test.
+ * body :
{"dubboTest":{"id":"123","name":"xiaoyu"},"idLists":["456","789"],"idMaps":{"id2":"2","id1":"1"}}
+ *
+ * @param complexBeanTest the complex bean test
+ * @return the dubbo test
+ */
+ DubboTest saveComplexBeanTest(ComplexBeanTest complexBeanTest);
+
+ /**
+ * Save complex bean test and name dubbo test.
+ * body :
{"complexBeanTest":{"dubboTest":{"id":"123","name":"xiaoyu"},"idLists":["456","789"],"idMaps":{"id2":"2","id1":"1"}},"name":"xiaoyu"}
+ * @param complexBeanTest the complex bean test
+ * @param name the name
+ * @return the dubbo test
+ */
+ DubboTest saveComplexBeanTestAndName(ComplexBeanTest complexBeanTest,
String name);
+}
diff --git
a/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-dubbo-api/src/main/java/org/apache/shenyu/examples/dubbo/api/service/DubboClassTestService.java
b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-dubbo-api/src/main/java/org/apache/shenyu/examples/dubbo/api/service/DubboClassTestService.java
new file mode 100644
index 000000000..ccafb9c21
--- /dev/null
+++
b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-dubbo-api/src/main/java/org/apache/shenyu/examples/dubbo/api/service/DubboClassTestService.java
@@ -0,0 +1,59 @@
+/*
+ * 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.examples.dubbo.api.service;
+
+import org.apache.shenyu.examples.dubbo.api.entity.DubboTest;
+import org.apache.shenyu.examples.dubbo.api.entity.ListResp;
+
+/**
+ * DubboClassTestService.
+ */
+public interface DubboClassTestService {
+
+ /**
+ * find by id.
+ * body:{"id":"1223"}
+ *
+ * @param id id
+ * @return DubboTest dubbo test
+ */
+ DubboTest findById(String id);
+
+ /**
+ * Find all dubbo test.
+ *
+ * @return the dubbo test
+ */
+ DubboTest findAll();
+
+ /**
+ * Insert dubbo test.
+ * body :{"id":"122344","name":"xiaoyu"}
+ *
+ * @param dubboTest the dubbo test
+ * @return the dubbo test
+ */
+ DubboTest insert(DubboTest dubboTest);
+
+ /**
+ * findList.
+ *
+ * @return {@linkplain ListResp}
+ */
+ ListResp findList();
+}