【问题标题】:How to add resilience4j retry to a spring boot 2 webclient call?如何将弹性 4j 重试添加到 spring boot 2 webclient 调用?
【发布时间】:2020-07-18 12:04:15
【问题描述】:

我正在尝试使用不工作的resilience4j 重试将重试机制添加到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")
    }

【问题讨论】:

    标签: spring spring-boot kotlin spring-webclient resilience4j


    【解决方案1】:

    尝试为您的异步函数返回一个 Future。

    @Retry(name = BACKEND_A)
    fun callRestTemplate(): Future<String> {
    

    我也看不到您的服务声明,但我遇到了同样的问题。在类上添加重试注释解决了它。

    @Retry(name = BACKEND_A)
    @Service
    class BackendServiceA() {
    

    【讨论】:

      猜你喜欢
      • 2019-07-12
      • 2020-03-10
      • 2020-11-03
      • 1970-01-01
      • 2021-01-23
      • 2021-03-07
      • 2022-01-16
      • 1970-01-01
      • 2021-11-01
      相关资源
      最近更新 更多