Sfoglia il codice sorgente

异步日志通道

gaodm 3 anni fa
parent
commit
070d642450

+ 6 - 0
pom.xml

@@ -188,6 +188,12 @@
             <artifactId>commons-pool2</artifactId>
         </dependency>
 
+        <!--消息中心-->
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
+        </dependency>
+
         <!-- 文件上传相关架包 -->
         <dependency>
             <groupId>commons-fileupload</groupId>

+ 19 - 0
src/main/java/com/diagbot/annotation/CdssLog.java

@@ -0,0 +1,19 @@
+package com.diagbot.annotation;
+
+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;
+
+/**
+ * @Description: 需要二次Token验证注解
+ * @author: gaodm
+ * @time: 2020/7/29 9:23
+ */
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface CdssLog {
+    String value() default "";
+}

+ 1 - 0
src/main/java/com/diagbot/config/ResourceServerConfigurer.java

@@ -133,6 +133,7 @@ public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
                 .antMatchers("/cache/clearRuleAll").permitAll()
                 .antMatchers("/term/termMatching").permitAll()
                 .antMatchers("/sys/mr/testIndication").permitAll()
+                .antMatchers("/test/logTest").permitAll()
                 .antMatchers("/**").authenticated();
         //                .antMatchers("/**").permitAll();
     }

+ 1 - 0
src/main/java/com/diagbot/config/security/UrlAccessDecisionManager.java

@@ -175,6 +175,7 @@ public class UrlAccessDecisionManager implements AccessDecisionManager {
                 || matchers("/cache/clearRuleAll", request)
                 || matchers("/term/termMatching", request)
                 || matchers("/sys/mr/testIndication", request)
+                || matchers("/test/logTest", request)
                 || matchers("/", request)) {
             return true;
         }

+ 23 - 0
src/main/java/com/diagbot/rabbit/MyProcessor.java

@@ -0,0 +1,23 @@
+package com.diagbot.rabbit;
+
+import org.springframework.cloud.stream.annotation.Input;
+import org.springframework.cloud.stream.annotation.Output;
+import org.springframework.messaging.MessageChannel;
+import org.springframework.messaging.SubscribableChannel;
+
+/**
+ * @Description: 自定义Stream发布和消费对象
+ * @author: gaodm
+ * @time: 2018/8/29 13:39
+ */
+public interface MyProcessor {
+
+    String INPUT_CdssLog = "inputCdssLog";
+    String OUTPUT_CdssLog= "outputCdssLog";
+
+    @Input(INPUT_CdssLog)
+    SubscribableChannel inputCdssLog();
+    @Output(OUTPUT_CdssLog)
+    MessageChannel outputCdssLog();
+
+}

+ 18 - 0
src/main/java/com/diagbot/rabbit/MyReceiver.java

@@ -0,0 +1,18 @@
+package com.diagbot.rabbit;
+
+import org.springframework.cloud.stream.annotation.EnableBinding;
+import org.springframework.cloud.stream.annotation.StreamListener;
+
+/**
+ * @Description: 消费者
+ * @author: wangfeng
+ * @time: 2018/8/29 14:02
+ */
+@EnableBinding({ MyProcessor.class })
+public class MyReceiver {
+
+    @StreamListener(MyProcessor.INPUT_CdssLog)
+    public void inputCdssLog(String message) {
+        System.out.println("Received <" + "接受到的信息" + ">" + message);
+    }
+}

+ 27 - 0
src/main/java/com/diagbot/rabbit/MySender.java

@@ -0,0 +1,27 @@
+package com.diagbot.rabbit;
+
+import com.diagbot.util.FastJsonUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.cloud.stream.annotation.EnableBinding;
+import org.springframework.integration.support.MessageBuilder;
+import org.springframework.messaging.MessageChannel;
+import org.springframework.stereotype.Component;
+
+/**
+ * @Description: 发布者
+ * @author: gaodm
+ * @time: 2018/8/29 13:41
+ */
+@Component
+@EnableBinding({ MyProcessor.class })
+public class MySender {
+
+    @Autowired
+    @Qualifier("outputCdssLog")
+    MessageChannel outputCdssLog;
+
+    public void outputCdssLogSend(String msg) {
+        outputCdssLog.send(MessageBuilder.withPayload(FastJsonUtils.getBeanToJson(msg)).build());
+    }
+}

+ 37 - 0
src/main/java/com/diagbot/web/LogTestController.java

@@ -0,0 +1,37 @@
+package com.diagbot.web;
+
+import com.diagbot.annotation.SysLogger;
+import com.diagbot.dto.RespDTO;
+import com.diagbot.rabbit.MySender;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @Description: 站内信控制层
+ * @author: gaodm
+ * @time: 2018/8/30 10:12
+ */
+@RestController
+@Api(value = "日志测试API", tags = { "日志测试API" })
+@RequestMapping("/test")
+@SuppressWarnings("unchecked")
+public class LogTestController {
+
+    @Autowired
+    private MySender mySender;
+
+
+    @ApiOperation(value = "异步日志测试[by:gaodm]", notes = "异步日志测试")
+    @PostMapping("/logTest")
+    @SysLogger("logTest")
+    public RespDTO<Boolean> logTest(@RequestBody String msg) {
+        mySender.outputCdssLogSend(msg);
+        return RespDTO.onSuc(true);
+    }
+}
+

+ 18 - 0
src/main/resources/application-dev.yml

@@ -106,6 +106,24 @@ spring:
     date-format: yyyy-MM-dd HH:mm:ss
     time-zone: GMT+8
 
+  cloud:
+    stream:
+      bindings:
+        outputCdssLog:
+          destination: cdssLog
+        inputCdssLog:
+          destination: cdssLog
+          group: cdssLogReceiveGroup
+
+  #mq
+  rabbitmq:
+    host: 192.168.2.236
+    port: 5672
+    username: lantone
+    password: lantone
+    publisher-confirms: true
+    virtual-host: /
+
   #redis
   redis:
     database:

+ 18 - 0
src/main/resources/application-local.yml

@@ -106,6 +106,24 @@ spring:
     date-format: yyyy-MM-dd HH:mm:ss
     time-zone: GMT+8
 
+  cloud:
+    stream:
+      bindings:
+        outputCdssLog:
+          destination: cdssLog
+        inputCdssLog:
+          destination: cdssLog
+          group: cdssLogReceiveGroup
+
+  #mq
+  rabbitmq:
+    host: localhost
+    port: 5672
+    username: guest
+    password: guest
+    publisher-confirms: true
+    virtual-host: /
+
   #redis
   redis:
     database:

+ 19 - 0
src/main/resources/application-pre.yml

@@ -106,6 +106,25 @@ spring:
     date-format: yyyy-MM-dd HH:mm:ss
     time-zone: GMT+8
 
+  cloud:
+    stream:
+      bindings:
+        outputCdssLog:
+          destination: cdssLog
+        inputCdssLog:
+          destination: cdssLog
+          group: cdssLogReceiveGroup
+
+  #mq
+  rabbitmq:
+    host: 192.168.2.121
+    port: 5672
+    username: lantone
+    password: lantone
+    publisher-confirms: true
+    virtual-host: /
+
+
   #redis
   redis:
     database:

+ 18 - 0
src/main/resources/application-pro.yml

@@ -106,6 +106,24 @@ spring:
     date-format: yyyy-MM-dd HH:mm:ss
     time-zone: GMT+8
 
+  cloud:
+    stream:
+      bindings:
+        outputCdssLog:
+          destination: cdssLog
+        inputCdssLog:
+          destination: cdssLog
+          group: cdssLogReceiveGroup
+
+  #mq
+  rabbitmq:
+    host: 192.168.2.122
+    port: 5672
+    username: lantone
+    password: lantone
+    publisher-confirms: true
+    virtual-host: /
+
   #redis
   redis:
     database:

+ 18 - 0
src/main/resources/application-test.yml

@@ -106,6 +106,24 @@ spring:
     date-format: yyyy-MM-dd HH:mm:ss
     time-zone: GMT+8
 
+  cloud:
+    stream:
+      bindings:
+        outputCdssLog:
+          destination: cdssLog
+        inputCdssLog:
+          destination: cdssLog
+          group: cdssLogReceiveGroup
+
+  #mq
+  rabbitmq:
+    host: 192.168.2.241
+    port: 5672
+    username: lantone
+    password: lantone
+    publisher-confirms: true
+    virtual-host: /
+
   #redis
   redis:
     database: