我正在使用 spring boot 和 spring cloud 开发微服务。我开始了解 hystrix 和断路器模式。我知道断路器用于在我依赖于获取数据的下游微服务出现错误的情况下以备用响应进行响应。我的问题是,如果我没有任何有意义的替代响应可供提供,我为什么还需要断路器?
最佳答案
简短回答:主要是为了停止复杂分布式系统中的级联故障。
I don't have any meaningful alternative response to provide, why would I need a circuit breaker at all?
只有当您的服务器只提供一个 REST 端点(和一个 HTTP 动词)时,这个问题才有意义。但几乎总是这样。即使是“微”服务也会有多个端点+多个http动词的组合。您不希望一个端点卡在缓慢的上游服务上,并在线程继续等待并最终导致整个应用程序崩溃后堆积线程。
看看官方documentation
What Is Hystrix For? --- Hystrix is designed to do the following:
- Give protection from and control over latency and failure from dependencies accessed (typically over the network) via third-party
client libraries.- Stop cascading failures in a complex distributed system.
- Fail fast and rapidly recover.
- Fallback and gracefully degrade when possible.
- Enable near real-time monitoring, alerting, and operational control.
“在可能的情况下回退并优雅地降级” 只是 hystrix 提供的功能之一。
关于java - 为什么 hystrix 或任何其他微服务断路器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40669943/