gpt4 book ai didi

java - Netty 10000 同时连接

转载 作者:行者123 更新时间:2023-12-04 21:28:02 24 4
gpt4 key购买 nike

我正在尝试使用 Netty 模拟 10000 个客户端同时连接到服务器。当 956 客户端连接到服务器时一切正常,但 957 客户端导致错误异常。

注意:我在同一台机器上运行服务器和客户端(win7 8GB ram,i7-CPU)

错误:

java.lang.IllegalStateException: failed to create a child event loop
io.netty.channel.ChannelException: failed to open a new selector

我的代码:

try {
con.connect();
} catch (Exception e) {
logger.error("Client: error connect to ip {} and port {}, ",id, ip, port,e);
return;
}

connect方法的代码是:

public void connect() {
workerGroup = new NioEventLoopGroup();
Bootstrap bs = new Bootstrap();
bs.group(workerGroup).channel(NioSocketChannel.class);
bs.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("idleStateHandler", new IdleStateHandler(0, 0, 300));
ch.pipeline().addLast("idleStateActionHandler", new IdleStateEventHandler());
ch.pipeline().addLast("logger", new LoggingHandler());
ch.pipeline().addLast("commandDecoder", new CommandDecoder());
ch.pipeline().addLast("commandEncoder", new CommandEncoder());
}
});

最佳答案

您应该为每个连接调用使用相同的 NioEventLoopGroup 实例。否则你会创建很多线程。

关于java - Netty 10000 同时连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31942647/

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