Spring 集成到不同服务器的多个 tcp 连接

Spring integration multiple tcp connections to different servers

是否可以设置一个可以同时连接到多个 TCP 服务器的动态 TCP 客户端? 我正在使用 spring 与 dsl 配置的集成。我已经阅读了有关此主题的其他一些问题,但无法找到使它起作用的方法。

下面我用来设置单个连接的配置:

@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() 根据一些网关调用参数,您可以在其中决定流程中的去向。

有关详细信息,请参阅文档:

https://docs.spring.io/spring-integration/docs/current/reference/html/dsl.html#java-dsl-routers https://docs.spring.io/spring-integration/docs/current/reference/html/message-routing.html#messaging-routing-chapter