Netty 4 发出警告“丢弃了到达管道末端的 1 条入站消息。请检查您的管道配置”。这是什么意思?应该如何处理? (以前是 reproduced here,直到根据接受的答案得到解决,但我宁愿对它的含义以及管道的工作原理有一般性的解释)
为了最大化网络反馈,客户端管道设置如下:
pipeline.addLast("logger", new LoggingHandler(LogLevel.TRACE))
pipeline.addLast("HttpRequestEncoder", new HttpClientCodec)
pipeline.addLast("handler", new myHandler)
当 Netty 发送两条 http 消息并成功接收并被服务器端确认时,我在客户端登录的所有内容是:
12 [main] DEBUG io.netty.util.internal.InternalLoggerFactory - Using Log4J as the default logging framework
164 [nioEventLoopGroup-1-2] DEBUG io.netty.channel.nio.SelectorUtil - Using select timeout of 500
164 [nioEventLoopGroup-1-2] DEBUG io.netty.channel.nio.SelectorUtil - Epoll-bug workaround enabled = false
229 [nioEventLoopGroup-1-2] WARN io.netty.channel.DefaultChannelPipeline - Discarded 1 inbound message(s) that reached at the end of the pipeline. Please check your pipeline configuration.
230 [nioEventLoopGroup-1-2] WARN io.netty.channel.DefaultChannelPipeline - Discarded 1 inbound message(s) that reached at the end of the pipeline. Please check your pipeline configuration.
而日志记录是这样设置的:
BasicConfigurator.configure
InternalLoggerFactory.setDefaultFactory(new Log4JLoggerFactory)
最佳答案
在 Netty 4 中,用于服务器或客户端的 HTTP 解码器总是为每个 HTTP 消息生成多个消息对象:
1 * HttpRequest / HttpResponse
0 - n * HttpContent
1 * LastHttpContent
换句话说:
- 服务器接收 1 个 HttpRequest、0-n 个 HttpContent 和 1 个 HttpLastContent
- 客户端收到 1 个 HttpResponse、0-n 个 HttpContent 和 1 个 HttpLastContent。
因此,如果您的处理程序仅使用 HttpRequest/HttpResponse,则其他消息将到达管道的末端。您需要使用它们,这是您的管道“配置错误”的地方。
此外,您可以将 HttpObjectAggregator 添加到您的管道中,以便生成 FullHttpRequest/FullHttpResponse 消息:
pipeline.addLast( "http-aggregator", new HttpObjectAggregator( MAX_SIZE ) );
但这意味着整个请求或响应,包括正文实体,在您的处理程序被调用之前加载。也许你不想要那个,YMMV。
关于Netty 管道警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15242793/