|
@@ -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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|