Wor*_*orM 4 java performance tcp tcplistener netty
我有一些关于Netty(服务器端),TCP/IP应用程序的问题;
我想知道在将请求从boss线程传递给工作线程时是否会因为netty(由于缺少配置等)而存在延迟?
我在用 :
new OrderedMemoryAwareThreadPoolExecutor(350, 0, 0, 1, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
实际上,我设置了最大线程数,350因为我不确定最佳数量.我每分钟记录同时工作的线程数,似乎平均值太低(几乎超过10).所以我会减少这个数字,因为它不是必需的.
有没有其他参数,重要的一点,我应该知道,以获得最佳性能?
bootstrap.setOption("tcpNoDelay", true); - 设置此参数有什么缺点吗?考虑到交货时间非常重要.
线程池执行器:
OrderedMemoryAwareThreadPoolExecutor executor = new OrderedMemoryAwareThreadPoolExecutor(48, 0, 0, 1, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
这是我的管道工厂:
ChannelPipeline pipeline = pipeline();
pipeline.addLast("frameDecoder", new DelimiterBasedFrameDecoder(GProperties.getIntProperty("decoder.maxFrameLength", 8000 * 1024), Delimiters.nulDelimiter()));
pipeline.addLast("stringDecoder", new StringDecoder( CharsetUtil.UTF_8 ));
pipeline.addLast("frameEncoder", new NullTermMessageEncoder());
pipeline.addLast("stringEncoder", new JSONEncoder( CharsetUtil.UTF_8 ));
pipeline.addLast("timeout", new IdleStateHandler(idleTimer, 42 , 0, 0));
pipeline.addLast("executor", new ExecutionHandler(executor));
pipeline.addLast("handler", new GServerHandler());
Run Code Online (Sandbox Code Playgroud)
和ServerBootstrap:
gServerBootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
gServerBootstrap.setPipelineFactory(new GServerPipelineFactory());
gServerBootstrap.setOption("backlog", 8129);
gServerBootstrap.setOption("child.tcpNoDelay", true);
gServerBootstrap.bind(new InetSocketAddress(GProperties.getIntProperty("server.port", 7679)));
Run Code Online (Sandbox Code Playgroud)
你能为这种配置建议什么?
Netty Boss线程仅用于设置连接,工作线程用于运行NioWorker(非阻塞读/写)或OioWorker(阻塞读/写).
如果您有执行处理程序,则工作线程将向OrderedMemoryAwareThreadPoolExecutor提交消息事件.
1)将Netty I/O工作线程数增加到超过处理器数量*2将无济于事.如果您正在使用分阶段执行程序,则为非I/O任务提供多个分阶段执行处理程序可能会增加延迟.
注意:最好在OMTPE构造函数中设置自己的ObjectSizeEstimator实现,因为许多CPU周期都花在计算使用的通道内存上.
2)您可以尝试其他一些Netty参数
//setting buffer size can improve I/O
bootstrap.setOption("child.sendBufferSize", 1048576);
bootstrap.setOption("child.receiveBufferSize", 1048576);
// better to have an receive buffer predictor
bootstrap.setOption("receiveBufferSizePredictorFactory", new AdaptiveReceiveBufferSizePredictorFactory(MIN_PACKET_SIZE, INITIAL_PACKET_SIZE, MAX_PACKET_SIZE))
//if the server is sending 1000 messages per sec, optimum write buffer water marks will
//prevent unnecessary throttling, Check NioSocketChannelConfig doc
bootstrap.setOption("writeBufferLowWaterMark", 32 * 1024);
bootstrap.setOption("writeBufferHighWaterMark", 64 * 1024);
Run Code Online (Sandbox Code Playgroud)
3)服务器引导程序应该是bootstrap.setOption("child.tcpNoDelay",true).
有一个实验隐藏参数:
Netty NioWorker使用SelectorUtil.select等待选择器事件,等待时间在SelectorUtil中进行硬编码,
selector.select(500);
Run Code Online (Sandbox Code Playgroud)
设置较小的值可以在netty sctp传输实现中获得更好的性能.不确定TCP.