Hystrix (prevent service avalanche)-service circuit breaker

Service avalanche

  • When calling between multiple services, suppose that microservice A calls microservice B and microservice C, and microservice B and microservice C call other microservices. This is the so-called "fan-out". If the fan-out chain If the response time of a microservice call on the road is too long or unavailable, the call to microservice A will occupy more and more system resources, which will cause the system to crash. This is the so-called "service avalanche"
  • For high-traffic applications, a single back-end dependency may cause all resources on all servers to saturate in a few seconds. These applications may also cause increased delays between services, strain on backup queues, threads, and other system resources, leading to more cascading failures in the entire system. These all indicate the need to isolate and manage failures and delays in order to facilitate The failure of a single dependency will not cancel the entire application or system

Hystrix

  • Hystrix is ​​an open source library used to deal with the delay and fault tolerance of distributed systems. In distributed systems, many dependencies will inevitably fail to call, such as timeouts, exceptions, etc., Hystrix can guarantee that in the case of a dependency problem , Will not cause overall service failure, avoid cascading failures, and improve the resilience of distributed systems.
  • Hystrix ("circuit breaker") itself is a kind of switching device. When a service unit fails, through the fault monitoring of the circuit breaker, it returns to the caller an alternative response (FallBack) that the service expects and can be processed. Rather than waiting for a long time or throwing exceptions that cannot be handled by the calling method, this ensures that the thread of the service caller will not be unnecessarily occupied for a long time, thereby avoiding the spread of faults in the distributed system and even an avalanche

What can Hystrix do

  • Service degradation
  • Service fusing
  • Service current limit
  • real time monitoring

Service fusing

  • The fuse mechanism is a micro-service link protection mechanism that responds to the service avalanche
  • When a microservice of the fan-out link is unavailable or the response time is too long, the service will be degraded, and the call of the microservice of the node will be fuse, and the wrong response information will be returned quickly. When it is detected that the microservice invocation response of the node is normal, the invocation link is restored. In the SpringCloud framework, the fuse mechanism is implemented by Hystrix. Hystrix will monitor the calls between microservices. When the failed calls reach a certain threshold (20 calls failed within 5 seconds), the fuse mechanism will be turned on.
  • Use annotations@HystrixCommand
@RestController
public class HelloController {
    
    

    @Autowired
    ServiceAFeignClient serviceAFeignClient;

    @RequestMapping("/call")
    @HystrixCommand(fallbackMethod = "hystrixCall")
    public String call() {
    
    
        String result = serviceAFeignClient.hello();
        if (!result.equals("h")){
    
    
            //抛出异常,然后执行备用方法
            throw new RuntimeException("数据不对");
        }
        return "b访问了a的hello(),返回的结果为" + result;
    }

    public String hystrixCall(){
    
    
        String result = serviceAFeignClient.hello();
        return "Hystrix调用备份方法"+result;
    }
}

In the above example, if the Call method fails to a certain threshold (throw throws an exception), then the alternate method hystrixCall will be called, and the service circuit breaker will be turned on with only one annotation.
Then you need to add an annotation in the startup class to EnableHystrixturn on the fuse support:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44976835/article/details/115000295