http - 我的 netty 程序有时会出现“对等连接重置”之类的异常
问题描述
我正在使用 netty-all-4.1.0.Final.jar 编写 http 服务器,在服务器侦听时,有时会发生“对等方重置连接”,详细信息如下:
Connection reset by peer
java.io.IOException: Connection reset by peer
at sun.nio.ch.FileDispatcherImpl.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
at sun.nio.ch.IOUtil.read(IOUtil.java:192)
at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)
at io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:288)
at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1098)
at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:350)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:112)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:544)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:485)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:399)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:371)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:742)
at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:145)
at java.lang.Thread.run(Thread.java:748)
这是我的主要服务器代码:
public static void main(String[] args) throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new HttpServerCodec());
//将同一个http请求的内容组装到一起,调用一次handler
pipeline.addLast(new HttpObjectAggregator(65536));
pipeline.addLast(new HttpServerHandler());
}
});
int port = ConfUtils.port;
ChannelFuture f = b.bind(port).sync();
logger.info("start lisner-------port is {}",port);
f.channel().closeFuture().sync();
} catch(Exception e){
logger.error(e.getMessage(),e);
}
finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
这是我的处理程序类:
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if(msg instanceof FullHttpRequest){
try{
logger.info("accept http msg!");
FullHttpRequest fullHttpRequest = (FullHttpRequest)msg;
ProcessMsg.dealMsg(fullHttpRequest);
logger.info("full header is {}",fullHttpRequest);
String responseHtml = "response from server!";
byte[] responseBytes = responseHtml.getBytes("UTF-8");
int contentLength = responseBytes.length;
FullHttpResponse response = new
DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
HttpResponseStatus.OK,Unpooled.wrappedBuffer(responseBytes));
response.headers().set("Content-Type", "text/html; charset=utf-8");
response.headers().set("Content-Length", Integer.toString(contentLength));
Thread.sleep(1000 * 5);
ctx.writeAndFlush(response);
ctx.flush();
logger.info("response from server!");
}catch(Exception e){
logger.error(e.getMessage(),e);
}
}
else{
logger.info("this is not http request");
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
LoggerFactory.getLogger("excplog").error(cause.getMessage(),cause);
ctx.close();
}
}
它并不总是发生,有时会发生,大多数时候不会,而且我们找不到任何关于请求的问题
解决方案
推荐阅读
- coldfusion - shib登录coldfusion后重定向
- node.js - MongoError: E11000 尝试登录时重复键错误收集
- python - 如果满足 for 循环中的条件,则在同一行中的其他单元格中获取值
- redis - 如何在不使用 SPOP 的情况下以原子方式从 REDIS SET 中弹出多个项目
? - reactjs - firebase 和 react 项目中的各种请求
- python - 使用python远程触发jenkins作业后如何获取作业的构建状态和控制台输出
- azure-keyvault - 在 AppSettin 中指定 Key Vault URI?
- r - 使用“melt”将 R 中的数据重塑为“long”
- html - 如何仅在本机 html 视频元素中显示全屏按钮?
- java - Spring WebFlux:合并 Mono 和 Flux / 将 Mono 放入 Flux?