Spring集成到不同服务器的多个tcp连接
是否可以设置一个可以同时连接到多个TCP服务器的动态TCP客户端? 我正在使用spring与dsl配置的集成。我已经读了一些关于这个话题的其他问题,但想不出一个办法来解决它 我用于设置以下单个连接的配置:Spring集成到不同服务器的多个tcp连接,tcp,spring-integration,Tcp,Spring Integration,是否可以设置一个可以同时连接到多个TCP服务器的动态TCP客户端? 我正在使用spring与dsl配置的集成。我已经读了一些关于这个话题的其他问题,但想不出一个办法来解决它 我用于设置以下单个连接的配置: @Configuration public class TcpAsyncConfig { MessageHandler handler = new MessageHandler(); @Bean public AbstractClientConnectionFactory client1(
@Configuration
public class TcpAsyncConfig {
MessageHandler handler = new MessageHandler();
@Bean
public AbstractClientConnectionFactory client1() {
return Tcp.netClient("localhost", 6050)
.leaveOpen(true)
.soKeepAlive(true)
.singleUseConnections(true)
.deserializer(new CustomSerializerDeserializer())
.get();
}
@Bean
public IntegrationFlow client1Out(AbstractClientConnectionFactory client1) {
return IntegrationFlows.from(RobotGateways.class)
.handle(Tcp.outboundAdapter(client1))
.get();
}
@Bean
public IntegrationFlow client1In(AbstractClientConnectionFactory client1) {
return IntegrationFlows.from(Tcp.inboundAdapter(client1))
.transform(Transformers.objectToString())
.log(msg -> "client1: " + msg.getPayload())
.handle(handler::handleMessage)
.get();
}
@Bean
@DependsOn("client1Out")
public RobotGateways robotGateways(RobotGateways gateways){
return gateways;
}
}
因此,我想创建一个连接列表,并使用网关在特定连接上发送消息。一个
AbstractClientConnectionFactory
实际上只能连接到一个TCP服务器。要连接到不同的服务器,您需要分别拥有多个Tcp.netClient()
然后可能需要有几个Tcp.outboundAdapter()
和Tcp.inboundAdapter()
定义
在网关流中,要确定发送到哪个客户端,您需要查看route()
,其中根据一些网关调用参数,您可以决定流中的去向
有关更多信息,请参阅文档:
有关为TCP客户端注册动态DSL流并向其路由的示例,请参见此示例。