【发布时间】:2015-11-05 03:22:37
【问题描述】:
我正在使用netty 4.0编写TCP服务器,可能同时有20k客户端负载。但我的服务器无法承受许多这样的连接。 这是我的代码。
private void initServer(){
EventLoopGroup boosGroup = new NioEventLoopGroup(100);
EventLoopGroup workerGroup = new NioEventLoopGroup(1000);
EventExecutorGroup eegHandle = new DefaultEventExecutorGroup(1000);
EventExecutorGroup eegDecode = new DefaultEventExecutorGroup(1000);
EventExecutorGroup eegEndcode = new DefaultEventExecutorGroup(1000);
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(boosGroup, workerGroup);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeLine = ch.pipeline();
//add check idle time of connection
pipeLine.addLast("Ilde event", new IdleStateHandler(timeIdleRead, timeIdleWrite, 0));
//add idle handler to handle idle connection
pipeLine.addLast("Idle handler", new ServerHandleIdleTime(logger));
//add decode handler to decode message received from client
pipeLine.addLast(eegDecode, new ServerMsgDecoder());
//add business handler to process business
pipeLine.addLast(eegHandle, new ServerHandleSimple());
//add encode handler to encode message send to client
pipeLine.addFirst(eegEncode, new ServerMsgEncoder());
}
});
bootstrap.option(ChannelOption.SO_BACKLOG, 200);
bootstrap.childOption(ChannelOption.SO_KEEPALIVE, false);
// bootstrap.option(ChannelOption.SO_TIMEOUT, 10000);
ChannelFuture channelFuture = bootstrap.bind(host, port);
channelFuture.sync();
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
logger.error("", e);
} finally {
workerGroup.shutdownGracefully();
boosGroup.shutdownGracefully();
}}
我应该像这样使用 3 for 3 处理程序 EventExecutorGroup 吗? 我使用 nthread(= 1000) for workerGroup 是否有足够的 20k 连接? 希望大家帮忙重新配置服务器。 谢谢!
【问题讨论】:
标签: java asynchronous server netty nio