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/10 07:55:06 UTC

[incubator-shenyu] branch master updated: [ISSUE #3485] update ShenyuGrpcClient (#3505)

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 34790a78e [ISSUE #3485] update ShenyuGrpcClient (#3505)
34790a78e is described below

commit 34790a78e40ca47c4fa600d91c23dc238a2858f3
Author: weihubeats <we...@163.com>
AuthorDate: Fri Jun 10 15:54:57 2022 +0800

    [ISSUE #3485] update ShenyuGrpcClient (#3505)
    
    * update ShenyuGrpcClient
    
    * update ShenyuGrpcClient
    
    * update ShenyuGrpcClient
    
    * add java doc
    
    * If the annotation on the method is empty, get the annotation on the class
    
    * get aop class
---
 .../client/grpc/GrpcClientBeanPostProcessor.java     | 20 ++++++++++++--------
 .../grpc/common/annotation/ShenyuGrpcClient.java     | 13 ++++++++++++-
 .../shenyu/examples/grpc/demo/EventServiceImpl.java  |  6 +++---
 .../shenyu/examples/grpc/demo/HelloServiceImpl.java  |  2 +-
 .../shenyu/examples/grpc/echo/EchoServiceImpl.java   |  2 +-
 .../examples/grpc/stream/StreamServiceImpl.java      |  8 ++++----
 6 files changed, 33 insertions(+), 18 deletions(-)

diff --git a/shenyu-client/shenyu-client-grpc/src/main/java/org/apache/shenyu/client/grpc/GrpcClientBeanPostProcessor.java b/shenyu-client/shenyu-client-grpc/src/main/java/org/apache/shenyu/client/grpc/GrpcClientBeanPostProcessor.java
index cb21929ba..893337f71 100644
--- a/shenyu-client/shenyu-client-grpc/src/main/java/org/apache/shenyu/client/grpc/GrpcClientBeanPostProcessor.java
+++ b/shenyu-client/shenyu-client-grpc/src/main/java/org/apache/shenyu/client/grpc/GrpcClientBeanPostProcessor.java
@@ -36,9 +36,10 @@ import org.apache.shenyu.register.common.config.PropertiesConfig;
 import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.aop.support.AopUtils;
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.config.BeanPostProcessor;
-import org.springframework.core.annotation.AnnotationUtils;
+import org.springframework.core.annotation.AnnotatedElementUtils;
 import org.springframework.lang.NonNull;
 import org.springframework.util.ReflectionUtils;
 
@@ -109,6 +110,9 @@ public class GrpcClientBeanPostProcessor implements BeanPostProcessor {
             LOG.error("failed to get grpc target class", e);
             return;
         }
+        if (AopUtils.isAopProxy(serviceBean)) {
+            clazz = AopUtils.getTargetClass(serviceBean);
+        }
         Class<?> parent = clazz.getSuperclass();
         Class<?> classes = parent.getDeclaringClass();
         String packageName;
@@ -125,21 +129,21 @@ public class GrpcClientBeanPostProcessor implements BeanPostProcessor {
             LOG.error(String.format("grpc SERVICE_NAME can not found: %s", classes));
             return;
         }
-        ShenyuGrpcClient grpcClassAnnotation = AnnotationUtils.findAnnotation(clazz, ShenyuGrpcClient.class);
-        String basePath = Optional.ofNullable(grpcClassAnnotation)
-                .map(annotation -> StringUtils.defaultIfBlank(grpcClassAnnotation.path(), "")).orElse("");
+        ShenyuGrpcClient beanShenyuClient = AnnotatedElementUtils.findMergedAnnotation(clazz, ShenyuGrpcClient.class);
+        String basePath = Optional.ofNullable(beanShenyuClient)
+                .map(annotation -> StringUtils.defaultIfBlank(beanShenyuClient.path(), "")).orElse("");
         if (basePath.contains("*")) {
             Method[] methods = ReflectionUtils.getDeclaredMethods(clazz);
             for (Method method : methods) {
-                publisher.publishEvent(buildMetaDataDTO(packageName, grpcClassAnnotation, method, basePath));
+                publisher.publishEvent(buildMetaDataDTO(packageName, beanShenyuClient, method, basePath));
             }
             return;
         }
         final Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(clazz);
         for (Method method : methods) {
-            ShenyuGrpcClient grpcClient = method.getAnnotation(ShenyuGrpcClient.class);
-            if (Objects.nonNull(grpcClient)) {
-                publisher.publishEvent(buildMetaDataDTO(packageName, grpcClient, method, basePath));
+            ShenyuGrpcClient methodShenyuClient = AnnotatedElementUtils.findMergedAnnotation(method, ShenyuGrpcClient.class);
+            if (Objects.nonNull(methodShenyuClient)) {
+                publisher.publishEvent(buildMetaDataDTO(packageName, methodShenyuClient, method, basePath));
             }
         }
     }
diff --git a/shenyu-client/shenyu-client-grpc/src/main/java/org/apache/shenyu/client/grpc/common/annotation/ShenyuGrpcClient.java b/shenyu-client/shenyu-client-grpc/src/main/java/org/apache/shenyu/client/grpc/common/annotation/ShenyuGrpcClient.java
index 294422cf1..3ef88fd61 100644
--- a/shenyu-client/shenyu-client-grpc/src/main/java/org/apache/shenyu/client/grpc/common/annotation/ShenyuGrpcClient.java
+++ b/shenyu-client/shenyu-client-grpc/src/main/java/org/apache/shenyu/client/grpc/common/annotation/ShenyuGrpcClient.java
@@ -17,6 +17,8 @@
 
 package org.apache.shenyu.client.grpc.common.annotation;
 
+import org.springframework.core.annotation.AliasFor;
+
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
@@ -28,12 +30,21 @@ import java.lang.annotation.Target;
 @Retention(RetentionPolicy.RUNTIME)
 @Target({ElementType.TYPE, ElementType.METHOD})
 public @interface ShenyuGrpcClient {
+
+    /**
+     * value string.
+     * @return the string
+     */
+    @AliasFor(attribute = "path")
+    String value() default "";
+    
     /**
      * Path string.
      *
      * @return the string
      */
-    String path();
+    @AliasFor(attribute = "value")
+    String path() default "";
 
     /**
      * Rule name string.
diff --git a/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/demo/EventServiceImpl.java b/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/demo/EventServiceImpl.java
index b3694bcfe..e89798374 100644
--- a/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/demo/EventServiceImpl.java
+++ b/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/demo/EventServiceImpl.java
@@ -25,11 +25,11 @@ import io.grpc.stub.StreamObserver;
 import org.apache.shenyu.client.grpc.common.annotation.ShenyuGrpcClient;
 import org.springframework.stereotype.Service;
 
-@ShenyuGrpcClient(path = "/eventService", desc = "event")
+@ShenyuGrpcClient("/eventService")
 @Service
 public class EventServiceImpl extends EventServiceGrpc.EventServiceImplBase {
 
-    @ShenyuGrpcClient(path = "/sendEvent", desc = "sendEvent")
+    @ShenyuGrpcClient("/sendEvent")
     @Override
     public void sendEvent(EventRequest request, StreamObserver<EventResponse> responseObserver) {
         EventResponse response = EventResponse.newBuilder().setData("received event:" + request.getData()).build();
@@ -37,7 +37,7 @@ public class EventServiceImpl extends EventServiceGrpc.EventServiceImplBase {
         responseObserver.onCompleted();
     }
 
-    @ShenyuGrpcClient(path = "/sendEventStream", desc = "sendEventStream")
+    @ShenyuGrpcClient("/sendEventStream")
     @Override
     public StreamObserver<EventRequest> sendEventStream(StreamObserver<EventResponse> responseObserver) {
         return new StreamObserver<EventRequest>() {
diff --git a/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/demo/HelloServiceImpl.java b/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/demo/HelloServiceImpl.java
index 3b25b1913..aa92fa219 100644
--- a/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/demo/HelloServiceImpl.java
+++ b/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/demo/HelloServiceImpl.java
@@ -24,7 +24,7 @@ import org.apache.shenyu.client.grpc.common.annotation.ShenyuGrpcClient;
 import org.springframework.stereotype.Service;
 
 
-@ShenyuGrpcClient(path = "/helloService/**", desc = "hello")
+@ShenyuGrpcClient("/helloService/**")
 @Service
 public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {
 
diff --git a/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/echo/EchoServiceImpl.java b/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/echo/EchoServiceImpl.java
index 99a4aca56..7e85f6439 100644
--- a/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/echo/EchoServiceImpl.java
+++ b/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/echo/EchoServiceImpl.java
@@ -37,7 +37,7 @@ public class EchoServiceImpl extends EchoServiceGrpc.EchoServiceImplBase {
     private static final Logger LOG = LoggerFactory.getLogger(EchoServiceImpl.class);
 
     @Override
-    @ShenyuGrpcClient(path = "/echo", desc = "echo")
+    @ShenyuGrpcClient("/echo")
     public void echo(final EchoRequest request, final StreamObserver<EchoResponse> responseObserver) {
         LOG.info("Received: {}", request.getMessage());
         EchoResponse.Builder response = EchoResponse.newBuilder()
diff --git a/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/stream/StreamServiceImpl.java b/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/stream/StreamServiceImpl.java
index 12ec440be..f6de4c1b7 100644
--- a/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/stream/StreamServiceImpl.java
+++ b/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/stream/StreamServiceImpl.java
@@ -32,7 +32,7 @@ public class StreamServiceImpl extends StreamServiceGrpc.StreamServiceImplBase {
     private static final Logger LOG = LoggerFactory.getLogger(StreamServiceImpl.class);
     
     @Override
-    @ShenyuGrpcClient(path = "/unaryFun", desc = "unaryFun")
+    @ShenyuGrpcClient("/unaryFun")
     public void unaryFun(final RequestData request, final StreamObserver<ResponseData> responseObserver) {
         LOG.info("unaryFun received:{}", request.getText());
         
@@ -44,7 +44,7 @@ public class StreamServiceImpl extends StreamServiceGrpc.StreamServiceImplBase {
     }
     
     @Override
-    @ShenyuGrpcClient(path = "/serverStreamingFun", desc = "serverStreamingFun")
+    @ShenyuGrpcClient("/serverStreamingFun")
     public void serverStreamingFun(final RequestData request, final StreamObserver<ResponseData> responseObserver) {
         LOG.info("serverStreamingFun received:{}", request.getText());
         
@@ -59,7 +59,7 @@ public class StreamServiceImpl extends StreamServiceGrpc.StreamServiceImplBase {
     }
     
     @Override
-    @ShenyuGrpcClient(path = "/clientStreamingFun", desc = "clientStreamingFun")
+    @ShenyuGrpcClient("/clientStreamingFun")
     public StreamObserver<RequestData> clientStreamingFun(final StreamObserver<ResponseData> responseObserver) {
         
         return new StreamObserver<RequestData>() {
@@ -87,7 +87,7 @@ public class StreamServiceImpl extends StreamServiceGrpc.StreamServiceImplBase {
     }
     
     @Override
-    @ShenyuGrpcClient(path = "/bidiStreamingFun", desc = "bidiStreamingFun")
+    @ShenyuGrpcClient("/bidiStreamingFun")
     public StreamObserver<RequestData> bidiStreamingFun(final StreamObserver<ResponseData> responseObserver) {
         
         return new StreamObserver<RequestData>() {