Browse Source

netty loggingHandler dynamic add/remove, netty logging file separate

shjung 2 weeks ago
parent
commit
022f57e069

+ 0 - 1
evps-comm-server/src/main/java/com/evps/comm/server/config/ApplicationConfig.java

@@ -23,7 +23,6 @@ public class ApplicationConfig extends NettyServerConfig {
 
     private String processId = "evps-comm-server";
     private double cpuLimits = 75;
-    private boolean packetDebug = true;
     private int autoEndMinutes = 20;
     private int lastCommTimeoutSeconds = 60;
 

+ 57 - 1
evps-comm-server/src/main/java/com/evps/comm/server/config/TraceConfig.java

@@ -2,8 +2,13 @@ package com.evps.comm.server.config;
 
 import com.evps.comm.server.EvpsCommServerApplication;
 import com.evps.comm.server.repository.ApplicationRepository;
+import com.evps.comm.server.xnet.server.handler.MdcLoggingHandler;
 import com.evps.common.dto.EvpsCenter;
+import com.evps.common.dto.NET;
 import com.its.common.utils.StringUtils;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelPipeline;
+import io.netty.handler.logging.LogLevel;
 import lombok.Getter;
 import lombok.RequiredArgsConstructor;
 import lombok.Setter;
@@ -64,8 +69,59 @@ public class TraceConfig {
             });
         }
         catch(Exception e) {
-            log.error("{}.loadTraceInfo Exception: {}", this.getClass().getSimpleName(), e.toString());
+            log.error("{}.loadTraceInfo(DUMP) Exception: {}", this.getClass().getSimpleName(), e.toString());
         }
+
+        try {
+            this.repo.getCenterMap().forEach((key, center) -> center.setTcpDump(false));
+
+            Properties props = getProperties();
+            String dumps = props.getProperty("TCP-DUMP",  "").trim();
+            if (dumps.isEmpty()) {
+                return;
+            }
+
+            List<String> regionCds = StringUtils.split(dumps, ",");
+            regionCds.forEach(id -> {
+                EvpsCenter center = this.repo.getCenterMap().get(id.trim());
+                if (center != null) {
+                    center.setTcpDump(true);
+//                    log.info("TraceConfig.loadTraceInfo: Center Dump: {}", center.getCenterId());
+                }
+            });
+
+        }
+        catch(Exception e) {
+            log.error("{}.loadTraceInfo(TCP-DUMP) Exception: {}", this.getClass().getSimpleName(), e.toString());
+        }
+
+        updateLoggingChannel();
     }
 
+    private void updateLoggingChannel() {
+
+        try {
+            this.repo.getCenterMap().forEach((key, center) -> {
+                if (center.getNetState().getChannel() != null &&center.getNetState().getState() != NET.CLOSED) {
+                    Channel channel = center.getNetState().getChannel();
+                    channel.eventLoop().execute(() -> {
+                        ChannelPipeline pipeline = channel.pipeline();
+                        boolean shouldHaveHandler = center.isTcpDump();
+                        boolean hasHandler = pipeline.get(ApplicationRepository.MDC_LOGGING_HANDLER_NAME) != null;
+
+                        if (shouldHaveHandler && !hasHandler) {
+                            log.info("Adding TCP dump handler to channel for center: {}", center.getCenterId());
+                            pipeline.addFirst(ApplicationRepository.MDC_LOGGING_HANDLER_NAME, new MdcLoggingHandler(LogLevel.INFO));
+                        } else if (!shouldHaveHandler && hasHandler) {
+                            log.info("Removing TCP dump handler from channel for center: {}", center.getCenterId());
+                            pipeline.remove(ApplicationRepository.MDC_LOGGING_HANDLER_NAME);
+                        }
+                    });
+                }
+            });
+        }
+        catch(Exception e) {
+            log.error("{}.updateLoggingChannel Exception: {}", this.getClass().getSimpleName(), e.toString());
+        }
+    }
 }

+ 1 - 0
evps-comm-server/src/main/java/com/evps/comm/server/entity/TbEvpsCenter.java

@@ -29,6 +29,7 @@ public class TbEvpsCenter implements Serializable {
                 .ipAddress(this.ipAddress.trim())
                 .useYn("Y".equals(this.useYn))
                 .dump(false)
+                .tcpDump(false)
                 .serviceBuffer(null)
                 .nodeBuffer(null)
                 .signalBuffer(null)

+ 2 - 0
evps-comm-server/src/main/java/com/evps/comm/server/repository/ApplicationRepository.java

@@ -34,6 +34,7 @@ import java.util.concurrent.ConcurrentHashMap;
 public class ApplicationRepository {
 
     public static final AttributeKey<EvpsCenter> SIG_REGION_ATTRIBUTE_KEY = AttributeKey.valueOf("Region");
+    public static final String MDC_LOGGING_HANDLER_NAME = "dumpLogger";
 
     public static final EvpsCenter center = EvpsCenter.builder()
             .idx(0)
@@ -43,6 +44,7 @@ public class ApplicationRepository {
             .ipAddress("127.0.0.1")
             .netState(new NetState())
             .dump(false)
+            .tcpDump(false)
             .build();
 
     @Getter

+ 2 - 2
evps-comm-server/src/main/java/com/evps/comm/server/xnet/server/EvpsCommServerInitializer.java

@@ -80,9 +80,9 @@ public class EvpsCommServerInitializer extends ChannelInitializer<Channel> {
 
         IdleStateHandler idleStateHandler = new IdleStateHandler(this.config.getReaderIdleTimeSeconds(), 0, 0);
         ChannelPipeline pipeline = channel.pipeline();
-        if (this.config.isPacketDebug()) {
+        if (center.isTcpDump()) {
             //pipeline.addLast(new LoggingHandler(LogLevel.INFO));
-            pipeline.addLast(new MdcLoggingHandler(LogLevel.INFO));
+            pipeline.addLast(ApplicationRepository.MDC_LOGGING_HANDLER_NAME, new MdcLoggingHandler(LogLevel.INFO));
         }
         pipeline.addLast("idleStateHandler", idleStateHandler);
         pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(

+ 0 - 1
evps-comm-server/src/main/resources/application.yml

@@ -49,7 +49,6 @@ management:
 application:
   process-id: evps-comm-server
   binding-port: 7800
-  packet-debug: true
   auto-end-minutes: 20
   last-comm-timeout-seconds: 60
   backlog: 0

+ 2 - 0
evps-common/src/main/java/com/evps/common/dto/EvpsCenter.java

@@ -23,6 +23,7 @@ public class EvpsCenter {
     private boolean useYn;
 
     private boolean dump;
+    private boolean tcpDump;
 
     // 센터 네트워크 상태 정보
     private NetState netState;
@@ -43,6 +44,7 @@ public class EvpsCenter {
         this.ipAddress = dto.getIpAddress();
         this.useYn = dto.isUseYn();
         this.dump = dto.isDump();
+        this.tcpDump = dto.isTcpDump();
     }
 
     public void clearServiceBuffer() {