Netty 10000 同时连接
Netty 10000 connections at the same time
我正在尝试使用 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 实例。否则你会创建很多线程。
我正在尝试使用 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 实例。否则你会创建很多线程。