一、分布式系统面临的问题
复杂分布式体系结构中的应用程序有几十个依赖项,每个依赖项在某个时候都会不可避免地失败。
[En]
An application in a complex distributed architecture has dozens of dependencies, and each dependency will inevitably fail at some time.
服务雪崩
多个微服务之间调用的时候,假设微服务A调用微服务B和微服务C,微服务B和微服务C又调用其它的微服务,这就是所谓的"扇出"。如果扇出的链路上某个微服务的调用响应时间过长或者不可用,对微服务A的调用就会占用越来越多的系统资源,进而引起系统崩溃,所谓的"雪崩效应".
对于高流量应用程序,单个后端依赖可能会导致所有服务器上的所有资源在几秒钟内饱和。比故障更糟糕的是,这些应用程序还可能导致服务之间的延迟增加、备份队列、线程和其他系统资源紧张,从而导致整个系统出现更多的级联故障。这些都表明需要隔离和管理故障和延迟,以便单个依赖项的故障不能取消整个应用程序或系统。
[En]
For high traffic applications, a single back-end dependency may cause all resources on all servers to be saturated within a few seconds. Worse than failure, these applications may also lead to increased delays between services, tight backup queues, threads, and other system resources, resulting in more cascading failures of the entire system. These all indicate the need to isolate and manage failures and delays so that the failure of a single dependency cannot cancel the entire application or system.
所以,
通常,当您发现某个模块的实例出现故障时,该模块仍会接收流量。然后,有问题的模块调用其他模块,这将导致级联故障或雪崩。
[En]
Usually, when you find that an instance of a module fails, the module still receives traffic. Then the module in question calls other modules, which will cause cascading failures, or avalanches.
二、Hystrix是什么
1、是什么
Hystrix是一个用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,Hystrix能够保证在一个依赖出问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。
"断路器"本身是一种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个符合预期的、可处理的备选响应(FallBack),而不是长时间的等待或者抛出调用方无法处理的异常,这样就保证了服务调用方的线程不会被长时间、不必要地占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。