我正在尝试使用不起作用的resilience4j retry将重试机制添加到webclient剩余调用中。在异常情况下,该方法仅被调用一次。我正在使用Kotlin的Spring Boot 2。
这是来电者
GlobalScope.launch {
println(service.callRest())
}
这是配置
resilience4j.retry:
configs:
default:
maxRetryAttempts: 3
waitDuration: 100
retryExceptions:
- java.lang.IllegalArgumentException
- java.util.concurrent.TimeoutException
- org.springframework.web.client.HttpServerErrorException
- java.io.IOException
- java.net.UnknownHostException
- org.springframework.web.reactive.function.client.WebClientResponseException
- org.springframework.web.reactive.function.client.WebClientResponseException$NotFound
- org.springframework.web.client.HttpClientErrorException$NotFound
instances:
backendA:
baseConfig: default
这是我的方法:
@Retry(name = BACKEND_A)
suspend fun callRest(): String {
println("tried calling")
return webClient.get()
.uri("/api/v1/dummy1")
.accept(APPLICATION_JSON)
.retrieve()
.awaitBody()
}
如果我从方法中抛出硬编码异常,则重试正常
@Retry(name = BACKEND_A)
@Throws(WebClientResponseException::class)
suspend fun callRest(): String {
println("tried calling")
throw WebClientResponseException("abc", 404, "abc", null, null, null)
此外,它与restTemplate一起使用
@Retry(name = BACKEND_A)
fun callRestTemplate(): String {
println("tried calling")
return restTemplate
.getForObject("/api/v1/dummy1")
}
最佳答案
尝试为您的异步函数返回Future。
@Retry(name = BACKEND_A)
fun callRestTemplate(): Future<String> {
另外,我看不到您的服务声明,但存在相同的问题。在类上添加重试注释可以解决它。
@Retry(name = BACKEND_A)
@Service
class BackendServiceA() {
关于spring - 如何在spring boot 2 webclient调用中添加resilience4j retry?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61060676/