소스 검색

1.gateway 不再依赖 log 模块
2.gateway 引入 amqp 依赖

reghao 9 달 전
부모
커밋
7ae6da5217

+ 6 - 5
gateway/pom.xml

@@ -17,11 +17,6 @@
     </properties>
 
     <dependencies>
-        <dependency>
-            <groupId>cn.reghao.tnb</groupId>
-            <artifactId>log</artifactId>
-            <version>1.0.0-SNAPSHOT</version>
-        </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-webflux</artifactId>
@@ -36,6 +31,12 @@
             <artifactId>spring-boot-starter-data-redis</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-amqp</artifactId>
+            <version>2.0.3.RELEASE</version>
+        </dependency>
+
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

+ 0 - 20
gateway/src/main/java/cn/reghao/tnb/gateway/config/LogProperties.java

@@ -1,20 +0,0 @@
-package cn.reghao.tnb.gateway.config;
-
-import lombok.Getter;
-import lombok.Setter;
-import org.springframework.boot.context.properties.ConfigurationProperties;
-import org.springframework.context.annotation.Configuration;
-
-/**
- * @author reghao
- * @date 2024-11-13 15:09:54
- */
-@Configuration
-@ConfigurationProperties(prefix = "log")
-@Setter
-@Getter
-public class LogProperties {
-    private String wsUrl;
-    private String token;
-    private Boolean enabled;
-}

+ 0 - 30
gateway/src/main/java/cn/reghao/tnb/gateway/config/SpringLifecycle.java

@@ -1,18 +1,11 @@
 package cn.reghao.tnb.gateway.config;
 
-import cn.reghao.jutil.jdk.machine.id.MachineId;
-import cn.reghao.jutil.jdk.machine.id.MachineIdLinux;
-import cn.reghao.tnb.log.Appenders;
-import cn.reghao.tnb.log.LoggerConfig;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.DisposableBean;
 import org.springframework.boot.ApplicationArguments;
 import org.springframework.boot.ApplicationRunner;
-import org.springframework.core.env.Environment;
 import org.springframework.stereotype.Component;
 
-import java.util.List;
-
 /**
  * @author reghao
  * @date 2022-03-28 11:54:49
@@ -20,34 +13,11 @@ import java.util.List;
 @Slf4j
 @Component
 public class SpringLifecycle implements ApplicationRunner, DisposableBean {
-    private final Environment env;
-    private final MachineId machineId;
-    private final LogProperties logProperties;
-
-    public SpringLifecycle(Environment env, LogProperties logProperties) {
-        this.env = env;
-        this.machineId = new MachineIdLinux();
-        this.logProperties = logProperties;
-    }
-
     @Override
     public void run(ApplicationArguments args) {
-        if (logProperties.getEnabled()) {
-            initLogConfig();
-        }
         log.info("gateway 启动...");
     }
 
-    private void initLogConfig() {
-        String wsUrl = logProperties.getWsUrl();
-        if (wsUrl != null) {
-            String token = logProperties.getToken();
-            String app = env.getProperty("spring.application.name");
-            String host = machineId.ipv4();
-            String url = String.format("%s?token=%s&app=%s&host=%s", wsUrl, token, app, host);
-            LoggerConfig.initLogger(List.of(Appenders.wsAppender(url, app, host)));
-        }
-    }
 
     @Override
     public void destroy() {

+ 8 - 2
gateway/src/main/java/cn/reghao/tnb/gateway/log/GatewayLogService.java

@@ -2,6 +2,7 @@ package cn.reghao.tnb.gateway.log;
 
 import cn.reghao.jutil.jdk.converter.DateTimeConverter;
 import cn.reghao.tnb.gateway.log.model.GatewayLog;
+import cn.reghao.tnb.gateway.mq.RabbitProducer;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.cloud.gateway.route.Route;
 import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
@@ -28,8 +29,14 @@ import java.util.Objects;
  */
 @Service
 public class GatewayLogService {
+    private final RabbitProducer rabbitProducer;
+
+    public GatewayLogService(RabbitProducer rabbitProducer) {
+        this.rabbitProducer = rabbitProducer;
+    }
+
     public void saveGatewayLog(GatewayLog gatewayLog) {
-        //gatewayLogRepository.insert(gatewayLog).subscribe();
+        //rabbitProducer.sendGatewayLog(gatewayLog);
     }
 
     public void setGatewayLog(ServerWebExchange exchange) {
@@ -51,7 +58,6 @@ public class GatewayLogService {
         // 计算执行时间
         long executeTime = Duration.between(gatewayLog.getRequestTime(), responseTime).toMillis();
         gatewayLog.setExecuteTime(executeTime);
-
         saveGatewayLog(gatewayLog);
     }
 

+ 34 - 0
gateway/src/main/java/cn/reghao/tnb/gateway/mq/RabbitProducer.java

@@ -0,0 +1,34 @@
+package cn.reghao.tnb.gateway.mq;
+
+import cn.reghao.jutil.jdk.serializer.JsonConverter;
+import cn.reghao.tnb.gateway.log.model.GatewayLog;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author reghao
+ * @date 2024-04-19 14:38:30
+ */
+@Component
+public class RabbitProducer {
+    private final RabbitTemplate rabbitTemplate;
+
+    public RabbitProducer(RabbitTemplate rabbitTemplate) {
+        this.rabbitTemplate = rabbitTemplate;
+    }
+
+    public void sendChatMessage(String msg) {
+        String exchange = exchange = "amq.fanout";
+        String routingKey = "tnb.message.chat.queue";
+        routingKey = "";
+        //如果交换机名称写错了。那么confirmCallback 回调失败的。
+        //如果设置routingkey 是一个错误,那么returncallback 会进行回调调用
+        rabbitTemplate.convertAndSend(exchange, routingKey, msg);
+    }
+
+    public void sendGatewayLog(GatewayLog gatewayLog) {
+        String routingKey = "tnb.gateway.log";
+        String jsonPayload = JsonConverter.objectToJson(gatewayLog);
+        rabbitTemplate.convertAndSend(routingKey, jsonPayload);
+    }
+}

+ 6 - 4
gateway/src/main/resources/application-dev.yml

@@ -1,13 +1,15 @@
-log:
-  enabled: false
-  ws-url: ws://bnt.reghao.cn/ws/log/push
-  token: 012345678
 spring:
   redis:
     database: 0
     host: localhost
     port: 6379
     password: Dev@123456
+  rabbitmq:
+    host: localhost
+    port: 5672
+    virtual-host: /
+    username: dev
+    password: Dev@123456
   cloud:
     discovery:
       enabled: true

+ 6 - 4
gateway/src/main/resources/application-test.yml

@@ -1,13 +1,15 @@
-log:
-  enabled: false
-  ws-url: ws://bnt.reghao.cn/ws/log/push
-  token: 012345678
 spring:
   redis:
     database: 0
     host: 192.168.0.209
     port: 6379
     password: Test@123456
+  rabbitmq:
+    host: 192.168.0.209
+    port: 5672
+    virtual-host: /
+    username: test
+    password: Test@123456
   cloud:
     discovery:
       enabled: true