gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:48:03 25 4
gpt4 key购买 nike

我真的很困惑老板组的线程数。我想不出我们需要多个老板线程的情况。在 do we need more than a single thread for boss group? Netty 的创建者说,如果我们在不同的服务器 Bootstrap 之间共享 NioEventLoopGroup,则多个老板线程很有用,但我不明白其中的原因。

考虑这个简单的 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,并将两个事件组共享给绑定(bind)到两个不同端口(例如 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();
}
}

在上面的处理程序中,我故意通过执行 while(true); 来保持 channel 忙碌。现在,如果我使用参数 9000 启动我的应用程序,它将创建两个绑定(bind)在端口 9000 和 9001 的服务器 Bootstrap 。
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 个工作线程并保持它们的 channel 繁忙而不关闭它:
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

我不明白的是,我有一个老板线程,我可以用两个服务器 Bootstrap 连接到两个端口。那么为什么我们需要多个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 ,则意味着您无法同时处理与此 Bootstrap 的连接。所以在非常非常糟糕的情况下,当老板线程只处理一个 Bootstrap 的连接时,将永远不会处理与另一个 Bootstrap 的连接。

worker 也一样 EventLoopGroup .

关于netty - 为什么我们真的需要多个 netty boss 线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34275138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com