Spring Cloud: Hystrix service blown


The circuit breaker mechanism is a microservice link protection mechanism to deal with the avalanche effect. When a microservice of the fan-out link fails or the response time is too long, the service will be degraded, and then the microservice call of the node will be blown. Returns the appropriate information for the error.
In Spring Cloud, the circuit breaker mechanism can be implemented through Hystrix, which can monitor calls between microservices. When the failed calls reach a certain threshold, the default is that 20 calls fail within 5 seconds, and the circuit breaker mechanism will be activated. The annotation of the circuit breaker mechanism is @HystrixCommand.
When the circuit breaker mechanism is activated, subsequent calls to the service interface no longer go through the network, and the local default method is directly executed to achieve the effect of service degradation.
After the specified time window, the service will recover from the circuit breaker state and accept the caller's remote call again.
Official website original image:

The following is the general process (made by the drawing tool, relatively simple)

Hystrix implements fuse mechanism


    //=========================Service fuse =====================
    @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),//Whether to open the circuit breaker
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),//number of requests
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),//time window
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"),//What is the failure rate to trip after
    public String paymentCircuitBreaker(Integer id){
            throw new RuntimeException("id<0");
        String serialNumber = IdUtil.simpleUUID();
        return Thread.currentThread().getName()+"\t"+"successful call,serial number:"+serialNumber;

    public String paymentCircuitBreaker_fallback(Integer id){
        return "id<0"+":"+id;


public class PaymentController {
    private PaymentService paymentService;

    public String paymentCircuitBreaker(@PathVariable Integer id){
        return paymentService.paymentCircuitBreaker(id);

In the above code, when the request id is less than 0, an exception is thrown, and when it is greater than 0, the request is normal.
Effect: When I continuously request the request with the id equal to the negative number, I find that the discovery service with the request id equal to the integer is still downgraded in a short period of time, but after a while, the request for the integer id succeeds again, indicating that the service fuse is triggered.
Fusing trigger diagram:


hystrix configuration properties

The hystrix configuration properties are mainly documented in HystrixCommandProperties.class:

Tags: Spring Cloud

Posted by NoMansLand on Wed, 18 May 2022 14:22:01 +0300