Netty 管道警告

标签 netty

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/

相关文章:

java - 为什么 HttpStaticFileServer 的 netty 示例使用 RandomAccessFile?

java - 有没有办法从一个 Netty channel 读取数据的速度与写入另一个 channel 的速度一样快?

java - netty 4 如何从服务器发送消息

java - 如何在Netty中使用带有UDP协议(protocol)的protobuf有效负载?您能给我一个使用自定义 protobuf 协议(protocol)的示例吗?

java - JConsole 总加载类行为

java - Netty 中的多个处理程序与单个处理程序

java - 简单的 Netty Http-Server 只返回 "Hello World"

java - 像 getNames() 这样的 Netty ChannelPipeline 方法是否保证按 "proper"顺序给出结果?

netty - 通过 Netty 的 Channel.write() 发送的消息在开始发送到网络时会保留它们的顺序吗?

java - messageReceived (netty) 未调用