我正在尝试使用不工作的resilience4j重试将重试机制添加到webclient rest调用。如果出现异常,该方法只会被调用一次。我正在使用带有 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")
}