【问题标题】:Call is not going to fallback method of Resilience4 Circuit breaker呼叫不会使用 Resilience4 断路器的回退方法
【发布时间】:2020-10-26 03:58:21
【问题描述】:

我正在使用 Resilience4j 断路器版本:'1.4.0 和 Spring Boot 版本 2.0.6,我的问题是 - 回退方法不起作用。该调用不会转到回退方法。 以下是我的代码:

@Override
@CircuitBreaker(name="mainService",fallbackMethod = "callFallback")
public JSONObject callService(JSONObject rawRequest) throws TimeoutException {
      ...
       throw new TimeoutException("Time occured while calling 
       service");
      ...
}

-- 和后备方法:

private JSONObject callFallback(JSONObject rawRequest,Throwable t){

    System.out.println("Inside fallback method callNsFallback, 
        cause"+t.toString());

        logger.info("Inside fallback method callFallback, 
        cause - {}",t.toString());

    return rawRequest;
}

--application.yml 中的配置

resilience4j:
circuitbreaker:
    configs:
    default:
        registerHealthIndicator: true
        ringBufferSizeInClosedState: 5
        ringBufferSizeInHalfOpenState: 3
        slidingWindowSize: 10
        minimumNumberOfCalls: 5
        permittedNumberOfCallsInHalfOpenState: 3
        automaticTransitionFromOpenToHalfOpenEnabled: true
        waitDurationInOpenState: 1s
        failureRateThreshold: 50
        eventConsumerBufferSize: 10
        recordExceptions:
        - java.util.concurrent.TimeoutException
    instances:
    mainService:
        baseConfig: default

【问题讨论】:

  • 如果没有回退方法,它会抛出 CallNotPermittedException 吗?
  • 你只捕获 TimeoutException 吗?
  • 感谢@SaifUrRahman 的回复。它没有抛出 CallNotPermittedException,我只使用了 TimeoutException。该问题通过添加带有resilience4J的'spring-aop'依赖来解决
  • 好的。您能否分享您的 pom.xml 作为答案?

标签: spring spring-boot circuit-breaker resilience4j


【解决方案1】:

可能的原因可能是您缺少一些必需的依赖项。在这里,我将分享对我有用的依赖项和 yaml 配置。

pom.xml

<dependencies>  
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>io.github.resilience4j</groupId>
            <artifactId>resilience4j-spring-boot2</artifactId>
            <version>1.7.1</version>
        </dependency>
    </dependencies>

application.yml

resilience4j:
  circuitbreaker:
    instances:
      menu-service:
        registerHealthIndicator: true
        eventConsumerBufferSize: 10
        automaticTransitionFromOpenToHalfOpenEnabled: true
        failureRateThreshold: 50
        minimumNumberOfCalls: 5
        permittedNumberOfCallsInHalfOpenState: 1
        slidingWindowSize: 10
        waitDurationInOpenState: 2s
        slidingWindowType: COUNT_BASED
        
        
management:
  health:
    circuitbreakers:
      enabled: true
  endpoints:
    web:
      exposure:
        include: health
  endpoint:
    health:
      show-details: always

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 2021-10-31
    • 2022-08-23
    相关资源
    最近更新 更多