我有配置服务器和应用程序从此服务器获取配置。 我想设置获取的重试机制。如果配置服务器不可用,应用程序将发送请求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
(配置服务器在那一刻关闭)
我做错了什么?有办法解决我的问题吗?
答案 0 :(得分:4)
答案是前两个答案的组合:
spring.cloud.config.fail-fast=true
spring-retry
和spring-boot-starter-aop
添加到您的类路径中。请参阅文档here。
答案 1 :(得分:1)
您将spring.cloud.config.fail-fast
设置为true。根据文档,这将暂停您的应用程序,但不会重试连接。
答案 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();
}