首页 > 解决方案 > Spring Integration - RoundRobin 和 Failover 的两个永久连接

问题描述

有可能(使用 Spring-Integration 的现有组件)有两个永久连接到两个不同的 TCP 服务器,这允许我们在连接之间进行循环,并且在失败的情况下,尝试使用另一个连接?

我们使用的是FailoverClientConnectionFactory,带有两个TcpNioClientConnectionFactory(每一个连接到不同的服务器)。但这意味着我们的应用程序以 50% 的速度运行。因为它使用共享连接,我们可以循环使用两个 TCP 服务器。

我正在为这种情况而苦苦挣扎。我们被认为是制作自己的 CustomRoundRobinFailoverConnectionFactory 或使用 IntegrationFlow 库,但也许这是一种更好的方法。


编辑 1 - 解决方案

@Bean
@ServiceActivator(inputChannel="outboundChannel")
public MessageHandler outboundGatewayOne() {
    TcpOutboundGateway tcpOutboundGateway = new TcpOutboundGateway();
    tcpOutboundGateway.setConnectionFactory(failoverClientConnectionFactoryOne);
    return tcpOutboundGateway;
}

@Bean
@ServiceActivator(inputChannel="outboundChannel")
public MessageHandler outboundGatewayTwo() {
    TcpOutboundGateway tcpOutboundGateway = new TcpOutboundGateway();
    tcpOutboundGateway.setConnectionFactory(failoverClientConnectionFactoryTwo);
    return tcpOutboundGateway;
}

@Bean
public MessageChannel outboundChannel() {
    return new DirectChannel();
}

@MessagingGateway(defaultRequestChannel="outboundChannel")
public interface TcpClientGateway {
    byte[] send(byte[] message);
}

所以这是有效的。但是现在,我想避免对两个 outboundGateways 进行硬编码。这是一个更好的方法吗?我尝试对两个连接工厂使用相同的 outboundGateway 但不起作用。

标签: springspring-bootspring-integrationspring-dsl

解决方案


FailoverClientConnectionFactory一种简单的解决方案是使用 2 个实例配置 2 个出站端点。

更改目标工厂的顺序。

FCCF1 -> server1, server2
FCCF2 -> server2, server1

使用相同的通道 ( DirectChannel) 作为端点的输入通道。

发送到该通道的消息将循环分发到两个端点。


推荐阅读