【问题标题】:Spring integration multiple tcp connections to different serversSpring集成到不同服务器的多个tcp连接
【发布时间】:2021-01-22 12:54:35
【问题描述】:

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

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

@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;
}
}

所以我想创建一个连接列表并使用网关在特定连接上发送消息。

【问题讨论】:

    标签: tcp spring-integration


    【解决方案1】:

    一个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

    【讨论】:

    猜你喜欢
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 2017-01-27
    • 2016-09-23
    • 2012-07-30
    • 1970-01-01
    相关资源
    最近更新 更多