瀏覽代碼

添加 WsAppender 将运行时日志通过 websocket 存储到 db

reghao 1 年之前
父節點
當前提交
227fcbb832
共有 39 個文件被更改,包括 399 次插入52 次删除
  1. 1 2
      README.md
  2. 6 0
      account/account-service/pom.xml
  3. 20 0
      account/account-service/src/main/java/cn/reghao/tnb/account/app/config/LogProperties.java
  4. 0 31
      account/account-service/src/main/java/cn/reghao/tnb/account/app/config/spring/AppLifecycle.java
  5. 59 0
      account/account-service/src/main/java/cn/reghao/tnb/account/app/config/spring/SpringLifecycle.java
  6. 2 1
      account/account-service/src/main/java/cn/reghao/tnb/account/app/service/CodeService.java
  7. 0 1
      account/account-service/src/main/java/cn/reghao/tnb/account/app/service/impl/AccountTokenServiceImpl.java
  8. 4 0
      account/account-service/src/main/resources/application-dev.yml
  9. 4 0
      account/account-service/src/main/resources/application-test.yml
  10. 1 0
      common/src/main/java/cn/reghao/tnb/common/prop/LogProperties.java
  11. 3 1
      content/content-service/src/main/java/cn/reghao/tnb/content/app/config/SpringLifecycle.java
  12. 4 0
      content/content-service/src/main/resources/application-dev.yml
  13. 4 0
      content/content-service/src/main/resources/application-test.yml
  14. 5 0
      data/data-service/pom.xml
  15. 20 0
      data/data-service/src/main/java/cn/reghao/tnb/data/app/config/LogProperties.java
  16. 56 0
      data/data-service/src/main/java/cn/reghao/tnb/data/app/config/SpringLifecycle.java
  17. 4 0
      data/data-service/src/main/resources/application-dev.yml
  18. 4 0
      data/data-service/src/main/resources/application-test.yml
  19. 1 1
      data/data-service/src/main/resources/application.yml
  20. 5 0
      file/file-service/pom.xml
  21. 32 3
      file/file-service/src/main/java/cn/reghao/tnb/file/app/config/SpringLifecycle.java
  22. 2 3
      file/file-service/src/main/java/cn/reghao/tnb/file/app/rpc/FileServiceImpl.java
  23. 2 3
      file/file-service/src/main/java/cn/reghao/tnb/file/app/rpc/JobServiceImpl.java
  24. 2 3
      file/file-service/src/main/java/cn/reghao/tnb/file/app/service/ReportService.java
  25. 4 0
      file/file-service/src/main/resources/application-dev.yml
  26. 4 0
      file/file-service/src/main/resources/application-test.yml
  27. 0 1
      file/file-service/src/main/resources/application.yml
  28. 1 0
      gateway/src/main/java/cn/reghao/tnb/gateway/config/LogProperties.java
  29. 3 1
      gateway/src/main/java/cn/reghao/tnb/gateway/config/SpringLifecycle.java
  30. 2 1
      gateway/src/main/resources/application-dev.yml
  31. 4 0
      gateway/src/main/resources/application-test.yml
  32. 5 0
      message/message-service/pom.xml
  33. 57 0
      message/message-service/src/main/java/cn/reghao/tnb/message/app/config/SpringLifecycle.java
  34. 4 0
      message/message-service/src/main/resources/application-dev.yml
  35. 4 0
      message/message-service/src/main/resources/application-test.yml
  36. 5 0
      user/user-service/pom.xml
  37. 57 0
      user/user-service/src/main/java/cn/reghao/tnb/user/app/config/SpringLifecycle.java
  38. 4 0
      user/user-service/src/main/resources/application-dev.yml
  39. 4 0
      user/user-service/src/main/resources/application-test.yml

+ 1 - 2
README.md

@@ -24,8 +24,7 @@ tnb 项目模块:
 - message:6003
 - file:6004
 - content:6005
-- mall:6006
-- data:6007
+- data:6006
 
 ## 构建部署
 ### pull 项目源码

+ 6 - 0
account/account-service/pom.xml

@@ -32,6 +32,12 @@
             <artifactId>message-api</artifactId>
             <version>1.0.0-SNAPSHOT</version>
         </dependency>
+        <dependency>
+            <groupId>cn.reghao.tnb</groupId>
+            <artifactId>log</artifactId>
+            <version>1.0.0-SNAPSHOT</version>
+            <scope>compile</scope>
+        </dependency>
 
         <dependency>
             <groupId>org.springframework.boot</groupId>

+ 20 - 0
account/account-service/src/main/java/cn/reghao/tnb/account/app/config/LogProperties.java

@@ -0,0 +1,20 @@
+package cn.reghao.tnb.account.app.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 - 31
account/account-service/src/main/java/cn/reghao/tnb/account/app/config/spring/AppLifecycle.java

@@ -1,31 +0,0 @@
-package cn.reghao.tnb.account.app.config.spring;
-
-import cn.reghao.tnb.account.app.service.PubkeyService;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.DisposableBean;
-import org.springframework.boot.ApplicationArguments;
-import org.springframework.boot.ApplicationRunner;
-import org.springframework.stereotype.Component;
-
-/**
- * @author reghao
- * @date 2022-03-23 09:22:01
- */
-@Slf4j
-@Component
-public class AppLifecycle implements ApplicationRunner, DisposableBean {
-    private final PubkeyService pubkeyService;
-
-    public AppLifecycle(PubkeyService pubkeyService) {
-        this.pubkeyService = pubkeyService;
-    }
-
-    @Override
-    public void run(ApplicationArguments args) throws Exception {
-        pubkeyService.getPrivateKey();
-    }
-
-    @Override
-    public void destroy() {
-    }
-}

+ 59 - 0
account/account-service/src/main/java/cn/reghao/tnb/account/app/config/spring/SpringLifecycle.java

@@ -0,0 +1,59 @@
+package cn.reghao.tnb.account.app.config.spring;
+
+import cn.reghao.jutil.jdk.machine.id.MachineId;
+import cn.reghao.jutil.jdk.machine.id.MachineIdLinux;
+import cn.reghao.tnb.account.app.config.LogProperties;
+import cn.reghao.tnb.account.app.service.PubkeyService;
+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-23 09:22:01
+ */
+@Slf4j
+@Component
+public class SpringLifecycle implements ApplicationRunner, DisposableBean {
+    private final PubkeyService pubkeyService;
+    private final Environment env;
+    private final MachineId machineId;
+    private final LogProperties logProperties;
+
+    public SpringLifecycle(PubkeyService pubkeyService, Environment env, LogProperties logProperties) {
+        this.pubkeyService = pubkeyService;
+        this.env = env;
+        this.machineId = new MachineIdLinux();
+        this.logProperties = logProperties;
+    }
+
+    @Override
+    public void run(ApplicationArguments args) throws Exception {
+        if (logProperties.getEnabled()) {
+            initLogConfig();
+        }
+        pubkeyService.getPrivateKey();
+    }
+
+    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() {
+    }
+}

+ 2 - 1
account/account-service/src/main/java/cn/reghao/tnb/account/app/service/CodeService.java

@@ -35,7 +35,8 @@ public class CodeService {
     private final long sessionTimeout;
     private final AccountRepository accountRepository;
 
-    public CodeService(RabbitProducer rabbitProducer, RedisString redisString, ServerProperties serverProperties, AccountRepository accountRepository) {
+    public CodeService(RabbitProducer rabbitProducer, RedisString redisString, ServerProperties serverProperties,
+                       AccountRepository accountRepository) {
         this.rabbitProducer = rabbitProducer;
         this.redisString = redisString;
         this.sessionTimeout = serverProperties.getServlet().getSession().getTimeout().getSeconds();

+ 0 - 1
account/account-service/src/main/java/cn/reghao/tnb/account/app/service/impl/AccountTokenServiceImpl.java

@@ -57,7 +57,6 @@ public class AccountTokenServiceImpl implements AccountTokenService {
                                    ServerProperties serverProperties, PubkeyService pubkeyService,
                                    IDObfuscation userIdObfuscation) {
         long sessionTimeout = serverProperties.getServlet().getSession().getTimeout().getSeconds();
-        serverProperties.getTomcat().getBasedir();
         this.redisOps = redisOps;
         this.redisString = redisString;
         this.redisStringObject = redisStringObject;

+ 4 - 0
account/account-service/src/main/resources/application-dev.yml

@@ -1,3 +1,7 @@
+log:
+  enabled: true
+  wsUrl: ws://bnt.reghao.cn/ws/log/push
+  token: 012345678
 dubbo:
   registry:
     address: zookeeper://localhost:2181

+ 4 - 0
account/account-service/src/main/resources/application-test.yml

@@ -1,3 +1,7 @@
+log:
+  enabled: true
+  wsUrl: ws://bnt.reghao.cn/ws/log/push
+  token: 012345678
 dubbo:
   registry:
     address: zookeeper://192.168.0.210:2181

+ 1 - 0
common/src/main/java/cn/reghao/tnb/common/prop/LogProperties.java

@@ -16,4 +16,5 @@ import org.springframework.context.annotation.Configuration;
 public class LogProperties {
     private String wsUrl;
     private String token;
+    private Boolean enabled;
 }

+ 3 - 1
content/content-service/src/main/java/cn/reghao/tnb/content/app/config/SpringLifecycle.java

@@ -33,7 +33,9 @@ public class SpringLifecycle implements ApplicationRunner, DisposableBean {
 
     @Override
     public void run(ApplicationArguments args) {
-        //initLogConfig();
+        if (logProperties.getEnabled()) {
+            initLogConfig();
+        }
         log.info("ContentService 启动...");
     }
 

+ 4 - 0
content/content-service/src/main/resources/application-dev.yml

@@ -1,3 +1,7 @@
+log:
+  enabled: true
+  wsUrl: ws://bnt.reghao.cn/ws/log/push
+  token: 012345678
 dubbo:
   registry:
     address: zookeeper://localhost:2181

+ 4 - 0
content/content-service/src/main/resources/application-test.yml

@@ -1,3 +1,7 @@
+log:
+  enabled: true
+  wsUrl: ws://bnt.reghao.cn/ws/log/push
+  token: 012345678
 dubbo:
   registry:
     address: zookeeper://192.168.0.210:2181

+ 5 - 0
data/data-service/pom.xml

@@ -23,6 +23,11 @@
             <artifactId>content-api</artifactId>
             <version>1.0.0-SNAPSHOT</version>
         </dependency>
+        <dependency>
+            <groupId>cn.reghao.tnb</groupId>
+            <artifactId>log</artifactId>
+            <version>1.0.0-SNAPSHOT</version>
+        </dependency>
 
         <dependency>
             <groupId>org.springframework.boot</groupId>

+ 20 - 0
data/data-service/src/main/java/cn/reghao/tnb/data/app/config/LogProperties.java

@@ -0,0 +1,20 @@
+package cn.reghao.tnb.data.app.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;
+}

+ 56 - 0
data/data-service/src/main/java/cn/reghao/tnb/data/app/config/SpringLifecycle.java

@@ -0,0 +1,56 @@
+package cn.reghao.tnb.data.app.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 2025-04-15 10:34:47
+ */
+@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("ContentService 启动...");
+    }
+
+    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() {
+        log.info("ContentService 停止...");
+    }
+}

+ 4 - 0
data/data-service/src/main/resources/application-dev.yml

@@ -1,3 +1,7 @@
+log:
+  enabled: true
+  wsUrl: ws://bnt.reghao.cn/ws/log/push
+  token: 012345678
 spring:
   cloud:
     discovery:

+ 4 - 0
data/data-service/src/main/resources/application-test.yml

@@ -1,3 +1,7 @@
+log:
+  enabled: true
+  wsUrl: ws://bnt.reghao.cn/ws/log/push
+  token: 012345678
 spring:
   cloud:
     discovery:

+ 1 - 1
data/data-service/src/main/resources/application.yml

@@ -1,5 +1,5 @@
 server:
-  port: 6007
+  port: 6006
 spring:
   application:
     name: data-service

+ 5 - 0
file/file-service/pom.xml

@@ -48,6 +48,11 @@
             <artifactId>content-api</artifactId>
             <version>1.0.0-SNAPSHOT</version>
         </dependency>
+        <dependency>
+            <groupId>cn.reghao.tnb</groupId>
+            <artifactId>log</artifactId>
+            <version>1.0.0-SNAPSHOT</version>
+        </dependency>
 
         <dependency>
             <groupId>org.springframework.boot</groupId>

+ 32 - 3
file/file-service/src/main/java/cn/reghao/tnb/file/app/config/SpringLifecycle.java

@@ -1,16 +1,24 @@
 package cn.reghao.tnb.file.app.config;
 
+import cn.reghao.jutil.jdk.machine.id.MachineId;
+import cn.reghao.jutil.jdk.machine.id.MachineIdLinux;
+import cn.reghao.tnb.common.prop.LogProperties;
 import cn.reghao.tnb.file.app.service.AliyunOss;
 import cn.reghao.tnb.file.app.service.AliyunService;
 import cn.reghao.tnb.file.app.db.mapper.StoreConfigMapper;
 import cn.reghao.tnb.file.app.model.constant.OssType;
 import cn.reghao.tnb.file.app.model.po.StoreConfig;
+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-23 09:22:01
@@ -18,15 +26,21 @@ import org.springframework.stereotype.Component;
 @Slf4j
 @Component
 public class SpringLifecycle implements ApplicationRunner, DisposableBean {
+    private final Environment env;
+    private final MachineId machineId;
+    private final LogProperties logProperties;
     private final AppProperties appProperties;
     private final StoreConfigMapper storeConfigMapper;
     private final OssConsoleClientFactory ossConsoleClientFactory;
     private final AliyunOss aliyunOss;
     private final AliyunService aliyunService;
 
-    public SpringLifecycle(AppProperties appProperties, StoreConfigMapper storeConfigMapper,
-                           OssConsoleClientFactory ossConsoleClientFactory, AliyunOss aliyunOss,
-                           AliyunService aliyunService) {
+    public SpringLifecycle(Environment env, LogProperties logProperties, AppProperties appProperties,
+                           StoreConfigMapper storeConfigMapper, OssConsoleClientFactory ossConsoleClientFactory,
+                           AliyunOss aliyunOss, AliyunService aliyunService) {
+        this.env = env;
+        this.machineId = new MachineIdLinux();
+        this.logProperties = logProperties;
         this.appProperties = appProperties;
         this.storeConfigMapper = storeConfigMapper;
         this.ossConsoleClientFactory = ossConsoleClientFactory;
@@ -36,6 +50,10 @@ public class SpringLifecycle implements ApplicationRunner, DisposableBean {
 
     @Override
     public void run(ApplicationArguments args) throws Exception {
+        if (logProperties.getEnabled()) {
+            initLogConfig();
+        }
+
         int ossType = appProperties.getOssType();
         StoreConfig storeConfig = storeConfigMapper.findByOssType(ossType);
         if (ossType == OssType.localOss.getCode()) {
@@ -49,6 +67,17 @@ public class SpringLifecycle implements ApplicationRunner, DisposableBean {
         }
     }
 
+    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() {
     }

+ 2 - 3
file/file-service/src/main/java/cn/reghao/tnb/file/app/rpc/FileServiceImpl.java

@@ -17,7 +17,6 @@ import cn.reghao.tnb.file.app.util.ImageUtil;
 import cn.reghao.tnb.file.app.model.constant.OssType;
 import org.apache.commons.io.FileUtils;
 import org.apache.dubbo.config.annotation.DubboService;
-import org.springframework.boot.autoconfigure.web.ServerProperties;
 import org.springframework.stereotype.Service;
 
 import java.io.ByteArrayOutputStream;
@@ -40,11 +39,11 @@ public class FileServiceImpl implements FileService {
     private final AppProperties appProperties;
 
     private FileServiceImpl(OssConsoleClientFactory ossConsoleClientFactory, JobDetailMapper jobDetailMapper,
-                            ServerProperties serverProperties, AppProperties appProperties) {
+                            AppProperties appProperties) {
         this.ossConsoleClientFactory = ossConsoleClientFactory;
         this.ossType = appProperties.getOssType();
         this.jobDetailMapper = jobDetailMapper;
-        this.baseDir = serverProperties.getTomcat().getBasedir().getAbsolutePath();
+        this.baseDir = System.getProperty("java.io.tmpdir");
         this.appProperties = appProperties;
     }
 

+ 2 - 3
file/file-service/src/main/java/cn/reghao/tnb/file/app/rpc/JobServiceImpl.java

@@ -20,7 +20,6 @@ import cn.reghao.tnb.file.app.model.constant.JobStatus;
 import cn.reghao.tnb.file.app.model.po.JobDetail;
 import org.apache.dubbo.config.annotation.DubboReference;
 import org.apache.dubbo.config.annotation.DubboService;
-import org.springframework.boot.autoconfigure.web.ServerProperties;
 import org.springframework.stereotype.Service;
 
 import java.util.UUID;
@@ -47,12 +46,12 @@ public class JobServiceImpl implements JobService {
     private final OrderMapper orderMapper;
 
     public JobServiceImpl(JobContext jobContext, OssConsoleClientFactory ossConsoleClientFactory,
-                          JobDetailMapper jobDetailMapper, ServerProperties serverProperties, OrderMapper orderMapper) {
+                          JobDetailMapper jobDetailMapper, OrderMapper orderMapper) {
         this.idGenerator = new SnowFlake(1L, 1L);
         this.jobContext = jobContext;
         this.ossConsoleClientFactory = ossConsoleClientFactory;
         this.jobDetailMapper = jobDetailMapper;
-        this.baseDir = serverProperties.getTomcat().getBasedir().getAbsolutePath();
+        this.baseDir = System.getProperty("java.io.tmpdir");
         this.orderMapper = orderMapper;
     }
 

+ 2 - 3
file/file-service/src/main/java/cn/reghao/tnb/file/app/service/ReportService.java

@@ -4,7 +4,6 @@ import cn.reghao.tnb.file.app.util.pdf.ContentStyle;
 import cn.reghao.tnb.file.app.util.pdf.DateTimeUtil;
 import cn.reghao.tnb.file.app.util.pdf.PDFUtil;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.boot.autoconfigure.web.ServerProperties;
 import org.springframework.stereotype.Service;
 
 import java.util.Date;
@@ -20,8 +19,8 @@ public class ReportService {
     private final String baseDir;
     private final PDFUtil pdfUtil;
 
-    public ReportService(ServerProperties serverProperties) {
-        this.baseDir = serverProperties.getTomcat().getBasedir().getAbsolutePath();
+    public ReportService() {
+        this.baseDir = System.getProperty("java.io.tmpdir");
         this.pdfUtil = new PDFUtil();
     }
 

+ 4 - 0
file/file-service/src/main/resources/application-dev.yml

@@ -1,3 +1,7 @@
+log:
+  enabled: true
+  wsUrl: ws://bnt.reghao.cn/ws/log/push
+  token: 012345678
 dubbo:
   registry:
     address: zookeeper://localhost:2181

+ 4 - 0
file/file-service/src/main/resources/application-test.yml

@@ -1,3 +1,7 @@
+log:
+  enabled: true
+  wsUrl: ws://bnt.reghao.cn/ws/log/push
+  token: 012345678
 dubbo:
   registry:
     address: zookeeper://192.168.0.210:2181

+ 0 - 1
file/file-service/src/main/resources/application.yml

@@ -9,7 +9,6 @@ dubbo:
 server:
   port: 6004
   tomcat:
-    basedir: /opt/tmp/tomcat
     max-http-form-post-size: 4MB
 spring:
   servlet:

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

@@ -16,4 +16,5 @@ import org.springframework.context.annotation.Configuration;
 public class LogProperties {
     private String wsUrl;
     private String token;
+    private Boolean enabled;
 }

+ 3 - 1
gateway/src/main/java/cn/reghao/tnb/gateway/config/SpringLifecycle.java

@@ -32,7 +32,9 @@ public class SpringLifecycle implements ApplicationRunner, DisposableBean {
 
     @Override
     public void run(ApplicationArguments args) {
-        //initLogConfig();
+        if (logProperties.getEnabled()) {
+            initLogConfig();
+        }
         log.info("gateway 启动...");
     }
 

+ 2 - 1
gateway/src/main/resources/application-dev.yml

@@ -1,5 +1,6 @@
 log:
-  wsUrl: ws://127.0.0.1:4030/ws/log/push
+  enabled: true
+  wsUrl: ws://bnt.reghao.cn/ws/log/push
   token: 012345678
 spring:
   redis:

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

@@ -1,3 +1,7 @@
+log:
+  enabled: true
+  wsUrl: ws://bnt.reghao.cn/ws/log/push
+  token: 012345678
 spring:
   redis:
     database: 0

+ 5 - 0
message/message-service/pom.xml

@@ -43,6 +43,11 @@
             <artifactId>user-api</artifactId>
             <version>1.0.0-SNAPSHOT</version>
         </dependency>
+        <dependency>
+            <groupId>cn.reghao.tnb</groupId>
+            <artifactId>log</artifactId>
+            <version>1.0.0-SNAPSHOT</version>
+        </dependency>
 
         <dependency>
             <groupId>mysql</groupId>

+ 57 - 0
message/message-service/src/main/java/cn/reghao/tnb/message/app/config/SpringLifecycle.java

@@ -0,0 +1,57 @@
+package cn.reghao.tnb.message.app.config;
+
+import cn.reghao.jutil.jdk.machine.id.MachineId;
+import cn.reghao.jutil.jdk.machine.id.MachineIdLinux;
+import cn.reghao.tnb.common.prop.LogProperties;
+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
+ */
+@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("ContentService 启动...");
+    }
+
+    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() {
+        log.info("ContentService 停止...");
+    }
+}

+ 4 - 0
message/message-service/src/main/resources/application-dev.yml

@@ -1,3 +1,7 @@
+log:
+  enabled: true
+  wsUrl: ws://bnt.reghao.cn/ws/log/push
+  token: 012345678
 dubbo:
   registry:
     address: zookeeper://localhost:2181

+ 4 - 0
message/message-service/src/main/resources/application-test.yml

@@ -1,3 +1,7 @@
+log:
+  enabled: true
+  wsUrl: ws://bnt.reghao.cn/ws/log/push
+  token: 012345678
 dubbo:
   registry:
     address: zookeeper://192.168.0.210:2181

+ 5 - 0
user/user-service/pom.xml

@@ -53,6 +53,11 @@
             <artifactId>admin-api</artifactId>
             <version>1.0.0-SNAPSHOT</version>
         </dependency>
+        <dependency>
+            <groupId>cn.reghao.tnb</groupId>
+            <artifactId>log</artifactId>
+            <version>1.0.0-SNAPSHOT</version>
+        </dependency>
 
         <dependency>
             <groupId>org.springframework.boot</groupId>

+ 57 - 0
user/user-service/src/main/java/cn/reghao/tnb/user/app/config/SpringLifecycle.java

@@ -0,0 +1,57 @@
+package cn.reghao.tnb.user.app.config;
+
+import cn.reghao.jutil.jdk.machine.id.MachineId;
+import cn.reghao.jutil.jdk.machine.id.MachineIdLinux;
+import cn.reghao.tnb.common.prop.LogProperties;
+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
+ */
+@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("ContentService 启动...");
+    }
+
+    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() {
+        log.info("ContentService 停止...");
+    }
+}

+ 4 - 0
user/user-service/src/main/resources/application-dev.yml

@@ -1,3 +1,7 @@
+log:
+  enabled: true
+  wsUrl: ws://bnt.reghao.cn/ws/log/push
+  token: 012345678
 dubbo:
   registry:
     address: zookeeper://localhost:2181

+ 4 - 0
user/user-service/src/main/resources/application-test.yml

@@ -1,3 +1,7 @@
+log:
+  enabled: true
+  wsUrl: ws://bnt.reghao.cn/ws/log/push
+  token: 012345678
 dubbo:
   registry:
     address: zookeeper://192.168.0.210:2181