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

问题描述 投票:0回答:2

我对 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();
}

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

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

在上面的处理程序中,我故意通过执行 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 线程数为 2*num_logic_processors)?

谢谢,

netty
2个回答
4
投票

Netty 的创建者表示,如果我们共享的话,多个 boss 线程会很有用 不同服务器引导程序之间的 NioEventLoopGroup,但我没有看到 其原因。

正如 Norman Maurer 所说,这不是必需的,但非常有用。

如果您将 1 个线程用于 2 个不同的引导程序,则意味着您无法同时处理与该引导程序的连接。因此,在非常非常糟糕的情况下,当 boss 线程仅处理一个引导程序的连接时,永远不会处理与另一个引导程序的连接。

工人也一样

EventLoopGroup


2
投票

当你bind()一个新端口时,Netty将一个新的ServerSocketChannel注册到bossGroup中的next()事件循环,所以我认为多线程boss可能会在多个端口应用程序上使用。

© www.soinside.com 2019 - 2024. All rights reserved.