netty实战之百万级流量NioEventLoopGroup线程数配置

来源:互联网 发布:华为交换机 端口详解 编辑:程序博客网 时间:2024/06/05 08:17

编写netty服务端程序的时候,会使用到两个线程组

EventLoopGroup parentGroup = new NioEventLoopGroup();
EventLoopGroup childGroup = new NioEventLoopGroup();

那么parentGroup和childGroup分别应该设置多少个线程呢?

关于netty的线程组概念,李林锋的Netty系列之Netty线程模型已经有详细介绍了,这里不详细说了。

如果你的应用服务的QPS只是几百万,那么parentGroup只需要设置为2,childGroup设置为4

EventLoopGroup  parentGroup = new NioEventLoopGroup(2, new DefaultThreadFactory("server1", true));EventLoopGroup  childGroup = new NioEventLoopGroup(4, new DefaultThreadFactory("server2", true));

目前自己参与的应用,QPS是一百多万,上面的设置是完全可以应付的。

至于netty客户端程序,设置4个线程。

EventLoopGroup  workGroup = new NioEventLoopGroup(4, new DefaultThreadFactory("client", true));

至于应用如果需要承受上千万上亿流量的,需要另外调整线程数。

这些线程数一般都不是写死的,一种是设置到环境变量里,而更好的方式是在JAVA进程启动的时候,指定参数,将线程数设置进去。

-Dnetty.server.parentgroup.size=2 -Dnetty.server.childgroup.size=4

应用的代码里可以通过

int parentThreadGroupSize = Integer.getInteger("netty.server.parentgroup.size");int childThreadGroupSize = Integer.getInteger("netty.server.childgroup.size");

获取到。

原创粉丝点击