perf[net]: 优化netty服务器启动的配置参数

This commit is contained in:
jaysunxiao committed 2021-08-25 22:43:40 +08:00
1 parent c48a66d3fc
commit cfaf5a5557
8 files changed
+91 -19

No files matched your search

@@ -17,12 +17,10 @@ import com.zfoo.net.NetContext;
import com.zfoo.net.handler.BaseDispatcherHandler;
import com.zfoo.net.session.model.Session;
import com.zfoo.protocol.exception.ExceptionUtils;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.*;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollSocketChannel;
@@ -66,6 +64,7 @@ public abstract class AbstractClient implements IClient {
this.bootstrap.group(nioEventLoopGroup)
.channel(Epoll.isAvailable() ? EpollSocketChannel.class : NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(16 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_MB))
.handler(channelChannelInitializer());
var channelFuture = bootstrap.connect(hostAddress, port);
channelFuture.syncUninterruptibly();
@@ -13,6 +13,7 @@
package com.zfoo.net.core;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
@@ -80,6 +81,7 @@ public abstract class AbstractServer implements IServer {
.channel(Epoll.isAvailable() ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
.option(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.TCP_NODELAY, true)
.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(16 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_MB))
.childHandler(channelChannelInitializer);
// 绑定端口,同步等待成功
// channelFuture = bootstrap.bind(hostAddress, port).sync();
@@ -19,6 +19,7 @@ import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler;
import com.zfoo.net.handler.idle.ServerIdleHandler;
import com.zfoo.net.session.model.Session;
import com.zfoo.protocol.IPacket;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
@@ -63,10 +64,10 @@ public class WebsocketGatewayServer extends AbstractServer {
channel.pipeline().addLast(new IdleStateHandler(0, 0, 180));
channel.pipeline().addLast(new ServerIdleHandler());
channel.pipeline().addLast(new HttpServerCodec());
channel.pipeline().addLast(new ChunkedWriteHandler());
channel.pipeline().addLast(new HttpObjectAggregator(64 * 1024));
channel.pipeline().addLast(new HttpServerCodec(8 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB));
channel.pipeline().addLast(new HttpObjectAggregator(16 * IOUtils.BYTES_PER_MB));
channel.pipeline().addLast(new WebSocketServerProtocolHandler("/websocket"));
channel.pipeline().addLast(new ChunkedWriteHandler());
channel.pipeline().addLast(new WebSocketCodecHandler());
channel.pipeline().addLast(new GatewayDispatcherHandler(packetFilter));
}
@@ -20,6 +20,7 @@ import com.zfoo.net.handler.idle.ServerIdleHandler;
import com.zfoo.net.session.model.Session;
import com.zfoo.protocol.IPacket;
import com.zfoo.protocol.exception.ExceptionUtils;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
@@ -81,10 +82,10 @@ public class WebsocketSslGatewayServer extends AbstractServer {
channel.pipeline().addLast(new ServerIdleHandler());
channel.pipeline().addLast(sslContext.newHandler(channel.alloc()));
channel.pipeline().addLast(new HttpServerCodec());
channel.pipeline().addLast(new ChunkedWriteHandler());
channel.pipeline().addLast(new HttpObjectAggregator(64 * 1024));
channel.pipeline().addLast(new HttpServerCodec(8 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB));
channel.pipeline().addLast(new HttpObjectAggregator(16 * IOUtils.BYTES_PER_MB));
channel.pipeline().addLast(new WebSocketServerProtocolHandler("/"));
channel.pipeline().addLast(new ChunkedWriteHandler());
channel.pipeline().addLast(new WebSocketCodecHandler());
channel.pipeline().addLast(new GatewayDispatcherHandler(packetFilter));
}
@@ -17,6 +17,7 @@ import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.handler.ServerDispatcherHandler;
import com.zfoo.net.handler.codec.http.HttpCodecHandler;
import com.zfoo.net.packet.model.DecodedPacketInfo;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
@@ -52,9 +53,9 @@ public class HttpServer extends AbstractServer {
private class ChannelHandlerInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel channel) {
channel.pipeline().addLast(new HttpServerCodec());
channel.pipeline().addLast(new HttpServerCodec(8 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB));
channel.pipeline().addLast(new HttpObjectAggregator(16 * IOUtils.BYTES_PER_MB));
channel.pipeline().addLast(new ChunkedWriteHandler());
channel.pipeline().addLast(new HttpObjectAggregator(64 * 1024));
channel.pipeline().addLast(new HttpCodecHandler(uriResolver));
channel.pipeline().addLast(new ServerDispatcherHandler());
}
@@ -16,6 +16,7 @@ package com.zfoo.net.core.websocket;
import com.zfoo.net.core.AbstractClient;
import com.zfoo.net.handler.ClientDispatcherHandler;
import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
@@ -48,10 +49,10 @@ public class WebsocketClient extends AbstractClient {
public class ChannelHandlerInitializer extends ChannelInitializer<SocketChannel> {
@Override
public void initChannel(SocketChannel channel) {
channel.pipeline().addLast(new HttpClientCodec());
channel.pipeline().addLast(new ChunkedWriteHandler());
channel.pipeline().addLast(new HttpObjectAggregator(64 * 1024));
channel.pipeline().addLast(new HttpClientCodec(8 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB));
channel.pipeline().addLast(new HttpObjectAggregator(16 * IOUtils.BYTES_PER_MB));
channel.pipeline().addLast(new WebSocketClientProtocolHandler(webSocketClientProtocolConfig));
channel.pipeline().addLast(new ChunkedWriteHandler());
channel.pipeline().addLast(new WebSocketCodecHandler());
channel.pipeline().addLast(new ClientDispatcherHandler());
}
@@ -16,6 +16,7 @@ package com.zfoo.net.core.websocket;
import com.zfoo.net.core.AbstractServer;
import com.zfoo.net.handler.ServerDispatcherHandler;
import com.zfoo.net.handler.codec.websocket.WebSocketCodecHandler;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
@@ -45,14 +46,14 @@ public class WebsocketServer extends AbstractServer {
@Override
public void initChannel(SocketChannel channel) {
// 编解码 http 请求
channel.pipeline().addLast(new HttpServerCodec());
// 写文件内容,支持异步发送大的码流,一般用于发送文件流
channel.pipeline().addLast(new ChunkedWriteHandler());
channel.pipeline().addLast(new HttpServerCodec(8 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB, 16 * IOUtils.BYTES_PER_KB));
// 聚合解码 HttpRequest/HttpContent/LastHttpContent 到 FullHttpRequest
// 保证接收的 Http 请求的完整性
channel.pipeline().addLast(new HttpObjectAggregator(64 * 1024));
channel.pipeline().addLast(new HttpObjectAggregator(16 * IOUtils.BYTES_PER_MB));
// 处理其他的 WebSocketFrame
channel.pipeline().addLast(new WebSocketServerProtocolHandler("/websocket"));
// 写文件内容,支持异步发送大的码流,一般用于发送文件流
channel.pipeline().addLast(new ChunkedWriteHandler());
// 编解码WebSocketFrame二进制协议
channel.pipeline().addLast(new WebSocketCodecHandler());
channel.pipeline().addLast(new ServerDispatcherHandler());