netty bootstrap 使用相同的工作组但无法使用相同的线程

netty bootstrap uses the same workergroup but fails to use the same thread

服务器:

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());

然而,出乎我的意料:clientChannel 处理程序 read 日志打印其 IO 线程是 ntLoopGroup-5-1,而 serverChannel 处理程序 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