我已经构建了以下简单的服务器,我正在使用ab
. 如果我运行ab
3000 个总请求(300 个并发),它就可以工作。如果我再次运行它,它会告诉我:
apr_socket_connect(): Connection reset by peer (54)
如果在此错误之后我尝试使用 curl 发出单个请求而不重新启动服务器,则它可以工作。如果我再次运行ab
它会显示相同的错误。它似乎无法处理太多的并发连接。代码下方:
public static void main(String[] args) throws Exception {
ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(new StringEncoder(), new MyServerHandler());
}
});
bootstrap.bind(new InetSocketAddress(9090));
System.out.println("Running");
}
这是处理程序:
public class MyServerHandler extends SimpleChannelUpstreamHandler {
private static AtomicLong request = new AtomicLong();
@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
throws Exception {
ChannelFuture channelFuture = e.getChannel().write("This is request #" + request.incrementAndGet() + "\n");
channelFuture.addListener(ChannelFutureListener.CLOSE);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
System.out.println(e.getCause());
e.getChannel().close();
}
}
如您所见,它非常简单,它只显示处理的请求总数。有小费吗?
谢谢