![](/img/trans.png)
[英]How to inject config properties in Spring Boot to Spring Retry annotation?
[英]How set up retry config fetching in spring boot?
我有配置服務器,應用程序從該服務器獲取配置。 我想設置抓取的重試機制。 如果配置服務器不可用,應用程序應發送請求 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
(配置服務器在那一刻關閉)
我究竟做錯了什么? 有辦法解決我的問題嗎?
答案是前兩個答案的組合:
spring.cloud.config.fail-fast=true
spring-retry
和spring-boot-starter-aop
到您的類路徑中。請參閱此處的文檔。
根據問題中的信息,我認為您的類路徑中缺少以下依賴項:
<!-- 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 -->
您將spring.cloud.config.fail-fast
設置為 true。 根據文檔,這將在異常情況下停止您的應用程序,並且不會重試連接。
來源: https : //cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_client.html#config-client-fail-fast
我通過將下一個 @Bean 添加到上下文來解決我的問題
@Bean
public RetryOperationsInterceptor configServerRetryInterceptor(RetryProperties properties) {
return RetryInterceptorBuilder
.stateless()
.backOffOptions(properties.getInitialInterval(),
properties.getMultiplier(),
properties.getMaxInterval())
.maxAttempts(properties.getMaxAttempts()).build();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.