FailoverClientConnectionFactory: соединен с резервным сервером, но все еще пытается соединиться с основным сервером - PullRequest
0 голосов
/ 27 февраля 2020

У меня проблема, если соединение с основным сервером и сервером отработки отказа разорвано. Через некоторое время происходит аварийное переключение на сервер и успешное установление соединения с отказоустойчивым сервером. Но все же пытается подключиться к основному серверу, это желаемое поведение?

Фрагмент кода для фабрики соединений:

List processServers = mConfigurationService.getProcessServers (); List factory = new ArrayList <> ();

        for (ServerInfo serverInfo : processServers) 
        {
            AbstractClientConnectionFactory clientFactory = new TcpNetClientConnectionFactory(
                    serverInfo.getServerAddress(), serverInfo.getServerPort());
            clientFactory.setApplicationEventPublisher(this.applicationEventPublisher);             
            clientFactory.setSingleUse(false);
            clientFactory.setDeserializer(mMessageSerializerDeserializer);
            clientFactory.setSerializer(mMessageSerializerDeserializer);
            factories.add(clientFactory);
        }

        failoverCF = new FailoverClientConnectionFactory(factories);
        failoverCF.setSingleUse(false);
        failoverCF.afterPropertiesSet();

Вот журналы, в которых мы видим, что соединение установлено с 10.90.xF, но оно все еще пытается соединиться с 10.90.xP

2020-02-27 16:33:53 [task-scheduler-10] DEBUG osiit c .TcpNetClientConnectionFactory - Открытие нового сокетного соединения для 10.90.xP: 42027 2020-02-27 16:33: 53 [task-scheduler-2] DEBUG osiit c .FailoverClientConnectionFactory $ FailoverTcpConnection - org.springframework.integration.ip.tcp.connection. TcpNetClientConnectionFactory@4946485c, хост = 10.90.xP, порт = 42027 не выполнен с java. net .ConnectException: Тайм-аут соединения: подключиться, попробовать другое 2020-02-27 16:33:53 [task-scheduler-2] DEBUG osiit c .FailoverClientConnectionFactory $ FailoverTcpConnection - Получено 10.90.xF: 42027: 65437: 1f582e09 -eb9a-4966-8fd2-60cb156ad015 от org.springframework.integration.ip.tcp.connection. TcpNetClientConnectionFactory@1df06ecd, хост = 10.90.xF, порт = 42027 2020-02-27 16:33:53 [task-scheduler-2 ] DEBUG osiit c .ClientModeConnectionManager - Соединение bae635ce-c64 3-4650-ba15-5fb2fe8af053: 1 установлено 2020-02-27 16:34:14 [task-scheduler-10] DEBUG osiit c .FailoverClientConnectionFactory $ FailoverTcpConnection - org.springframework.integration.ip.tcp.connection. TcpNetClientConnectionFactory@4946485c, хост = 10.90.xP, порт = 42027: сбой при java. net .ConnectException: истекло время ожидания соединения: подключиться, попробовать другое 2020-02-27 16:34:14 [task-scheduler-7] DEBUG osiit c .TcpNetClientConnectionFactory - Открытие нового сокетного соединения с 10.90.xP: 42027 2020-02-27 16:34:14 [task-scheduler-10] DEBUG osiit c .FailoverClientConnectionFactory $ FailoverTcpConnection - Получено 10.90.xF: 42027 : 65437: 1f582e09-eb9a-4966-8fd2-60cb156ad015 от org.springframework.integration.ip.tcp.connection. TcpNetClientConnectionFactory@1df06ecd, хост = 10.90.xF, порт = 42027 2020-02-27 16:34:14 [задача -scheduler-10] DEBUG osiitcp.TcpSendingMessageHandler - получено соединение 7a451368-13b2-4abf-908e-3cfe0fca2148: 1

2020-02-27 16:34:35 [task-scheduler-7] DEBUG osiit c .FailoverClientConnectionFactory $ FailoverTcpConnection - org.springf ramework.integration.ip.tcp.connection. TcpNetClientConnectionFactory@4946485c, host = 10.90.xP, port = 42027 завершились неудачно с java. net .ConnectException: Тайм-аут соединения: соединение, попытка другого 2020-02-27 16: 34:35 [task-scheduler-7] DEBUG osiit c .FailoverClientConnectionFactory $ FailoverTcpConnection - Получено 10.90.xF: 42027: 65437: 1f582e09-eb9a-4966-8fd2-60cb156ad015 из org.spring.conf . TcpNetClientConnectionFactory@1df06ecd, хост = 10.90.xF, порт = 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

1 Ответ

0 голосов
/ 28 февраля 2020

Спасибо за ваше терпение; Я воспроизвел его.

Это ошибка https://github.com/spring-projects/spring-integration/issues/3199

РЕДАКТИРОВАТЬ

Новые свойства для управления этим поведением:

/**
 * 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;
}

EDIT2

Помимо модульных тестов , я протестировал исправление с приложением, которое написал, чтобы воспроизвести вашу проблему:

@SpringBootApplication
@EnableScheduling
public class So60432039Application {

    public static void main(String[] args) {
        SpringApplication.run(So60432039Application.class, args);
    }

    @Bean
    public AbstractServerConnectionFactory server1() {
        TcpNetServerConnectionFactory factory = new TcpNetServerConnectionFactory(1234);
        factory.registerListener(m -> {
            System.out.println("1:" + m.getPayload());
            return false;
        });
        return factory;
    }

    @Bean
    public AbstractServerConnectionFactory server2() {
        TcpNetServerConnectionFactory factory = new TcpNetServerConnectionFactory(1235);
        factory.registerListener(m -> {
            System.out.println("2:" + m.getPayload());
            return false;
        });
        return factory;
    }

    @Bean
    public AbstractClientConnectionFactory client1() {
        return new TcpNetClientConnectionFactory("localhost", 1234);
    }

    @Bean
    public AbstractClientConnectionFactory client2() {
        return new TcpNetClientConnectionFactory("localhost", 1235);
    }

    @Bean
    public FailoverClientConnectionFactory failover() {
        FailoverClientConnectionFactory factory =
                new FailoverClientConnectionFactory(Arrays.asList(client1(), client2()));
        factory.registerListener(m -> false);
        factory.setRefreshSharedInterval(15_000);
        factory.setCloseOnRefresh(true);
        return factory;
    }

    @Scheduled(fixedDelay = 5_000, initialDelay = 5_000)
    public void getConnection() throws Exception {
        try {
            TcpConnectionSupport connection = failover().getConnection();
            connection.send(new GenericMessage<>("foo"));
            System.out.println(connection.getConnectionId());
        }
        catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        catch (RuntimeException e) {
            System.out.println(e.getMessage());
        }
    }

    @Bean
    public ApplicationRunner runner() {
        return args -> {
            client1().start();
            client2().start();
            failover().start();
            System.out.println("Hit enter to start server2");
            System.in.read();
            server2().start();
            System.out.println("Hit enter to start server1");
            System.in.read();
            server1().start();
            System.out.println("Hit enter to stop server1");
            System.in.read();
            server1().stop();
        };
    }

}
logging.level.org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory=debug
logging.level.org.springframework.integration.ip.tcp.connection.FailoverClientConnectionFactory$FailoverTcpConnection=debug

С factory.setRefreshSharedInterval(15_000); на месте; Я получаю:

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

EDIT3

Вот пом

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.22.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>so60432039</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>so60432039</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-integration</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-ip</artifactId>
            <version>4.3.22.BUILD-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...