我真的很困惑老板组的线程数量。我无法弄清楚我们需要多个老板线程的场景。在Boss 组是否需要多个线程? https://stackoverflow.com/questions/22280916/do-we-need-more-than-a-single-thread-for-boss-groupNetty 的创建者说,如果我们在不同的服务器引导程序之间共享 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)?
Thanks,