【问题标题】:How set up retry config fetching in spring boot?如何在 Spring Boot 中设置重试配置获取?
【发布时间】:2018-10-25 03:08:33
【问题描述】:

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

在 spring 文档中我找到了下一个配置

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 

(此时配置服务器已关闭)

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

【问题讨论】:

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


    【解决方案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】:

      我通过将下一个 @Bean 添加到上下文解决了我的问题

      @Bean
          public RetryOperationsInterceptor configServerRetryInterceptor(RetryProperties properties) {
              return RetryInterceptorBuilder
                      .stateless()
                      .backOffOptions(properties.getInitialInterval(),
                              properties.getMultiplier(),
                              properties.getMaxInterval())
                      .maxAttempts(properties.getMaxAttempts()).build();
          }
      

      【讨论】:

        【解决方案3】:

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

                <!-- 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 -->
        

        【讨论】:

          【解决方案4】:

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

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

          请参阅文档here

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-05-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-08-30
            • 2019-12-23
            相关资源
            最近更新 更多