【问题标题】:Connection not found when processing reply GenericMessage处理回复 GenericMessage 时未找到连接
【发布时间】:2021-06-03 19:24:45
【问题描述】:

尝试使用 spring-integration-ip 设置 Spring Boot TCP Server,但是当向服务器发送消息时总是得到错误消息:

2021-06-03 23:58:36.916 ERROR 35752 --- [pool-1-thread-4] o.s.i.ip.tcp.TcpInboundGateway           : Connection not found when processing reply GenericMessage [payload=byte[5], headers={ip_tcp_remotePort=51436, ip_connectionId=localhost:51436:10001:dbc9ca03-a509-4a66-9bbd-020478794bbd, ip_localInetAddress=/0:0:0:0:0:0:0:1, ip_address=0:0:0:0:0:0:0:1, id=3c446dc8-f417-e387-a4e4-42c023da0d4f, ip_hostname=localhost, timestamp=1622746716916}] for GenericMessage [payload=byte[3], headers={ip_tcp_remotePort=51436, ip_connectionId=localhost:51436:10001:dbc9ca03-a509-4a66-9bbd-020478794bbd, ip_localInetAddress=/0:0:0:0:0:0:0:1, ip_address=0:0:0:0:0:0:0:1, id=59caa1c3-20df-57c0-d1dc-81c2b0adfe0d, ip_hostname=localhost, timestamp=1622746716916}]

这是我的 TcpServerConfig 文件:

@Configuration
@EnableIntegration
public class TcpServerConfig {

    @Value("${tcp.server.port}")
    private int port;

    @Bean
    public AbstractServerConnectionFactory serverConnectionFactory() {
        TcpNioServerConnectionFactory serverConnectionFactory = new TcpNioServerConnectionFactory(port);
        serverConnectionFactory.setUsingDirectBuffers(true);
        return serverConnectionFactory;
    }

    @Bean
    public MessageChannel inboundChannel() {
        return new DirectChannel();
    }

    @Bean
    public TcpInboundGateway inboundGateway(AbstractServerConnectionFactory serverConnectionFactory,
                                            MessageChannel inboundChannel) {
        TcpInboundGateway tcpInboundGateway = new TcpInboundGateway();
        tcpInboundGateway.setConnectionFactory(serverConnectionFactory);
        tcpInboundGateway.setRequestChannel(inboundChannel);
        return tcpInboundGateway;
    }
}

和 TcpServerEnpoint 文件:

@Component
@MessageEndpoint
public class TcpServerEndpoint {

    @ServiceActivator(inputChannel = "inboundChannel")
    public byte[] process(byte[] message) {
        System.out.println(Arrays.toString(message));
        return "Hello".getBytes();
    }
}

我正在尝试发出简单的请求 -> 响应 TCP 服务器。发送消息后,它会在调试控制台中打印出来,但也会引发错误。

【问题讨论】:

  • 有没有机会让你的那个简单的 Spring Boot 项目在我们这边玩和复制?
  • 这是一个简单的tcp服务器,只有2个文件和maven依赖列表中的spring-integration-ip。就是这样。
  • 好吧,但您展示的内容仍然必须开箱即用。如果没有,那就有问题了。因此,如果我们在同一环境中调查问题,那就太好了。例如:你如何测试这个解决方案真的很重要。感谢理解。
  • @ArtemBilan 项目很简单,OS是MacOS,放到GitHub上github.com/RussellSk/spring-tcp-server
  • 我尝试将我的 Netty 项目移动到 spring-integration-ip。感谢您的帮助,我知道问题可能出在序列化器和反序列化器中,但不知道该怎么办。我在 Netty 项目中使用此命令并尝试使用 spring-integration-ip 来实现它,命令示例:echo -ne "\x01\x02" |数控本地主机 8080 | hexdump -v -e "%02X"

标签: java spring-boot spring-integration spring-integration-ip


【解决方案1】:

这是我为你准备的:

@SpringBootApplication
public class So67827589Application {

    public static void main(String[] args) {
        SpringApplication.run(So67827589Application.class, args);
    }

    @Bean
    public AbstractServerConnectionFactory serverConnectionFactory() {
        TcpNioServerConnectionFactory serverConnectionFactory = new TcpNioServerConnectionFactory(7777);
        serverConnectionFactory.setUsingDirectBuffers(true);
        return serverConnectionFactory;
    }

    @Bean
    public MessageChannel inboundChannel() {
        return new DirectChannel();
    }

    @Bean
    public TcpInboundGateway inboundGateway(AbstractServerConnectionFactory serverConnectionFactory,
            MessageChannel inboundChannel) {

        TcpInboundGateway tcpInboundGateway = new TcpInboundGateway();
        tcpInboundGateway.setConnectionFactory(serverConnectionFactory);
        tcpInboundGateway.setRequestChannel(inboundChannel);
        return tcpInboundGateway;
    }

    @ServiceActivator(inputChannel = "inboundChannel")
    public byte[] process(byte[] message) {
        System.out.println(Arrays.toString(message));
        return "Hello".getBytes();
    }

}

然后我去命令行做:

telnet localhost 7777

然后输入任何内容并返回Hello。然后我可以打字越来越多 - Hello 会回到我身边。永远!

所以,一切都按预期进行。 Spring Boot 2.5.0.

【讨论】:

  • 提供的解决方案给出了相同的结果但有错误。我想应该有 outboundChannel 和 inboundChannel。
  • 嗯,您确实需要确保您的客户端发送的分隔符正是服务器所期望的。 telnet util 发送一个\n\r,这是 Spring Integration 中那些连接工厂的默认值。请参阅有关序列化程序的文档:docs.spring.io/spring-integration/docs/current/reference/html/…
  • 请写下您的答案(不在评论中),以便我将其标记为已接受。解决我的问题,谢谢。
  • 好吧,我们现在就在你的答案中:答案的 cmets 增加了它的问题。您的问题是关于 Spring Integration 不起作用 - 我的原始答案证明相反。与客户的不兼容是你一开始没有提到的另一个故事。但是,是的,够了。很高兴您解决了问题!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-16
  • 2019-09-20
  • 1970-01-01
  • 1970-01-01
  • 2021-03-06
相关资源
最近更新 更多