Search code examples
spring-cloud-netflixhystrixspring-cloud-feignhttp-status-code-429

Hystrix/Feign to solely react on HTTP status 429


I'm using Feign from the spring-cloud-starter-feign to send requests to a defined backend. I would like to use Hystrix as a circuit-breaker but for only one type of use-case: If the backend responds with a HTTP 429: Too many requests code, my Feign client should wait exactly one hour until it contacts the real backend again. Until then, a fallback method should be executed.

How would I have to configure my Spring Boot (1.5.10) application in order to accomplish that? I see many configuration possibilities but only few examples which are - in my opinion - unfortunately not resolved around use-cases.


Solution

  • I could solve it with the following adjustments:

    Properties in application.yml:

    hystrix.command.commandKey:
      execution.isolation.thread.timeoutInMilliseconds: 10_000
      metrics.rollingStats.timeInMilliseconds: 10_000
      circuitBreaker:
        errorThresholdPercentage: 1
        requestVolumeThreshold: 1
        sleepWindowInMilliseconds: 3_600_000
    

    Code in the respective Java class:

    @HystrixCommand(fallbackMethod = "fallbackMethod", commandKey = COMMAND_KEY)
    public void doCall(String parameter) {
        try {
            feignClient.doCall(parameter);
        } catch (FeignException e) {
            if (e.status() == 429) {
                throw new TooManyRequestsException(e.getMessage());
            } 
        }
    }