Spring integration FailoverClient连接工厂:已连接故障转移服务器,但仍在尝试连接主服务器

Spring integration FailoverClient连接工厂:已连接故障转移服务器,但仍在尝试连接主服务器,spring-integration,Spring Integration,如果主服务器和故障转移服务器之间的连接被中断,并且也未启动,则我会遇到问题。一段时间后,故障转移服务器启动,并成功建立与服务器故障转移的连接。但它仍然试图连接主服务器,这是期望的行为吗 连接工厂的代码段: List processServers=mConfigurationService.getProcessServers(); 列表工厂=新的ArrayList() 这里是日志,我们可以看到连接是用10.90.x.F建立的,但它仍在尝试连接10.90.x.p 2020-02-27 16:33:5

如果主服务器和故障转移服务器之间的连接被中断,并且也未启动,则我会遇到问题。一段时间后,故障转移服务器启动,并成功建立与服务器故障转移的连接。但它仍然试图连接主服务器,这是期望的行为吗

连接工厂的代码段:

List processServers=mConfigurationService.getProcessServers(); 列表工厂=新的ArrayList()

这里是日志,我们可以看到连接是用10.90.x.F建立的,但它仍在尝试连接10.90.x.p

2020-02-27 16:33:53[task-scheduler-10]调试 o、 s.i.i.t.c.TcpNetClientConnectionFactory-打开新插座 连接到10.90.x.P:42027 2020-02-27 16:33:53[任务调度器-2] 调试 o、 s.i.i.t.c.FailoverClient连接工厂$FailoverCPConnection- org.springframework.integration.ip.tcp.connection。TcpNetClientConnectionFactory@4946485c, 主机=10.90.x.P,端口=42027失败,出现java.net.ConnectException: 连接超时:连接,正在尝试另一个2020-02-27 16:33:53 [task-scheduler-2]调试 o、 s.i.i.t.c.FailoverClient连接工厂$FailoverCPConnection- 得到 10.90 x.F:42027:65437:1f582e09-eb9a-4966-8fd2-60cb156ad015 从…起 org.springframework.integration.ip.tcp.connection。TcpNetClientConnectionFactory@1df06ecd, 主机=10.90.x.F,端口=42027 2020-02-27 16:33:53[任务调度器-2] 调试o.s.i.i.t.c.ClientModeConnectionManager-连接 bae635ce-c643-4650-ba15-5fb2fe8af053:1已建立2020-02-27 16:34:14 [task-scheduler-10]调试 o、 s.i.i.t.c.FailoverClient连接工厂$FailoverCPConnection- org.springframework.integration.ip.tcp.connection。TcpNetClientConnectionFactory@4946485c, 主机=10.90.x.P,端口=42027失败,出现java.net.ConnectException: 连接超时:连接,正在尝试另一个2020-02-27 16:34:14 [task-scheduler-7]调试o.s.i.i.t.c.TcpNetClientConnectionFactory- 打开新的插座连接至10.90.x.P:42027 2020-02-27 16:34:14 [task-scheduler-10]调试 o、 s.i.i.t.c.FailoverClient连接工厂$FailoverCPConnection- 得到 10.90 x.F:42027:65437:1f582e09-eb9a-4966-8fd2-60cb156ad015 从…起 org.springframework.integration.ip.tcp.connection。TcpNetClientConnectionFactory@1df06ecd, 主机=10.90.x.F,端口=42027 2020-02-27 16:34:14[任务调度器-10] 调试o.s.i.i.tcp.TcpSendingMessageHandler-获取连接 7a451368-13b2-4abf-908e-3cfe0fca2148:1

2020-02-27 16:34:35[task-scheduler-7]调试 o、 s.i.i.t.c.FailoverClient连接工厂$FailoverCPConnection- org.springframework.integration.ip.tcp.connection。TcpNetClientConnectionFactory@4946485c, 主机=10.90.x.P,端口=42027失败,出现java.net.ConnectException: 连接超时:连接,正在尝试另一个2020-02-27 16:34:35 [task-scheduler-7]调试 o、 s.i.i.t.c.FailoverClient连接工厂$FailoverCPConnection- 得到 10.90 x.F:42027:65437:1f582e09-eb9a-4966-8fd2-60cb156ad015 从…起 org.springframework.integration.ip.tcp.connection。TcpNetClientConnectionFactory@1df06ecd, 主机=10.90.x.F,端口=42027

带有示例项目的新日志:

2020-03-04 21:57:39.027  INFO 76636 --- [           main] .s.i.i.t.c.TcpNetServerConnectionFactory : started server2, port=1235
Hit enter to start server1
2020-03-04 21:57:39.029  INFO 76636 --- [pool-1-thread-1] .s.i.i.t.c.TcpNetServerConnectionFactory : server2, port=1235 Listening
2020-03-04 21:57:39.081 DEBUG 76636 --- [ask-scheduler-1] .s.i.i.t.c.TcpNetClientConnectionFactory : client2: Added new connection: localhost:1235:52400:26295f67-0ebf-45bd-b7f9-ad7d6a08ffbf
2020-03-04 21:57:39.084 DEBUG 76636 --- [ask-scheduler-1] tConnectionFactory$FailoverTcpConnection : Got localhost:1235:52400:26295f67-0ebf-45bd-b7f9-ad7d6a08ffbf from client2, host=localhost, port=1235
ee1cbb1c-0ae0-49b3-969a-38cf28ed6aea:1
2:[B@53ec43e4
ee1cbb1c-0ae0-49b3-969a-38cf28ed6aea:2
2:[B@1da20d77
ee1cbb1c-0ae0-49b3-969a-38cf28ed6aea:3
2:[B@6acde81
2020-03-04 21:57:54.096 DEBUG 76636 --- [ask-scheduler-3] .s.i.i.t.c.TcpNetClientConnectionFactory : Opening new socket connection to localhost:1234
2020-03-04 21:57:56.102 DEBUG 76636 --- [ask-scheduler-3] tConnectionFactory$FailoverTcpConnection : client1, host=localhost, port=1234 failed with java.net.ConnectException: Connection refused: connect, trying another
2020-03-04 21:57:56.103 DEBUG 76636 --- [ask-scheduler-3] tConnectionFactory$FailoverTcpConnection : Got localhost:1235:52400:26295f67-0ebf-45bd-b7f9-ad7d6a08ffbf from client2, host=localhost, port=1235
8bfe4fab-1931-4565-af56-f162252f972b:1
2:[B@46fd87cc
8bfe4fab-1931-4565-af56-f162252f972b:2
2:[B@77bf1f94

谢谢你的耐心;我复制了它

这是一只虫子

编辑

控制此行为的新属性:

/**
 * When using a shared connection {@link #setSingleUse(boolean) singleUse} is false,
 * specify how long to wait before trying to fail back to start from the beginning of
 * the factory list. Default is 0 for backwards compatibility to always try to get a
 * connection to the primary server. If you don't want to fail back until the current
 * connection is closed, set this to {@link Long#MAX_VALUE}.
 * Cannot be changed when using {@link CachingClientConnectionFactory} delegates.
 * @param refreshSharedInterval the interval in milliseconds.
 * @since 4.3.22
 * @see #setSingleUse(boolean)
 * @see #setCloseOnRefresh(boolean)
 */
public void setRefreshSharedInterval(long refreshSharedInterval) {
    Assert.isTrue(!this.cachingDelegates,
            "'refreshSharedInterval' cannot be changed when using 'CachingClientConnectionFactory` delegates");
    this.refreshSharedInterval = refreshSharedInterval;
}

/**
 * When using a shared connection {@link #setSingleUse(boolean) singleUse} is false,
 * set this to true to close the old shared connection after a refresh. If this is
 * false, the connection will remain open, but unused until its connection factory is
 * again used to get a connection. Default is false for backwards compatibility.
 * Cannot be changed when using {@link CachingClientConnectionFactory} delegates.
 * @param closeOnRefresh true to close.
 * @since 4.3.22
 * @see #setSingleUse(boolean)
 * @see #setRefreshSharedInterval(long)
 */
public void setCloseOnRefresh(boolean closeOnRefresh) {
    Assert.isTrue(!this.cachingDelegates,
            "'closeOnRefresh' cannot be changed when using 'CachingClientConnectionFactory` delegates");
    this.closeOnRefresh = closeOnRefresh;
}
Hit enter to start server2
<enter>
2020-03-03 09:25:45.143  INFO 79176 --- [           main] .s.i.i.t.c.TcpNetServerConnectionFactory : started server2, port=1235
Hit enter to start server1
2020-03-03 09:25:45.144  INFO 79176 --- [pool-1-thread-1] .s.i.i.t.c.TcpNetServerConnectionFactory : server2, port=1235 Listening
2020-03-03 09:25:47.363 DEBUG 79176 --- [ask-scheduler-2] .s.i.i.t.c.TcpNetClientConnectionFactory : Opening new socket connection to localhost:1234
2020-03-03 09:25:47.368 DEBUG 79176 --- [ask-scheduler-2] tConnectionFactory$FailoverTcpConnection : client1, host=localhost, port=1234 failed with java.net.ConnectException: Connection refused (Connection refused), trying another
2020-03-03 09:25:47.368 DEBUG 79176 --- [ask-scheduler-2] .s.i.i.t.c.TcpNetClientConnectionFactory : Opening new socket connection to localhost:1235
2020-03-03 09:25:47.369 DEBUG 79176 --- [ask-scheduler-2] .s.i.i.t.c.TcpNetClientConnectionFactory : client2: Added new connection: localhost:1235:52775:31caf4d1-0fd1-49bf-8be6-f9fa935bd8a0
2020-03-03 09:25:47.371 DEBUG 79176 --- [ask-scheduler-2] tConnectionFactory$FailoverTcpConnection : Got localhost:1235:52775:31caf4d1-0fd1-49bf-8be6-f9fa935bd8a0 from client2, host=localhost, port=1235
618d04e0-aa3a-4bac-966c-982d93528dd9:1
2:[B@47858489
2020-03-03 09:25:52.373 DEBUG 79176 --- [ask-scheduler-1] .s.i.i.t.c.TcpNetClientConnectionFactory : Opening new socket connection to localhost:1234
2020-03-03 09:25:52.374 DEBUG 79176 --- [ask-scheduler-1] tConnectionFactory$FailoverTcpConnection : client1, host=localhost, port=1234 failed with java.net.ConnectException: Connection refused (Connection refused), trying another
2020-03-03 09:25:52.374 DEBUG 79176 --- [ask-scheduler-1] tConnectionFactory$FailoverTcpConnection : Got localhost:1235:52775:31caf4d1-0fd1-49bf-8be6-f9fa935bd8a0 from client2, host=localhost, port=1235
51b1c8ab-15c6-4108-acb7-b34584bf8506:1
2:[B@79bc08ef
2020-03-03 09:25:57.378 DEBUG 79176 --- [ask-scheduler-2] .s.i.i.t.c.TcpNetClientConnectionFactory : Opening new socket connection to localhost:1234
2020-03-03 09:25:57.378 DEBUG 79176 --- [ask-scheduler-2] tConnectionFactory$FailoverTcpConnection : client1, host=localhost, port=1234 failed with java.net.ConnectException: Connection refused (Connection refused), trying another
2020-03-03 09:25:57.379 DEBUG 79176 --- [ask-scheduler-2] tConnectionFactory$FailoverTcpConnection : Got localhost:1235:52775:31caf4d1-0fd1-49bf-8be6-f9fa935bd8a0 from client2, host=localhost, port=1235
3c49c6f7-fe6a-410b-acff-cfa20aba42b1:1
2:[B@774d8ab2
EDIT2

除此之外,我还使用我编写的应用程序测试了修复程序,以重现您的问题:

@springboot应用程序
@使能调度
公共类SO60432039应用程序{
公共静态void main(字符串[]args){
SpringApplication.run(So60432039Application.class,args);
}
@豆子
公共抽象服务器连接工厂服务器1(){
TcpNetServerConnectionFactory=新的TcpNetServerConnectionFactory(1234);
factory.registerListener(m->{
System.out.println(“1:+m.getPayload());
返回false;
});
返回工厂;
}
@豆子
公共抽象服务器连接工厂服务器2(){
TcpNetServerConnectionFactory=新的TcpNetServerConnectionFactory(1235);
factory.registerListener(m->{
System.out.println(“2:+m.getPayload());
返回false;
});
返回工厂;
}
@豆子
公共抽象客户端连接工厂客户端1(){
返回新的TcpNetClientConnectionFactory(“localhost”,1234);
}
@豆子
公共抽象客户端连接工厂客户端2(){
返回新的TcpNetClientConnectionFactory(“localhost”,1235);
}
@豆子
公共故障转移客户端连接工厂故障转移(){
故障转移客户机连接工厂=
新的FailoverClient连接工厂(Arrays.asList(client1(),client2());
factory.registerListener(m->false);
factory.setRefreshSharedInterval(15_000);
factory.setCloseOnRefresh(true);
返回工厂;
}
@计划(固定延迟=5_000,初始延迟=5_000)
public void getConnection()引发异常{
试一试{
TcpConnectionSupport connection=failover().getConnection();
发送(新的通用消息(“foo”);
System.out.println(connection.getConnectionId());
}
捕捉(中断异常e){
Thread.currentThread().interrupt();
}
捕获(运行时异常e){
System.out.println(e.getMessage());
}
}
@豆子
公共应用程序运行程序(){
返回参数->{
client1().start();
client2().start();
故障转移().start();
System.out.println(“点击回车启动服务器2”);
System.in.read();
server2().start();
System.out.println(“点击回车启动服务器1”);
System.in.read();
server1().start();
System.out.println(“命中e
logging.level.org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory=debug
logging.level.org.springframework.integration.ip.tcp.connection.FailoverClientConnectionFactory$FailoverTcpConnection=debug
Hit enter to start server2
<enter>
2020-03-03 09:23:36.065  INFO 78635 --- [           main] .s.i.i.t.c.TcpNetServerConnectionFactory : started server2, port=1235
Hit enter to start server1
2020-03-03 09:23:36.066  INFO 78635 --- [pool-1-thread-1] .s.i.i.t.c.TcpNetServerConnectionFactory : server2, port=1235 Listening
2020-03-03 09:23:38.690 DEBUG 78635 --- [ask-scheduler-2] .s.i.i.t.c.TcpNetClientConnectionFactory : Opening new socket connection to localhost:1234
2020-03-03 09:23:38.695 DEBUG 78635 --- [ask-scheduler-2] tConnectionFactory$FailoverTcpConnection : client1, host=localhost, port=1234 failed with java.net.ConnectException: Connection refused (Connection refused), trying another
2020-03-03 09:23:38.695 DEBUG 78635 --- [ask-scheduler-2] .s.i.i.t.c.TcpNetClientConnectionFactory : Opening new socket connection to localhost:1235
2020-03-03 09:23:38.697 DEBUG 78635 --- [ask-scheduler-2] .s.i.i.t.c.TcpNetClientConnectionFactory : client2: Added new connection: localhost:1235:52647:f370c7e4-2455-4114-9c7a-55bd38f728cf
2020-03-03 09:23:38.698 DEBUG 78635 --- [ask-scheduler-2] tConnectionFactory$FailoverTcpConnection : Got localhost:1235:52647:f370c7e4-2455-4114-9c7a-55bd38f728cf from client2, host=localhost, port=1235
2053c07b-3d7c-4ac0-8b46-1e2f703fa6a9:1
2:[B@7d7d9ef3
2053c07b-3d7c-4ac0-8b46-1e2f703fa6a9:2
2:[B@3316e0a4
2053c07b-3d7c-4ac0-8b46-1e2f703fa6a9:3
2:[B@2dae23bc
Hit enter to start server2
<enter>
2020-03-03 09:25:45.143  INFO 79176 --- [           main] .s.i.i.t.c.TcpNetServerConnectionFactory : started server2, port=1235
Hit enter to start server1
2020-03-03 09:25:45.144  INFO 79176 --- [pool-1-thread-1] .s.i.i.t.c.TcpNetServerConnectionFactory : server2, port=1235 Listening
2020-03-03 09:25:47.363 DEBUG 79176 --- [ask-scheduler-2] .s.i.i.t.c.TcpNetClientConnectionFactory : Opening new socket connection to localhost:1234
2020-03-03 09:25:47.368 DEBUG 79176 --- [ask-scheduler-2] tConnectionFactory$FailoverTcpConnection : client1, host=localhost, port=1234 failed with java.net.ConnectException: Connection refused (Connection refused), trying another
2020-03-03 09:25:47.368 DEBUG 79176 --- [ask-scheduler-2] .s.i.i.t.c.TcpNetClientConnectionFactory : Opening new socket connection to localhost:1235
2020-03-03 09:25:47.369 DEBUG 79176 --- [ask-scheduler-2] .s.i.i.t.c.TcpNetClientConnectionFactory : client2: Added new connection: localhost:1235:52775:31caf4d1-0fd1-49bf-8be6-f9fa935bd8a0
2020-03-03 09:25:47.371 DEBUG 79176 --- [ask-scheduler-2] tConnectionFactory$FailoverTcpConnection : Got localhost:1235:52775:31caf4d1-0fd1-49bf-8be6-f9fa935bd8a0 from client2, host=localhost, port=1235
618d04e0-aa3a-4bac-966c-982d93528dd9:1
2:[B@47858489
2020-03-03 09:25:52.373 DEBUG 79176 --- [ask-scheduler-1] .s.i.i.t.c.TcpNetClientConnectionFactory : Opening new socket connection to localhost:1234
2020-03-03 09:25:52.374 DEBUG 79176 --- [ask-scheduler-1] tConnectionFactory$FailoverTcpConnection : client1, host=localhost, port=1234 failed with java.net.ConnectException: Connection refused (Connection refused), trying another
2020-03-03 09:25:52.374 DEBUG 79176 --- [ask-scheduler-1] tConnectionFactory$FailoverTcpConnection : Got localhost:1235:52775:31caf4d1-0fd1-49bf-8be6-f9fa935bd8a0 from client2, host=localhost, port=1235
51b1c8ab-15c6-4108-acb7-b34584bf8506:1
2:[B@79bc08ef
2020-03-03 09:25:57.378 DEBUG 79176 --- [ask-scheduler-2] .s.i.i.t.c.TcpNetClientConnectionFactory : Opening new socket connection to localhost:1234
2020-03-03 09:25:57.378 DEBUG 79176 --- [ask-scheduler-2] tConnectionFactory$FailoverTcpConnection : client1, host=localhost, port=1234 failed with java.net.ConnectException: Connection refused (Connection refused), trying another
2020-03-03 09:25:57.379 DEBUG 79176 --- [ask-scheduler-2] tConnectionFactory$FailoverTcpConnection : Got localhost:1235:52775:31caf4d1-0fd1-49bf-8be6-f9fa935bd8a0 from client2, host=localhost, port=1235
3c49c6f7-fe6a-410b-acff-cfa20aba42b1:1
2:[B@774d8ab2