Search code examples
spring-cloudhystrixspring-cloud-netflixspring-cloud-feign

Spring Cloud config feign fallback(CircuitBreaker) rule


Now I use feign with hystrix, it turns out that Circuit will turn to Open status when fallback method invoke 20 times in 5s. How can I change this rule. For example, let the Circuit status turn to open when fallback method invoke 50 times in 5s, or by fallback callback rate. Here is my main java code.

ConsumerApplication.java

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
@RibbonClients({@RibbonClient(name = "cloud-provider", configuration = CloudProviderConfiguration.class)})
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

UserFeignClient.java

@FeignClient(name = "cloud-provider", fallback = UserFeignClient.HystrixClientFallback.class)
public interface UserFeignClient {
    @RequestMapping("/{id}")
    BaseResponse findByIdFeign(@RequestParam("id") Long id);

    @RequestMapping("/add")
    BaseResponse addUserFeign(UserVo userVo);

    @Component
    class HystrixClientFallback implements UserFeignClient {
        private static final Logger LOGGER = LoggerFactory.getLogger(HystrixClientFallback.class);

        @Override
        public BaseResponse findByIdFeign(@RequestParam("id") Long id) {
            BaseResponse response = new BaseResponse();
            response.setMessage("disable!!!!");
            return response;
        }

        @Override
        public BaseResponse addUserFeign(UserVo userVo) {
            BaseResponse response = new BaseResponse();
            response.setMessage("disable");
            return response;
        }
    }
}

Solution

  • The configuration parameters are described here https://github.com/Netflix/Hystrix/wiki/Configuration

    hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds changes the 5 second window you are seeing.

    hystrix.command.default.circuitBreaker.requestVolumeThreshold defaults to 20 requests.