为什么我们真的需要多个netty boss线程?

我真的很担心老板组的线程数量.我无法弄清楚我们需要多个boss线程的场景.在
do we need more than a single thread for boss group?中,Netty的创建者说如果我们在不同的服务器引导之间共享NioEventLoopGroup,那么多个boss线程是有用的,但是我没有看到它的原因.

考虑这个简单的Echo服务器:

public class EchoServer {

private final int port;
private List<ChannelFuture> channelFutures = new ArrayList<ChannelFuture>(2);

public EchoServer(int port) {
    this.port = port;
}

public void start() throws Exception {

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup(4);

    for (int i = 0; i != 2; ++i) {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class) // the channel type
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch)
                            throws Exception {
                        System.out.println("Connection accepted by server");
                        ch.pipeline().addLast(
                                new EchoServerHandler());
                    }
                });

        // wait till binding to port completes
        ChannelFuture f = b.bind(port + i).sync();
        channelFutures.add(f);
        System.out.println("Echo server started and listen on " + f.channel().localAddress());
    }

    for (ChannelFuture f : channelFutures)
        f.channel().closeFuture().sync();

    // close gracefully
    workerGroup.shutdownGracefully().sync();
    bossGroup.shutdownGracefully().sync();
}

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println(
                "Usage: " + EchoServer.class.getSimpleName() +
                        " <port>");
        return;
    }
    int port = Integer.parseInt(args[0]);
    new EchoServer(port).start();
}

在上面的示例中,我创建了一个具有1个线程的bossGroup和具有4个线程的workerGroup,并将两个事件组共享到绑定到两个不同端口(例如9000和9001)的两个不同的bootstrap.以下是我的处理程序:

@ChannelHandler.Sharable
public class EchoServerHandler extends ChannelInboundHandlerAdapter {

@Override
public void channelRead(ChannelHandlerContext ctx,
                        Object msg)  throws Exception  {
    ByteBuf in = (ByteBuf) msg;
    System.out.println("Server received: " + in.toString(CharsetUtil.UTF_8) + " from channel " + ctx.channel().hashCode());
    ctx.write(in);
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
    System.out.println("Read complete for channel " + ctx.channel().hashCode());
    // keep channel busy forever
    while(true); 
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx,
                            Throwable cause) {
    cause.printStackTrace();
    ctx.close();
}
}

在上面的处理程序中,我故意保持通道忙碌(真实);现在,如果我使用参数9000启动我的应用程序,它将创建两个绑定在端口9000和9001的服务器引导程序.

Echo server started and listen on /0:0:0:0:0:0:0:0:9090
Echo server started and listen on /0:0:0:0:0:0:0:0:9091

现在,如果我连接到两个端口并开始发送数据,则可以接收的最大连接数为4,这是有道理的,因为我创建了4个工作线程并保持其信道忙而不关闭它:

echo 'abc' > /dev/tcp/localhost/9000
echo 'def' > /dev/tcp/localhost/9000
echo 'ghi' > /dev/tcp/localhost/9001
echo 'jkl' > /dev/tcp/localhost/9000
echo 'mno' > /dev/tcp/localhost/9001 # will not get connected

你也可以这样做:

telnet localhost 9000 -> then send data "abc"
telnet localhost 9000 -> then send data "def"
telnet localhost 9001 -> then send data "ghi"
telnet localhost 9000 -> then send data "jkl"
telnet localhost 9001 -> # will not get connected

我不明白的是,我有一个boss线程,我可以连接两个端口,两个服务器bootstraps.那么为什么我们需要不止一个boss线程(默认情况下,boss线程的数量是2 * num_logical_processors)?

谢谢,

最佳答案

the creator of Netty says multiple boss threads are useful if we share
NioEventLoopGroup between different server bootstraps, but I don’t see
the reason for it.

正如Norman Maurer所说,这没有必要,但它非常有用.

如果您使用1个线程进行2个不同的bootstrap,则意味着您无法同时处理与此bootstraps的连接.所以在非常糟糕的情况下,当boss线程只处理一个引导程序的连接时,永远不会处理与另一个引导程序的连接.

对于工作者EventLoopGroup也是如此.

点赞