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 2023/06/12 08:52:06 UTC

[shenyu] branch master updated: [type:refactor] enhance brpc (#4725)

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/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 3cb2debaf [type:refactor] enhance brpc (#4725)
3cb2debaf is described below

commit 3cb2debaf04955740cc0b88632e4440e21b958df
Author: zhengpeng <84...@qq.com>
AuthorDate: Mon Jun 12 16:51:56 2023 +0800

    [type:refactor] enhance brpc (#4725)
    
    * enhance brpc demo
    
    * enhance brpc demo
    
    * brpc enhance
    
    * enhance brpc
    
    * enhance brpc
    
    * Empty Commit to setup deployments
    
    * Empty Commit to setup deployments
    
    * Empty Commit to setup deployments
    
    * Empty Commit to setup deployments
    
    ---------
    
    Co-authored-by: likeguo <33...@users.noreply.github.com>
    Co-authored-by: xiaoyu <xi...@apache.org>
---
 .../shenyu/examples/brpc/api/entity/BigObject.java |  64 +++++++++++++
 .../examples/brpc/api/entity/ComplexObjects.java   |  64 +++++++++++++
 .../examples/brpc/api/service/BrpcTestService.java |  77 +++++++++++++++
 .../src/main/http/brpc-test-api.http               |  81 ++++++++++++++++
 .../brpc/service/impl/BrpcTestServiceImpl.java     | 104 +++++++++++++++++++++
 .../plugin/brpc/cache/ApplicationConfigCache.java  |  17 ++--
 .../shenyu/plugin/brpc/proxy/BrpcProxyService.java |   2 +-
 .../brpc/cache/ApplicationConfigCacheTest.java     |   4 +-
 8 files changed, 401 insertions(+), 12 deletions(-)

diff --git a/shenyu-examples/shenyu-examples-brpc/shenyu-examples-brpc-api/src/main/java/org/apache/shenyu/examples/brpc/api/entity/BigObject.java b/shenyu-examples/shenyu-examples-brpc/shenyu-examples-brpc-api/src/main/java/org/apache/shenyu/examples/brpc/api/entity/BigObject.java
new file mode 100644
index 000000000..cd8c10a34
--- /dev/null
+++ b/shenyu-examples/shenyu-examples-brpc/shenyu-examples-brpc-api/src/main/java/org/apache/shenyu/examples/brpc/api/entity/BigObject.java
@@ -0,0 +1,64 @@
+/*
+ * 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.brpc.api.entity;
+
+/**
+ * big object.
+ */
+public class BigObject {
+
+    private String[] obj;
+
+    private Integer id;
+
+    /**
+     * get id.
+     * @return id
+     */
+    public Integer getId() {
+        return id;
+    }
+
+    /**
+     * set id.
+     * @param id id
+     */
+    public void setId(final Integer id) {
+        this.id = id;
+    }
+
+
+    /**
+     * get obj.
+     *
+     * @return String[]
+     */
+    public String[] getObj() {
+        return obj;
+    }
+
+    /**
+     * set obj.
+     *
+     * @param obj obj
+     */
+    public void setObj(final String[] obj) {
+        this.obj = obj;
+    }
+
+}
diff --git a/shenyu-examples/shenyu-examples-brpc/shenyu-examples-brpc-api/src/main/java/org/apache/shenyu/examples/brpc/api/entity/ComplexObjects.java b/shenyu-examples/shenyu-examples-brpc/shenyu-examples-brpc-api/src/main/java/org/apache/shenyu/examples/brpc/api/entity/ComplexObjects.java
new file mode 100644
index 000000000..04e7d0f70
--- /dev/null
+++ b/shenyu-examples/shenyu-examples-brpc/shenyu-examples-brpc-api/src/main/java/org/apache/shenyu/examples/brpc/api/entity/ComplexObjects.java
@@ -0,0 +1,64 @@
+/*
+ * 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.brpc.api.entity;
+
+/**
+ * ComplexObjects.
+ */
+public class ComplexObjects {
+
+    private BigObject bigObject;
+
+    private User user;
+
+    /**
+     * getBigObject.
+     *
+     * @return BigObject
+     */
+    public BigObject getBigObject() {
+        return bigObject;
+    }
+
+    /**
+     * setBigObject.
+     *
+     * @param bigObject big object
+     */
+    public void setBigObject(final BigObject bigObject) {
+        this.bigObject = bigObject;
+    }
+
+    /**
+     * getUser.
+     *
+     * @return User
+     */
+    public User getUser() {
+        return user;
+    }
+
+    /**
+     * setUser.
+     *
+     * @param user User
+     */
+    public void setUser(final User user) {
+        this.user = user;
+    }
+}
diff --git a/shenyu-examples/shenyu-examples-brpc/shenyu-examples-brpc-api/src/main/java/org/apache/shenyu/examples/brpc/api/service/BrpcTestService.java b/shenyu-examples/shenyu-examples-brpc/shenyu-examples-brpc-api/src/main/java/org/apache/shenyu/examples/brpc/api/service/BrpcTestService.java
new file mode 100644
index 000000000..c241adbf3
--- /dev/null
+++ b/shenyu-examples/shenyu-examples-brpc/shenyu-examples-brpc-api/src/main/java/org/apache/shenyu/examples/brpc/api/service/BrpcTestService.java
@@ -0,0 +1,77 @@
+/*
+ * 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.brpc.api.service;
+
+import org.apache.shenyu.examples.brpc.api.entity.BigObject;
+import org.apache.shenyu.examples.brpc.api.entity.ComplexObjects;
+import org.apache.shenyu.examples.brpc.api.entity.User;
+
+import java.util.List;
+
+public interface BrpcTestService {
+
+    /**
+     * get array.
+     *
+     * @param param param
+     * @return array
+     */
+    String[] getArray(String[] param);
+
+
+    /**
+     * getUserByObj.
+     *
+     * @param inputUser input user
+     * @return user
+     */
+    User getUserByObj(User inputUser);
+
+    /**
+     * get users.
+     *
+     * @param inputUsers input users
+     * @return users
+     */
+    List<User> getUsersList0(List<User> inputUsers);
+
+    /**
+     * get users.
+     *
+     * @param inputUsers input users
+     * @param param      param
+     * @return users
+     */
+    List<User> getUsersList1(List<User> inputUsers, String[] param);
+
+    /**
+     * big objects.
+     *
+     * @return big objects
+     */
+    BigObject bigObject0();
+
+    /**
+     * big objects.
+     *
+     * @param user user
+     * @return big objects
+     */
+    ComplexObjects complexObjects(User user);
+
+}
diff --git a/shenyu-examples/shenyu-examples-brpc/shenyu-examples-brpc-service/src/main/http/brpc-test-api.http b/shenyu-examples/shenyu-examples-brpc/shenyu-examples-brpc-service/src/main/http/brpc-test-api.http
index 818db857e..230472862 100644
--- a/shenyu-examples/shenyu-examples-brpc/shenyu-examples-brpc-service/src/main/http/brpc-test-api.http
+++ b/shenyu-examples/shenyu-examples-brpc/shenyu-examples-brpc-service/src/main/http/brpc-test-api.http
@@ -51,4 +51,85 @@ Content-Type: application/json
 
 {
   "userId": 127
+}
+
+
+### shengyu getway proxy getArray
+POST http://localhost:9195/brpc/getArray
+Accept: application/json
+Content-Type: application/json
+
+{
+  "param": [
+    "aaa",
+    "bbb",
+    "cccc"
+  ]
+}
+
+
+### shengyu getway proxy getUserByObj
+POST http://localhost:9195/brpc/getUserByObj
+Accept: application/json
+Content-Type: application/json
+
+{
+  "inputUser":
+    {
+      "userId": 127,
+      "userName": "zst"
+    }
+}
+
+### shengyu getway proxy getUsersList0
+POST http://localhost:9195/brpc/getUsersList0
+Accept: application/json
+Content-Type: application/json
+
+{
+  "inputUsers": [
+    {
+      "userId": 127,
+      "userName": "zst"
+    }
+  ]
+}
+
+### shengyu getway proxy getUsersList1
+POST http://localhost:9195/brpc/getUsersList1
+Accept: application/json
+Content-Type: application/json
+
+{
+  "param": [
+    "aaa",
+    "bbb",
+    "cccc"
+  ],
+  "inputUsers": [
+    {
+      "userId": 127,
+      "userName": "zst"
+    }
+  ]
+}
+
+
+
+### shengyu getway proxy bigObject0
+POST http://localhost:9195/brpc/bigObject0
+Accept: application/json
+Content-Type: application/json
+
+
+### shengyu getway proxy complexObjects
+POST http://localhost:9195/brpc/complexObjects
+Accept: application/json
+Content-Type: application/json
+
+{
+"user": {
+  "userId": 127,
+  "name": "zst"
+}
 }
\ No newline at end of file
diff --git a/shenyu-examples/shenyu-examples-brpc/shenyu-examples-brpc-service/src/main/java/org/apache/shenyu/examples/brpc/service/impl/BrpcTestServiceImpl.java b/shenyu-examples/shenyu-examples-brpc/shenyu-examples-brpc-service/src/main/java/org/apache/shenyu/examples/brpc/service/impl/BrpcTestServiceImpl.java
new file mode 100644
index 000000000..9167c6285
--- /dev/null
+++ b/shenyu-examples/shenyu-examples-brpc/shenyu-examples-brpc-service/src/main/java/org/apache/shenyu/examples/brpc/service/impl/BrpcTestServiceImpl.java
@@ -0,0 +1,104 @@
+/*
+ * 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.brpc.service.impl;
+
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.shenyu.client.brpc.common.annotation.ShenyuBrpcClient;
+import org.apache.shenyu.client.brpc.common.annotation.ShenyuBrpcService;
+import org.apache.shenyu.examples.brpc.api.entity.BigObject;
+import org.apache.shenyu.examples.brpc.api.entity.ComplexObjects;
+import org.apache.shenyu.examples.brpc.api.entity.User;
+import org.apache.shenyu.examples.brpc.api.service.BrpcTestService;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * Brpc test service.
+ */
+@ShenyuBrpcService
+public class BrpcTestServiceImpl implements BrpcTestService {
+
+    private static final Integer SIZE = 1024 * 1024;
+
+    @Override
+    @ShenyuBrpcClient("/getArray")
+    public String[] getArray(final String[] param) {
+        return param;
+    }
+
+    @Override
+    @ShenyuBrpcClient("/getUsersList0")
+    public List<User> getUsersList0(final List<User> inputUsers) {
+        if (CollectionUtils.isNotEmpty(inputUsers)) {
+            return inputUsers;
+        } else {
+            return Collections.emptyList();
+        }
+    }
+
+    @Override
+    @ShenyuBrpcClient("/getUserByObj")
+    public User getUserByObj(final User inputUser) {
+        return Optional.ofNullable(inputUser).orElse(new User());
+    }
+
+    @Override
+    @ShenyuBrpcClient("/getUsersList1")
+    public List<User> getUsersList1(final List<User> inputUsers, final String[] param) {
+        if (CollectionUtils.isNotEmpty(inputUsers)) {
+            return inputUsers;
+        } else {
+            return Collections.emptyList();
+        }
+    }
+
+    @Override
+    @ShenyuBrpcClient("/bigObject0")
+    public BigObject bigObject0() {
+        BigObject bigObject = new BigObject();
+        bigObject.setId(1);
+        String[] obj = new String[SIZE];
+        for (int i = 0; i < SIZE; i++) {
+            obj[i] = String.valueOf(i);
+        }
+        bigObject.setObj(obj);
+        return bigObject;
+    }
+
+    @Override
+    @ShenyuBrpcClient("/complexObjects")
+    public ComplexObjects complexObjects(final User user) {
+        User newUser = Optional.ofNullable(user).orElse(new User());
+        newUser.setUserId(1L);
+        newUser.setUserName("new user");
+        ComplexObjects complexObjects = new ComplexObjects();
+        complexObjects.setUser(user);
+        BigObject bigObject = new BigObject();
+        bigObject.setId(1);
+        String[] obj = new String[SIZE];
+        for (int i = 0; i < SIZE; i++) {
+            obj[i] = String.valueOf(i);
+        }
+        bigObject.setObj(obj);
+        complexObjects.setBigObject(bigObject);
+        return complexObjects;
+    }
+
+}
diff --git a/shenyu-plugin/shenyu-plugin-brpc/src/main/java/org/apache/shenyu/plugin/brpc/cache/ApplicationConfigCache.java b/shenyu-plugin/shenyu-plugin-brpc/src/main/java/org/apache/shenyu/plugin/brpc/cache/ApplicationConfigCache.java
index 0283c38ce..4939120b3 100644
--- a/shenyu-plugin/shenyu-plugin-brpc/src/main/java/org/apache/shenyu/plugin/brpc/cache/ApplicationConfigCache.java
+++ b/shenyu-plugin/shenyu-plugin-brpc/src/main/java/org/apache/shenyu/plugin/brpc/cache/ApplicationConfigCache.java
@@ -35,7 +35,6 @@ import org.apache.shenyu.common.dto.MetaData;
 import org.apache.shenyu.common.dto.convert.plugin.BrpcRegisterConfig;
 import org.apache.shenyu.common.utils.GsonUtils;
 import org.apache.shenyu.plugin.brpc.exception.ShenyuBrpcPluginException;
-import org.apache.shenyu.plugin.brpc.util.ProxyInfoUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.lang.NonNull;
@@ -165,11 +164,11 @@ public final class ApplicationConfigCache {
         brpcParamExtInfo.getMethodInfo().forEach(methodInfo -> {
             if (CollectionUtils.isNotEmpty(methodInfo.getParamTypes())) {
                 try {
-                    Class<?>[] paramTypes = new Class[methodInfo.getParamTypes().size()];
+                    String[] paramTypes = new String[methodInfo.getParamTypes().size()];
                     String[] paramNames = new String[methodInfo.getParamTypes().size()];
                     for (int i = 0; i < methodInfo.getParamTypes().size(); i++) {
                         Pair<String, String> pair = methodInfo.getParamTypes().get(i);
-                        paramTypes[i] = ProxyInfoUtil.getParamClass(pair.getKey());
+                        paramTypes[i] = pair.getLeft();
                         paramNames[i] = pair.getValue();
                         PARAM_MAP.put(methodInfo.getMethodName(), new BrpcParamInfo(paramTypes, paramNames));
                     }
@@ -373,21 +372,21 @@ public final class ApplicationConfigCache {
      */
     public static class BrpcParamInfo {
 
-        private Class<?>[] paramTypes;
+        private String[] paramTypes;
 
         private String[] paramNames;
 
-        BrpcParamInfo(final Class<?>[] paramTypes, final String[] paramNames) {
+        BrpcParamInfo(final String[] paramTypes, final String[] paramNames) {
             this.paramTypes = paramTypes;
             this.paramNames = paramNames;
         }
 
         /**
-         * Get param types class [ ].
+         * Get param types.
          *
-         * @return the class [ ]
+         * @return the param types []
          */
-        public Class<?>[] getParamTypes() {
+        public String[] getParamTypes() {
             return paramTypes;
         }
 
@@ -396,7 +395,7 @@ public final class ApplicationConfigCache {
          *
          * @param paramTypes the param types
          */
-        public void setParamTypes(final Class<?>[] paramTypes) {
+        public void setParamTypes(final String[] paramTypes) {
             this.paramTypes = paramTypes;
         }
 
diff --git a/shenyu-plugin/shenyu-plugin-brpc/src/main/java/org/apache/shenyu/plugin/brpc/proxy/BrpcProxyService.java b/shenyu-plugin/shenyu-plugin-brpc/src/main/java/org/apache/shenyu/plugin/brpc/proxy/BrpcProxyService.java
index 4fbe7b997..88b7c0e80 100644
--- a/shenyu-plugin/shenyu-plugin-brpc/src/main/java/org/apache/shenyu/plugin/brpc/proxy/BrpcProxyService.java
+++ b/shenyu-plugin/shenyu-plugin-brpc/src/main/java/org/apache/shenyu/plugin/brpc/proxy/BrpcProxyService.java
@@ -79,7 +79,7 @@ public class BrpcProxyService {
             params = new Object[num];
             Map<String, Object> bodyMap = GsonUtils.getInstance().convertToMap(body);
             for (int i = 0; i < num; i++) {
-                params[i] = bodyMap.get(brpcParamInfo.getParamNames()[i]).toString();
+                params[i] = bodyMap.get(brpcParamInfo.getParamNames()[i]);
             }
         }
         initThreadPool();
diff --git a/shenyu-plugin/shenyu-plugin-brpc/src/test/java/org/apache/shenyu/plugin/brpc/cache/ApplicationConfigCacheTest.java b/shenyu-plugin/shenyu-plugin-brpc/src/test/java/org/apache/shenyu/plugin/brpc/cache/ApplicationConfigCacheTest.java
index 5cde44667..f215a7857 100644
--- a/shenyu-plugin/shenyu-plugin-brpc/src/test/java/org/apache/shenyu/plugin/brpc/cache/ApplicationConfigCacheTest.java
+++ b/shenyu-plugin/shenyu-plugin-brpc/src/test/java/org/apache/shenyu/plugin/brpc/cache/ApplicationConfigCacheTest.java
@@ -64,9 +64,9 @@ public final class ApplicationConfigCacheTest {
     public void testBrpcParamInfo() {
         ApplicationConfigCache.BrpcParamInfo paramInfo = new ApplicationConfigCache.BrpcParamInfo(null, null);
         paramInfo.setParamNames(new String[]{"test"});
-        paramInfo.setParamTypes(new Class<?>[]{ApplicationConfigCache.class});
+        paramInfo.setParamTypes(new String[]{"org.apache.shenyu.plugin.brpc.cache.ApplicationConfigCache"});
         Assertions.assertEquals(paramInfo.getParamNames()[0], "test");
-        Assertions.assertEquals(paramInfo.getParamTypes()[0], ApplicationConfigCache.class);
+        Assertions.assertEquals(paramInfo.getParamTypes()[0], "org.apache.shenyu.plugin.brpc.cache.ApplicationConfigCache");
     }
 
     @Test