update 优化netty配置

This commit is contained in:
Lemon
2025-07-21 14:49:34 +08:00
parent 55d7788c1c
commit c2b30e093e
2 changed files with 13 additions and 7 deletions

View File

@@ -15,6 +15,7 @@ import io.netty.handler.codec.mqtt.MqttEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.ResourceLeakDetector;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@@ -53,23 +54,28 @@ public class NettyServerManager implements CommandLineRunner {
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.DEBUG))
// .option(ChannelOption.SO_BACKLOG, 128) // 默认128
.option(ChannelOption.SO_BACKLOG, 1024)
.option(ChannelOption.SO_BACKLOG, 2048)
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) // 启用池化内存分配器
// .option(ChannelOption.SO_RCVBUF, 64 * 1024)
// .option(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.SO_KEEPALIVE, true) // 保持连接
// .childOption(ChannelOption.SO_RCVBUF, 64 * 1024) // 接收缓冲区
// .childOption(ChannelOption.SO_SNDBUF, 64 * 1024) // 发送缓冲区
.childOption(ChannelOption.SO_RCVBUF, 1024 * 1024) // 接收缓冲区
.childOption(ChannelOption.SO_SNDBUF, 1024 * 1024) // 发送缓冲区
.childOption(ChannelOption.SO_RCVBUF, 2 * 1024 * 1024) // 接收缓冲区
.childOption(ChannelOption.SO_SNDBUF, 2 * 1024 * 1024) // 发送缓冲区
.childOption(ChannelOption.TCP_NODELAY, true) // 禁用 Nagle 算法
.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(32 * 1024, 64 * 1024)) // 写缓冲水位
.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(64 * 1024, 256 * 1024)) // 写缓冲水位
.childOption(ChannelOption.SO_REUSEADDR, true)
// .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) // 启用池化内存分配器
// 添加连接超时设置
.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000)
.childHandler(nettyServerChannelInitializer)
.localAddress(new InetSocketAddress(host, port));
ChannelFuture future = bootstrap.bind(port).sync();
// 在启动时启用内存泄漏检测
ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.PARANOID);
if (future.isSuccess()) {
log.info("NettyServer启动成功, 开始监听端口:{}", port);
} else {

View File

@@ -150,9 +150,9 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
ByteBuf buffer = ctx.alloc().buffer().writeBytes(response);
this.channelWrite(channel.id(), buffer);
// 获取桩号
String pileSn = PileChannelEntity.getPileSnByChannelId(ctx.channel().id().asLongText());
// String pileSn = PileChannelEntity.getPileSnByChannelId(ctx.channel().id().asLongText());
// 批量响应客户端
this.channelWriteBatch(pileSn, buffer);
// this.channelWriteBatch(pileSn, buffer);
if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) {
// 应答帧类型
byte[] responseFrameTypeBytes = YKCFrameTypeCode.PlatformAnswersRelation.getResponseFrameTypeBytes(frameTypeBytes);