【问题标题】:Resilience4j.retry with exception is not workingResilience4j.retry 异常不工作
【发布时间】:2020-08-08 07:20:08
【问题描述】:

我正在使用resilience4j.retry 和resilience4j.circuitbreaker。 Service1 正在调用另一个服务Service2,它可以抛出异常。即使我遇到异常,我也应该尝试连接至少定义的次数waitDuration * maxRetryAttempts。

Service1 ---> 调用 ---> Service2

application.yml

resilience4j.retry:
  instances:
    service1:
      maxRetryAttempts: 4
      waitDuration: 1000 #ms
      retryExceptions:  
        - org.apache.http.conn.HttpHostConnectException
        - org.apache.thrift.TException
        - java.net.ConnectException

Service1Repository.java

Service2Response response = CircuitBreaker.decorateSupplier(circuitBreaker,
                                    Retry.decorateSupplier(retry, () -> {
                                        try {
                                            return service2.getdata(params);
                                        } catch (TException e) {
                                            log.error("msg", e);
                                            throw new Service1Exception("msg", e);
                                        }
                                    })
                            ).get();

但是,当Service2 关闭时,它不会重试(最多 4 次,每次应该在 1000 毫秒后发生)我会立即收到响应(在我的情况下为null)。我尝试增加值(10000ms),还是一样。我看到打印了日志,但如果Serviece2 关闭,服务应该等待waitDuration * maxRetryAttempts。

【问题讨论】:

    标签: java rest circuit-breaker retry-logic resilience4j


    【解决方案1】:

    如果要捕获已检查的异常,则必须使用decorateCallable。

    【讨论】:

    • 你知道decorateRunnable只捕获RuntimeException和Exception的原因吗?
    • 因为你通常不应该捕获 Throwable/Error。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2018-10-03
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多