소스 검색

优化代码

johnclot69 2 주 전
부모
커밋
38f8db1c53
2개의 변경된 파일28개의 추가작업 그리고 20개의 파일을 삭제
  1. 8 8
      incubator-core/build.gradle
  2. 20 12
      incubator-core/src/main/java/com/incubator/core/network/NettySystem.java

+ 8 - 8
incubator-core/build.gradle

@@ -8,17 +8,17 @@ version = "1.0-SNAPSHOT"
 // 项目依赖
 dependencies {
     // 日志相关
-    api 'org.slf4j:slf4j-api:2.0.16' // SLF4J API
-    api 'org.apache.logging.log4j:log4j-slf4j2-impl:2.24.2' // SLF4J -> Log4j2 桥接
-    api 'org.apache.logging.log4j:log4j-core:2.24.2' // Log4j2 实现
+    api "org.slf4j:slf4j-api:2.0.16" // SLF4J API
+    api "org.apache.logging.log4j:log4j-slf4j2-impl:2.24.2" // SLF4J -> Log4j2 桥接
+    api "org.apache.logging.log4j:log4j-core:2.24.2" // Log4j2 实现
 
     // 系统核心依赖
-    api 'net.onedaybeard.artemis:artemis-odb:2.3.0'
-    api 'com.lmax:disruptor:4.0.0' // 高性能队列
+    api "net.onedaybeard.artemis:artemis-odb:2.3.0"
+    api "com.lmax:disruptor:4.0.0" // 高性能队列
 
     // 网络通信和序列化
-    api 'io.netty:netty-all:4.2.1.Final' // Netty 全功能包
-    api 'org.msgpack:msgpack-core:0.9.9' // MessagePack 序列化
+    api "io.netty:netty-all:4.2.1.Final" // Netty 全功能包
+    api "org.msgpack:msgpack-core:0.9.9" // MessagePack 序列化
 
     // 测试依赖
     testImplementation platform("org.junit:junit-bom:5.10.0")
@@ -27,7 +27,7 @@ dependencies {
 }
 
 configurations.configureEach {
-    exclude group: 'ch.qos.logback', module: 'logback-classic'
+    exclude group: "ch.qos.logback", module: "logback-classic"
 }
 
 build.dependsOn(copyAllDependencies)

+ 20 - 12
incubator-core/src/main/java/com/incubator/core/network/NettySystem.java

@@ -32,8 +32,8 @@ import java.util.concurrent.ThreadFactory;
 public class NettySystem extends BaseSystem {
 
     private final int port;
-    private static final int BOSS_THREADS = 1;
-//    private static final int BOSS_THREADS = Math.max(1, Runtime.getRuntime().availableProcessors() / 2);
+//    private static final int BOSS_THREADS = 1;
+    private static final int BOSS_THREADS = Math.max(1, Runtime.getRuntime().availableProcessors() / 2);
     private static final int WORKER_THREADS = Math.max(1, Runtime.getRuntime().availableProcessors() * 2);
 
     private final ServerBootstrap bootstrap = new ServerBootstrap();
@@ -80,10 +80,10 @@ public class NettySystem extends BaseSystem {
                 // 子选项
                 .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                 .childOption(ChannelOption.TCP_NODELAY, true) // 关闭 Nagle 算法,提升小数据传输性能
-                .childOption(ChannelOption.SO_KEEPALIVE, true) // 保持连接
-                .childOption(ChannelOption.SO_RCVBUF, 64 * 1024) // 增大接收缓冲区大小
-                .childOption(ChannelOption.SO_SNDBUF, 64 * 1024) // 增大发送缓冲区大小
-                .childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(32 * 1024, 64 * 1024)) // 提升写缓冲区大小
+//                .childOption(ChannelOption.SO_KEEPALIVE, true) // 保持连接
+                .childOption(ChannelOption.SO_RCVBUF, 4 * 64 * 1024) // 增大接收缓冲区大小
+                .childOption(ChannelOption.SO_SNDBUF, 4 * 64 * 1024) // 增大发送缓冲区大小
+                .childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(4 * 32 * 1024, 4 * 64 * 1024)) // 提升写缓冲区大小
                 .childHandler(new ChannelInitializer<SocketChannel>() {
                     @Override
                     protected void initChannel(SocketChannel ch) throws Exception {
@@ -143,16 +143,24 @@ public class NettySystem extends BaseSystem {
             if (this.serverChannel != null) {
                 this.serverChannel.close().sync();
             }
-            if (this.bossGroup != null) {
-                this.bossGroup.shutdownGracefully().sync();
-            }
-            if (this.workerGroup != null) {
-                this.workerGroup.shutdownGracefully().sync();
-            }
+            this.shutdownEventLoopGroup(this.bossGroup, "BossGroup");
+            this.shutdownEventLoopGroup(this.workerGroup, "WorkerGroup");
             Logger.info("[Netty] 服务停止成功");
         } catch (InterruptedException e) {
             Logger.error("[Netty] 服务停止异常: {}", e.getMessage(), e);
         }
     }
 
+    private void shutdownEventLoopGroup(EventLoopGroup group, String groupName) {
+        if (group != null) {
+            try {
+                group.shutdownGracefully().sync();
+                Logger.info("[Netty] {} 已关闭", groupName);
+            } catch (InterruptedException e) {
+                Thread.currentThread().interrupt();
+                Logger.error("[Netty] {} 关闭失败: {}", groupName, e.getMessage(), e);
+            }
+        }
+    }
+
 }