具有重试配置的 Resiliency4j 断路器不起作用

发布于 2025-01-13 16:09:58 字数 2226 浏览 1 评论 0原文

我在服务方法上同时使用 @CircuitBreaker@Retry 注释。当我应用这两种配置时,重试配置不会生效。

下面是配置:

resilience4j:
  circuitbreaker:
    instances:
      inventorymicroservice:
        registerHealthIndicator: true
        ringBufferSizeInClosedState: 5
        ringBufferSizeInHalfOpenState: 3
        waitDurationInOpenState: 30000
        failureRateThreshold: 50
        slowCallRateThreshold: 50
        recordExceptions:
          - java.io.IOException
          - java.util.concurrent.TimeoutException
          - java.net.ConnectException
          - org.springframework.web.reactive.function.client.WebClientRequestException
  retry:
    instances:
      retryConfig:
        maxAttempts: 3
        waitDuration: 10s
        enableExponentialBackoff: true
        exponentialBackoffMultiplier: 2
        retryExceptions:
          - org.springframework.web.client.HttpServerErrorException
          - java.io.IOException
          - java.io.IOException
          - java.util.concurrent.TimeoutException
          - java.net.ConnectException
          - org.springframework.web.reactive.function.client.WebClientRequestException

服务方法:

   @CircuitBreaker(name = "inventorymicroservice", fallbackMethod = "fallBack")
    @Retry(name = "retryConfig", fallbackMethod = "fallBack")
    public Order saveOrder(Order order){
        Order savedOrder = this.orderRepository.save(order);
        log.info("Calling the inventory service to update the quantity :: ");
        //ResponseEntity<Integer> integerResponseEntity = this.restTemplate.postForEntity("http://localhost:9222/api/inventory", null, Integer.class);
        Integer response = this.webClient
                .post()
                .uri("/api/inventory")
                .retrieve()
                .bodyToMono(Integer.class)
                .block();
        log.info("Response from the inventory microservice :: {}", response);
        return savedOrder;
    }

    private Order fallBack(Throwable exception){
        log.error("Exception while invoking the REST endpoint :: ", exception.getMessage());
        return Order.builder().build();
    }

我哪里出错了?另外,如何使用函数式编程将此配置转换为编程配置。

I am using both @CircuitBreaker and @Retry annotations on a service method. When I apply both the configurations, the retry configurations are not taking affect.

Below is the configuration:

resilience4j:
  circuitbreaker:
    instances:
      inventorymicroservice:
        registerHealthIndicator: true
        ringBufferSizeInClosedState: 5
        ringBufferSizeInHalfOpenState: 3
        waitDurationInOpenState: 30000
        failureRateThreshold: 50
        slowCallRateThreshold: 50
        recordExceptions:
          - java.io.IOException
          - java.util.concurrent.TimeoutException
          - java.net.ConnectException
          - org.springframework.web.reactive.function.client.WebClientRequestException
  retry:
    instances:
      retryConfig:
        maxAttempts: 3
        waitDuration: 10s
        enableExponentialBackoff: true
        exponentialBackoffMultiplier: 2
        retryExceptions:
          - org.springframework.web.client.HttpServerErrorException
          - java.io.IOException
          - java.io.IOException
          - java.util.concurrent.TimeoutException
          - java.net.ConnectException
          - org.springframework.web.reactive.function.client.WebClientRequestException

Service method:

   @CircuitBreaker(name = "inventorymicroservice", fallbackMethod = "fallBack")
    @Retry(name = "retryConfig", fallbackMethod = "fallBack")
    public Order saveOrder(Order order){
        Order savedOrder = this.orderRepository.save(order);
        log.info("Calling the inventory service to update the quantity :: ");
        //ResponseEntity<Integer> integerResponseEntity = this.restTemplate.postForEntity("http://localhost:9222/api/inventory", null, Integer.class);
        Integer response = this.webClient
                .post()
                .uri("/api/inventory")
                .retrieve()
                .bodyToMono(Integer.class)
                .block();
        log.info("Response from the inventory microservice :: {}", response);
        return savedOrder;
    }

    private Order fallBack(Throwable exception){
        log.error("Exception while invoking the REST endpoint :: ", exception.getMessage());
        return Order.builder().build();
    }

Where am I going wrong? Also, how to convert this configuration to programmatic configuration using functional programming.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最初的梦 2025-01-20 16:09:58

默认的Resilience4j方面顺序

Retry( CircuitBreaker( RateLimiter( TimeLimiter( Bulkhead( function)))))

你的CircuitBreaker有一个后备,所以它永远不会抛出异常,因此 Retry 永远不会看到失败的重试调用。

删除重试后备,并更改方面顺序,以便 CircuitBreaker 在重试后继续工作。

resilience4j:
  circuitbreaker:
    circuitBreakerAspectOrder: 1
  retry:
    retryAspectOrder: 2

The default Resilience4j aspect order is

Retry( CircuitBreaker( RateLimiter( TimeLimiter( Bulkhead( function)))))

Your CircuitBreaker has a fallback, so it never throws an exception, so Retry never sees a failed invocation to retry.

Remove the Retry fallback, and change the aspect order so CircuitBreaker does its work after Retry.

resilience4j:
  circuitbreaker:
    circuitBreakerAspectOrder: 1
  retry:
    retryAspectOrder: 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文