Netty4-TCP服务器-使用Camel进行基本测试



我是一个初学者,正在学习Camel,并尝试使用apache蓝图在Camel上运行netty4。我正在使用netty:创建一个TCP服务器

  <camelContext xmlns="http://camel.apache.org/schema/blueprint">
    <route id="timerToLog">
      <from uri="netty4:tcp://localhost:5150"/>
      <log message="The message contains ${body}"/>
      <to uri="mock:result"/>
    </route>
  </camelContext>

当我使用mvn:运行camel时,一切都很好

[         Blueprint Extender: 1] TCPNettyServerBootstrapFactory INFO  ServerBootstrap binding to localhost:5150
[         Blueprint Extender: 1] NettyConsumer                  INFO  Netty consumer bound to: localhost:5150
[         Blueprint Extender: 1] BlueprintCamelContext          INFO  Route: timerToLog started and consuming from: Endpoint[tcp://localhost:5150]

我使用Hercules连接到tcp服务器并发送数据(甚至尝试了windows telnet),一旦我发送"Hello"ascii文本,连接就会关闭,并出现以下错误:

[d #0 - NettyEventExecutorGroup] NettyConsumer                  WARN  Closing channel as an exception was thrown from Netty. Caused by: [io.netty.hand
ler.codec.TooLongFrameException - Adjusted frame length exceeds 1048576: 1212501072 - discarded]
io.netty.handler.codec.TooLongFrameException: Adjusted frame length exceeds 1048576: 1212501072 - discarded
        at io.netty.handler.codec.LengthFieldBasedFrameDecoder.fail(LengthFieldBasedFrameDecoder.java:499)
        at io.netty.handler.codec.LengthFieldBasedFrameDecoder.failIfNecessary(LengthFieldBasedFrameDecoder.java:477)
        at io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:403) ... 

我只是通过插座发送一个"你好",但它仍然显示帧长度超过了!我知道我错过了一些非常基本的东西。请耐心等待并帮助:)

根据Camel Netty4组件文档,若url的文本行参数为NULL,则安装并使用默认编码器/解码器。这相当于使用默认行为textline=false

在您的示例中,netty4组件有以下内容:

 <from uri="netty4:tcp://localhost:5150"/>

因此,您使用的默认编码器/解码器可能不是基于文本行的编码器/解码器。你可能会在文档中找到默认的编码器,但我认为通过找到这些信息,你可能会比我学到更多。

请记住,Netty使用编码器和解码器的概念来向客户端和服务器发送数据以及从客户端和服务器接收数据。这些解码器定义了Netty应该如何从套接字读取原始数据。如果你不熟悉这一点,Netty上有大量的文档可用。

让我们看看如何实现您想要实现的简单的基于文本行的协议编码器/解码器。

<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route id="timerToLog">
  <from uri="netty4:tcp://localhost:5150?textline=true"/>
  <log message="The message contains ${body}"/>
  <to uri="mock:result"/>
</route>

你所缺少的只是编码器位。

最新更新