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/11 12:38:47 UTC
[incubator-shenyu] branch master updated: add: AOP for grpc-example (#3532)
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 c6bcaaaf3 add: AOP for grpc-example (#3532)
c6bcaaaf3 is described below
commit c6bcaaaf334673af3491562224fc7bc9d5cab43a
Author: SongTao Zhuang <51...@users.noreply.github.com>
AuthorDate: Sat Jun 11 20:38:43 2022 +0800
add: AOP for grpc-example (#3532)
---
shenyu-examples/shenyu-examples-grpc/pom.xml | 5 +++
.../org/apache/shenyu/examples/grpc/aop/Log.java | 26 ++++++++++++
.../shenyu/examples/grpc/aop/LogInterceptor.java | 48 ++++++++++++++++++++++
.../shenyu/examples/grpc/echo/EchoServiceImpl.java | 2 +
4 files changed, 81 insertions(+)
diff --git a/shenyu-examples/shenyu-examples-grpc/pom.xml b/shenyu-examples/shenyu-examples-grpc/pom.xml
index 05b80afd7..14d3867e7 100644
--- a/shenyu-examples/shenyu-examples-grpc/pom.xml
+++ b/shenyu-examples/shenyu-examples-grpc/pom.xml
@@ -44,6 +44,11 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-aop</artifactId>
+ </dependency>
+
<dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-spring-boot-starter-client-grpc</artifactId>
diff --git a/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/aop/Log.java b/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/aop/Log.java
new file mode 100644
index 000000000..920f4ecc3
--- /dev/null
+++ b/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/aop/Log.java
@@ -0,0 +1,26 @@
+/*
+ * 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.grpc.aop;
+
+import java.lang.annotation.*;
+
+@Target({ElementType.TYPE, ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface Log {
+}
diff --git a/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/aop/LogInterceptor.java b/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/aop/LogInterceptor.java
new file mode 100644
index 000000000..66f50903d
--- /dev/null
+++ b/shenyu-examples/shenyu-examples-grpc/src/main/java/org/apache/shenyu/examples/grpc/aop/LogInterceptor.java
@@ -0,0 +1,48 @@
+/*
+ * 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.grpc.aop;
+
+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;
+
+@Aspect
+@Component
+public class LogInterceptor {
+
+ private static final Logger log = LoggerFactory.getLogger(LogInterceptor.class);
+
+ @Pointcut("@annotation(org.apache.shenyu.examples.grpc.aop.Log)")
+ public void logPointcut() {
+ // just for pointcut
+ }
+
+ @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-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 7e85f6439..4bd68a779 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
@@ -27,6 +27,7 @@ import java.net.InetAddress;
import java.net.UnknownHostException;
import org.apache.shenyu.client.grpc.common.annotation.ShenyuGrpcClient;
+import org.apache.shenyu.examples.grpc.aop.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@@ -37,6 +38,7 @@ public class EchoServiceImpl extends EchoServiceGrpc.EchoServiceImplBase {
private static final Logger LOG = LoggerFactory.getLogger(EchoServiceImpl.class);
@Override
+ @Log
@ShenyuGrpcClient("/echo")
public void echo(final EchoRequest request, final StreamObserver<EchoResponse> responseObserver) {
LOG.info("Received: {}", request.getMessage());