简体   繁体   中英

resilience4j Spring Boot 2

Trying out a simple Spring Boot 2 + Resilience4j project.

But facing an issue that the circuit breaker is always CLOSED though the host application is down.

Service class

@Autowired
private RestTemplate restTemplate;

@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}

@CircuitBreaker(name = "mainService", fallbackMethod="testFallBack")
public ResponseEntity<String> invokeService(int i) {
    return restTemplate.exchange(
            "http://localhost:9092/", // This service is always down
            HttpMethod.GET,
            null,
            String.class
    );
}

private  ResponseEntity<String> testFallBack(int i, Exception e) {
    return new ResponseEntity<String>("In fallback method", HttpStatus.INTERNAL_SERVER_ERROR);
}

Resilience4J Config

management.endpoint.health.show-details: always
management.health.circuitbreakers.enabled: true

resilience4j:
  circuitbreaker:
    configs:
      default:
        registerHealthIndicator: true
        minimumNumberOfCalls: 5
        permittedNumberOfCallsInHalfOpenState: 3
        automaticTransitionFromOpenToHalfOpenEnabled: true
        waitDurationInOpenState: 5s
        failureRateThreshold: 50
        eventConsumerBufferSize: 10
    instances:
      mainService:
        baseConfig: default

The Service is being called multiple times from the Controller and I expect it to fallback after minimum 5 calls but the circuit breaker is always CLOSED and for each call from controller the host service is being called and up with Connection refused.

Dependencies: spring-boot-starter-web, resilience4j-spring-boot2, spring-aop, spring-boot-starter-actuator

Earlier I tried out programmatic approach using CircuitBreakerRegistry and Decorator Function which works as expected.

Actually, you misunderstand the circuit breaker parameters. See the documentation :

minimumNumberOfCalls , default: 100

Configures the minimum number of calls which are required (per sliding window period) before the CircuitBreaker can calculate the error rate or slow call rate.

For example, if minimumNumberOfCalls is 10, then at least 10 calls must be recorded, before the failure rate can be calculated. If only 9 calls have been recorded the CircuitBreaker will not transition to open even if all 9 calls have failed.

slidingWindowSize , default: 100

Configures the size of the sliding window which is used to record the outcome of calls when the CircuitBreaker is closed.

In your configuration, there is

minimumNumberOfCalls: 5
slidingWindowSize: 100  ## implicitly, because you have not set this parameters

And you

expect it to fallback after minimum 5 calls

However, your circuit breaker opens after 100 failures, not after 5.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM