Sfoglia il codice sorgente

eureka 添加日志配置

reghao 11 mesi fa
parent
commit
e65d742f85

+ 6 - 0
eureka/eureka-server/pom.xml

@@ -18,6 +18,12 @@
     </properties>
 
     <dependencies>
+        <dependency>
+            <groupId>cn.reghao.tnb</groupId>
+            <artifactId>log</artifactId>
+            <version>1.0.0-SNAPSHOT</version>
+        </dependency>
+
         <dependency>
             <groupId>cn.reghao.tnb.eureka</groupId>
             <artifactId>eureka-api</artifactId>

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

@@ -0,0 +1,20 @@
+package cn.reghao.tnb.eureka.server.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
eureka/eureka-server/src/main/java/cn/reghao/tnb/eureka/server/config/SpringLifecycle.java

@@ -0,0 +1,56 @@
+package cn.reghao.tnb.eureka.server.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-23 09:22:01
+ */
+@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) throws Exception {
+        if (logProperties.getEnabled()) {
+            initLogConfig();
+        }
+        log.info("EurekaServer 启动...");
+    }
+
+    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("EurekaServer 停止...");
+    }
+}

+ 4 - 0
eureka/eureka-server/src/main/resources/application-dev.yml

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

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

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