如何在spring boot中设置重试配置?

时间:2018-05-15 10:09:14

标签: java spring spring-boot kotlin spring-cloud

我有配置服务器和应用程序从此服务器获取配置。 我想设置获取的重试机制。如果配置服务器不可用,应用程序将发送请求10分钟。

在春季文档中,我发现了下一个配置

spring.cloud.config.uri=http://localhost:9090
spring.cloud.config.fail-fast=true
spring.cloud.config.retry.max-interval=10000
spring.cloud.config.retry.max-attempts=2000

但他们什么都没改变。我的应用程序没有通过

重试请求失败
Caused by: java.net.ConnectException: Connection refused: connect 

(配置服务器在那一刻关闭)

我做错了什么?有办法解决我的问题吗?

4 个答案:

答案 0 :(得分:4)

答案是前两个答案的组合:

  • 您需要设置spring.cloud.config.fail-fast=true
  • 您还需要将spring-retryspring-boot-starter-aop添加到您的类路径中。

请参阅文档here

答案 1 :(得分:1)

您将spring.cloud.config.fail-fast设置为true。根据文档,这将暂停您的应用程序,但不会重试连接。

来源:https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_client.html#config-client-fail-fast

答案 2 :(得分:0)

根据问题中的信息,我认为您缺少类路径中的以下依赖项:

        <!-- for auto retry -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
        <version>1.2.4.RELEASE</version>
    </dependency>
    <!-- for auto retry -->

答案 3 :(得分:-2)

我通过将下一个@Bean添加到上下文

来解决了我的问题
@Bean
    public RetryOperationsInterceptor configServerRetryInterceptor(RetryProperties properties) {
        return RetryInterceptorBuilder
                .stateless()
                .backOffOptions(properties.getInitialInterval(),
                        properties.getMultiplier(),
                        properties.getMaxInterval())
                .maxAttempts(properties.getMaxAttempts()).build();
    }