Netty Bootstrap 使用相同的 workergroup,但无法使用相同的线程



服务器:

EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(8);
try {
ServerBootstrap b = new ServerBootstrap();
b.option(ChannelOption.SO_BACKLOG, 1024);
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)

客户:

Bootstrap b = new Bootstrap();
b.group(workerGroup)
.channel(NioSocketChannel.class)
...
clientChannel = b.connect(host, port);

当服务器通道处理程序read包时,它会通过clientChannel向另一台服务器发出请求,cleintChannel.writeAndFlush(msg->newMsg());

但是,出乎我的意料:客户端通道处理程序read日志打印其 IO 线程是ntLoopGroup-5-1,而服务器通道处理程序read日志打印其 IO 线程ntLoopGroup-5-2

我希望通过使用netty共享事件循环,该程序可以具有较低的上下文切换速率。

您可以通过将acceptedChannel.eventLoop()用作传递到Bootstrapgroup(...)的组来执行此操作。这正是我们在 HexDump 示例中所做的:

https://github.com/netty/netty/blob/netty-4.1.25.Final/example/src/main/java/io/netty/example/proxy/HexDumpProxyFrontendHandler.java#L47

最新更新