使用 Spring 集成 TCP IP 套接字发送和接收数据

Send and receive data using Spring Integration TCP IP socket

我正在创建一个简单的 spring 启动应用程序 (PoC),以使用 Spring 集成 TCP 通过套接字将产品 ID(字符串)从客户端发送到服务器。如果服务器收到正确的产品数据,服务器将返回我需要打印的产品详细信息。只需要建立连接并通过发送适当的数据来获得响应。

请告诉我我应该实施的 classes 是什么? outbound/inboud 网关、消息通道、tcplisteners?我应该使用 xml 配置还是注释?我是 SI 的新手,如果你能给我一个关于如何实施它的想法,我会很有帮助。


这是我更新的集成 xml。

<int-ip:tcp-connection-factory id="client" type="client" host="XX.XX.XX.99" port="9XXX" single-use="true" so-timeout="10000" />

<int:channel id="input"/>

<int-ip:tcp-outbound-gateway id="outGateway" request-channel="input" reply-channel="clientBytes2StringChannel" connection-factory="client" request-timeout="10000" reply-timeout="10000"/>

<int:object-to-string-transformer id="clientBytes2String" input-channel="clientBytes2StringChannel"/> 

<int:service-activator input-channel="clientBytes2StringChannel" ref="echoService" method="test"/> 

<bean id="echoService" class="org.springframework.integration.samples.tcpclientserver.EchoService"/>

<int:channel id="toSA"/>

但这仍然打印回显结果。此外,当我从 main class 调用 abstractClientConnectionfactory 上的 getHost 时,它显示 "localhost"。如何确认连接是否有效?


<int:gateway id="gw"
             service-interface="org.springframework.integration.samples.tcpclientserver.SimpleGateway"
             default-request-channel="input"/>

<int-ip:tcp-connection-factory id="client" type="client" host="xx.xx.xx.99"
                         port="9xxx"
                         single-use="false" so-timeout="300000" using-nio="false"
                         so-keep-alive="true" serializer="byteArrayRawSerializer"
                             deserializer="byteArrayRawSerializer"/>


<bean id="byteArrayRawSerializer" class="org.springframework.integration.ip.tcp.serializer.ByteArrayRawSerializer" />


<int-ip:tcp-outbound-gateway id="outGateway"
                             request-channel="input"
                             reply-channel="responseBytes2StringChannel"
                             connection-factory="client"
                             request-timeout="10000"
                             reply-timeout="10000" />

<int:object-to-string-transformer id="clientBytes2String"
                                  input-channel="responseBytes2StringChannel" output-channel="toSA"/>


<int:service-activator input-channel="toSA" ref="echoService" method="test"/> 

<bean id="echoService" class="org.springframework.integration.samples.tcpclientserver.EchoService"/>

<int:channel id="toSA"/> 

<int:transformer id="errorHandler" input-channel="errorChannel" expression="payload.failedMessage.payload + ':' + payload.cause.message"/>
<int:channel id="responseBytes2StringChannel"></int:channel>

**** 更新 SI xml ****

<int:gateway id="gw"
             service-interface="org.springframework.integration.samples.tcpclientserver.SimpleGateway"
             default-request-channel="objectIn"/>

<int:channel id="objectIn" />

<int-ip:tcp-connection-factory id="client"
                               type="client"
                               host="xx.xx.xx.99"
                               port="9xxx"
                               single-use="true"
                               so-timeout="50000"
                               using-nio="false"
                               so-keep-alive="true"/>
                               <!-- 
                               serializer="byteArrayLengthSerializer"
                               deserializer="byteArrayLengthSerializer"

<bean id="byteArrayLengthSerializer" class="org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer " />
-->
<int:payload-serializing-transformer input-channel="objectIn" output-channel="objectOut"/>

<int-ip:tcp-outbound-gateway id="outGateway"
                             request-channel="objectOut"
                             reply-channel="bytesIn"
                             connection-factory="client"
                             request-timeout="10000"
                             reply-timeout="10000"
                              />

<int:payload-deserializing-transformer input-channel="bytesIn" output-channel="objectOut" />

<int:object-to-string-transformer id="clientBytes2String"
                                  input-channel="objectOut" output-channel="toSA"/>



<int:service-activator input-channel="toSA" ref="echoService" method="test"/> 

<bean id="echoService" class="org.springframework.integration.samples.tcpclientserver.EchoService"/>

<int:channel id="objectOut"/> 
<int:channel id="toSA"/> 
<int:channel id="bytesIn"/> 

我建议你先走文档路线:https://docs.spring.io/spring-integration/docs/current/reference/html/ip.html to investigate what Spring Integration provides for you in regards of TCP/IP. Then it would be great to jump into samples project to see what we suggest for configuration and usage options: https://github.com/spring-projects/spring-integration-samples