spring - 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 但不起作用。
- 答:不,你不能。我们需要将两个 OutboundGateway 作为 DirectChannel 上的消费者来获得循环分发。每个网关都是一个消费者。
解决方案
FailoverClientConnectionFactory
一种简单的解决方案是使用 2 个实例配置 2 个出站端点。
更改目标工厂的顺序。
FCCF1 -> server1, server2
FCCF2 -> server2, server1
使用相同的通道 ( DirectChannel
) 作为端点的输入通道。
发送到该通道的消息将循环分发到两个端点。
推荐阅读
- javascript - 无法在 React 中访问窗口变量
- python - router registration order influences output
- python-3.x - Why is it necessary to create a conda environment for Tensorflow during installation? Why can't we install Tensorflow through pip?
- scala - 在 Spark Scala 中创建 UDF 以获得单列中的特定格式,而不是 SPARK DATAFRAME AND SQL 中的三个不同列
- javascript - 在 Vue.js 中,如何访问单个文件组件中的函数?
- specman - Specman e:如何从序列中锁定驱动程序的项目队列?
- dart - Flutter setState 在内部 Widget 中
- python - 为什么在 DataFrame 上应用函数(使用“apply”)比在 Series 上快得多?
- google-cloud-platform - 谷歌云数据流:不能杀死工作
- javascript - Jquery 幻灯片在 IE 11 中的显示方式与在 Chrome 中的显示方式不同?