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

为什么我们真的需要多个netty boss线程?,netty,Netty,对于boss组的线程数,我真的很困惑。我想不出我们需要多个boss线程的场景。在《Netty的创建者》一书中,他说如果我们在不同的服务器引导之间共享NioEventLoopGroup,那么多个boss线程是有用的,但我看不出原因 考虑这个简单的Echo服务器: public class EchoServer { private final int port; private List<ChannelFuture> channelFutures = new ArrayList<

对于boss组的线程数,我真的很困惑。我想不出我们需要多个boss线程的场景。在《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();
}
在上面的处理程序中,我通过while(true)故意让通道保持繁忙;现在,如果我用参数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线程,我能够通过两个服务器引导连接到两个端口。那么,为什么我们需要多个boss线程(默认情况下,boss线程的数量是2*num\u逻辑处理器)

谢谢

Netty的创建者说,如果我们共享多个boss线程,那么多个boss线程是有用的 NioEventLoopGroup在不同的服务器引导之间,但我看不到 原因是什么

正如诺曼·莫勒(Norman Maurer)所说,这不是必要的,但非常有用

如果您将1个线程用于2个不同的引导,这意味着您无法同时处理到此引导的连接。所以在非常糟糕的情况下,当boss线程只处理一个引导的连接时,到另一个引导的连接将永远不会被处理


workers
EventLoopGroup

也一样,所以一个boss线程可以处理多个连接,但不能同时处理?而且,拥有多个boss线程是很有用的(例如,在一个web服务器中,我们希望同时有多个连接?)此外,我的理解或工作线程是否正确?谢谢。@BestCoderEver boss线程处理连接并将处理传递给工作线程。是的,有多个boss线程是很有用的。但不需要太多,因为在普通情况下,worker线程比boss线程工作的时间更长。如果答案有帮助,请将其标记为已接受。
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