使用Spring Boot以及Spring Integration构建的Socket通信客户端演示项目(使用Java配置)
mvn spring-boot:runcurl -X POST -H 'Content-Type: text/plain' -d "消息内容" http://localhost:8080
- 注意在
DemoApplication上增加了@IntegrationComponentScan注解才能正确扫描到@MessagingGateway,使用方法与@ComponentScan相同。 - TCP通信使用到了
org.springframework.integration:spring-integration-ip包,需要在pom.xml中增加。 com.dewafer.demo.server包下的内容为演示使用的远程Socket仿真服务器,该服务器在程序启动时会一起启动。- 配置文件主要参考
com.dewafer.demo.config.IntegrationConfig。 TcpClientGateway使用方法的演示在DemoController中。- 整个Integration flow大致为 http POST request -->
DemoController-->TcpClientGateway-->requestChannel-->tcpOutboundGateway-->tcpInboundChannel-->objectToStringTransformer-->replyChannel-->TcpClientGateway-->DemoController--> http response - 因为flow比较简单没有使用DSL进行配置。
- 所有的Channel皆为
DirectChannel,整个flow在一个thread上完成。 - 因为演示用的Socket仿真服务器使用了
java.io.DataInputStream#readUTF方法,所以为TcpOutboundGateway配置了HEADER_SIZE_UNSIGNED_SHORT的ByteArrayLengthHeaderSerializer,请根据实际情况替换Serializer。请参考相关的官方文档。