如何在 Spring Boot 中设置重试配置获取?

Art*_*nov 5 java spring kotlin spring-boot spring-cloud

我有配置服务器,应用程序从该服务器获取配置。我想设置抓取的重试机制。如果配置服务器不可用,应用程序应发送 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
Run Code Online (Sandbox Code Playgroud)

但他们什么也改变不了。我的应用程序不执行重试请求,它只是失败了

Caused by: java.net.ConnectException: Connection refused: connect 
Run Code Online (Sandbox Code Playgroud)

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

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

Mar*_*nee 8

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

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

请参阅此处的文档。


Art*_*nov -1

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

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