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/17 17:56:38 UTC

[incubator-shenyu] branch master updated: [ISSUE #3523]Add AOP for shenyu-client-websocket. (#3576)

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 8c097ea98 [ISSUE #3523]Add AOP for shenyu-client-websocket. (#3576)
8c097ea98 is described below

commit 8c097ea98d31c864ca887067ae93f81e40ef4e0a
Author: Zihao Huang <hz...@gmail.com>
AuthorDate: Sat Jun 18 01:56:33 2022 +0800

    [ISSUE #3523]Add AOP for shenyu-client-websocket. (#3576)
---
 .../shenyu-example-spring-native-websocket/pom.xml |  4 +++
 .../apache/shenyu/examples/websocket/aop/Log.java} | 30 ++++++----------
 .../examples/websocket/aop/LogInterceptor.java}    | 40 +++++++++++++---------
 .../websocket/controller/TestHttpController.java   |  2 ++
 .../pom.xml                                        |  5 +++
 .../{handler/EchoHandler.java => aop/Log.java}     | 30 ++++++----------
 .../EchoHandler.java => aop/LogInterceptor.java}   | 40 +++++++++++++---------
 .../examples/websocket/handler/EchoHandler.java    |  2 ++
 8 files changed, 81 insertions(+), 72 deletions(-)

diff --git a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/pom.xml b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/pom.xml
index de7100bb4..be2d201b6 100644
--- a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/pom.xml
+++ b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/pom.xml
@@ -48,6 +48,10 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-websocket</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-aop</artifactId>
+        </dependency>
 
         <dependency>
             <groupId>org.projectlombok</groupId>
diff --git a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/src/main/java/org/apache/shenyu/examples/websocket/aop/Log.java
similarity index 52%
copy from shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java
copy to shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/src/main/java/org/apache/shenyu/examples/websocket/aop/Log.java
index a9dd557bb..e90533302 100644
--- a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java
+++ b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/src/main/java/org/apache/shenyu/examples/websocket/aop/Log.java
@@ -15,26 +15,16 @@
  * limitations under the License.
  */
 
-package org.apache.shenyu.examples.websocket.handler;
+package org.apache.shenyu.examples.websocket.aop;
 
-import org.springframework.lang.NonNull;
-import org.springframework.stereotype.Component;
-import org.springframework.web.reactive.socket.WebSocketHandler;
-import org.springframework.web.reactive.socket.WebSocketSession;
-import reactor.core.publisher.Mono;
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
 
-/**
- * The type Echo handler.
- */
-@Component
-public class EchoHandler implements WebSocketHandler {
-
-    @Override
-    @NonNull
-    public Mono<Void> handle(final WebSocketSession session) {
-        return session.send(
-                session.receive()
-                        .map(msg -> session.textMessage(
-                                "result apache shenyu : -> " + msg.getPayloadAsText())));
-    }
+@Target({ElementType.TYPE, ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface Log {
 }
diff --git a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/src/main/java/org/apache/shenyu/examples/websocket/aop/LogInterceptor.java
similarity index 51%
copy from shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java
copy to shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/src/main/java/org/apache/shenyu/examples/websocket/aop/LogInterceptor.java
index a9dd557bb..3a0af1bce 100644
--- a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java
+++ b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/src/main/java/org/apache/shenyu/examples/websocket/aop/LogInterceptor.java
@@ -15,26 +15,34 @@
  * limitations under the License.
  */
 
-package org.apache.shenyu.examples.websocket.handler;
+package org.apache.shenyu.examples.websocket.aop;
 
-import org.springframework.lang.NonNull;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
-import org.springframework.web.reactive.socket.WebSocketHandler;
-import org.springframework.web.reactive.socket.WebSocketSession;
-import reactor.core.publisher.Mono;
 
-/**
- * The type Echo handler.
- */
+@Aspect
 @Component
-public class EchoHandler implements WebSocketHandler {
+public class LogInterceptor {
+
+    private static final Logger log = LoggerFactory.getLogger(LogInterceptor.class);
+
+    @Pointcut("@annotation(org.apache.shenyu.examples.websocket.aop.Log)")
+    public void logPointcut() {
+        //just for pointcut
+    }
 
-    @Override
-    @NonNull
-    public Mono<Void> handle(final WebSocketSession session) {
-        return session.send(
-                session.receive()
-                        .map(msg -> session.textMessage(
-                                "result apache shenyu : -> " + msg.getPayloadAsText())));
+    @Around("logPointcut()")
+    public Object around(ProceedingJoinPoint point) throws Throwable {
+        try {
+            log.info("before proceed");
+            return point.proceed(point.getArgs());
+        } finally {
+            log.info("after proceed");
+        }
     }
 }
diff --git a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/src/main/java/org/apache/shenyu/examples/websocket/controller/TestHttpController.java b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/src/main/java/org/apache/shenyu/examples/websocket/controller/TestHttpController.java
index 4163fd998..aef2dbe9c 100644
--- a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/src/main/java/org/apache/shenyu/examples/websocket/controller/TestHttpController.java
+++ b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-native-websocket/src/main/java/org/apache/shenyu/examples/websocket/controller/TestHttpController.java
@@ -18,6 +18,7 @@
 package org.apache.shenyu.examples.websocket.controller;
 
 import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
+import org.apache.shenyu.examples.websocket.aop.Log;
 import org.apache.shenyu.examples.websocket.config.WsSessionManager;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -36,6 +37,7 @@ import java.io.IOException;
 public class TestHttpController {
 
     @RequestMapping("/sendMsg")
+    @Log
     public @ResponseBody
     Object sendMsg(String token, String msg) throws IOException {
         WebSocketSession webSocketSession = WsSessionManager.get(token);
diff --git a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/pom.xml b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/pom.xml
index fbdbaa837..4fabc47ee 100644
--- a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/pom.xml
+++ b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/pom.xml
@@ -61,6 +61,11 @@
             <artifactId>spring-boot-starter-actuator</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-aop</artifactId>
+        </dependency>
+
         <dependency>
             <groupId>io.netty</groupId>
             <artifactId>netty-all</artifactId>
diff --git a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/aop/Log.java
similarity index 52%
copy from shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java
copy to shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/aop/Log.java
index a9dd557bb..e90533302 100644
--- a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java
+++ b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/aop/Log.java
@@ -15,26 +15,16 @@
  * limitations under the License.
  */
 
-package org.apache.shenyu.examples.websocket.handler;
+package org.apache.shenyu.examples.websocket.aop;
 
-import org.springframework.lang.NonNull;
-import org.springframework.stereotype.Component;
-import org.springframework.web.reactive.socket.WebSocketHandler;
-import org.springframework.web.reactive.socket.WebSocketSession;
-import reactor.core.publisher.Mono;
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
 
-/**
- * The type Echo handler.
- */
-@Component
-public class EchoHandler implements WebSocketHandler {
-
-    @Override
-    @NonNull
-    public Mono<Void> handle(final WebSocketSession session) {
-        return session.send(
-                session.receive()
-                        .map(msg -> session.textMessage(
-                                "result apache shenyu : -> " + msg.getPayloadAsText())));
-    }
+@Target({ElementType.TYPE, ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface Log {
 }
diff --git a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/aop/LogInterceptor.java
similarity index 51%
copy from shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java
copy to shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/aop/LogInterceptor.java
index a9dd557bb..3a0af1bce 100644
--- a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java
+++ b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/aop/LogInterceptor.java
@@ -15,26 +15,34 @@
  * limitations under the License.
  */
 
-package org.apache.shenyu.examples.websocket.handler;
+package org.apache.shenyu.examples.websocket.aop;
 
-import org.springframework.lang.NonNull;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
-import org.springframework.web.reactive.socket.WebSocketHandler;
-import org.springframework.web.reactive.socket.WebSocketSession;
-import reactor.core.publisher.Mono;
 
-/**
- * The type Echo handler.
- */
+@Aspect
 @Component
-public class EchoHandler implements WebSocketHandler {
+public class LogInterceptor {
+
+    private static final Logger log = LoggerFactory.getLogger(LogInterceptor.class);
+
+    @Pointcut("@annotation(org.apache.shenyu.examples.websocket.aop.Log)")
+    public void logPointcut() {
+        //just for pointcut
+    }
 
-    @Override
-    @NonNull
-    public Mono<Void> handle(final WebSocketSession session) {
-        return session.send(
-                session.receive()
-                        .map(msg -> session.textMessage(
-                                "result apache shenyu : -> " + msg.getPayloadAsText())));
+    @Around("logPointcut()")
+    public Object around(ProceedingJoinPoint point) throws Throwable {
+        try {
+            log.info("before proceed");
+            return point.proceed(point.getArgs());
+        } finally {
+            log.info("after proceed");
+        }
     }
 }
diff --git a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java
index a9dd557bb..263718e01 100644
--- a/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java
+++ b/shenyu-examples/shenyu-examples-websocket/shenyu-example-spring-reactive-websocket/src/main/java/org/apache/shenyu/examples/websocket/handler/EchoHandler.java
@@ -17,6 +17,7 @@
 
 package org.apache.shenyu.examples.websocket.handler;
 
+import org.apache.shenyu.examples.websocket.aop.Log;
 import org.springframework.lang.NonNull;
 import org.springframework.stereotype.Component;
 import org.springframework.web.reactive.socket.WebSocketHandler;
@@ -31,6 +32,7 @@ public class EchoHandler implements WebSocketHandler {
 
     @Override
     @NonNull
+    @Log
     public Mono<Void> handle(final WebSocketSession session) {
         return session.send(
                 session.receive()