有可能(使用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);
}
所以这是有效的。但现在,我想避免对两个界外网关进行硬编码。这是更好的方法吗?我尝试为两个连接工厂使用相同的outboundGateway,但不起作用。
- 回答:不,你不能。我们需要将两个OutboundGateway作为DirectChannel上的消费者来获得循环分发。每个网关都是一个消费者
一个简单的解决方案是用2个FailoverClientConnectionFactory
实例配置2个出站端点。
更改目标工厂的顺序。
FCCF1 -> server1, server2
FCCF2 -> server2, server1
使用与端点的输入通道相同的通道(DirectChannel
(。
发送到该通道的消息将循环分发到两个端点。