为什么 hystrix 或任何其他微服务断路器?
why hystrix or any other circuit breaker for a microservice?
我正在使用 spring 引导和 spring 云开发微服务。我开始了解 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 提供的功能之一。
如果您重构整体式应用程序以将其拆分为多个微服务,Hystrix 也可能很有用。
将其投入生产时,您可能希望将旧的单体代码保留一段时间作为备用响应。因此,如果微服务不可用,则只会执行旧代码,因此基本上可以降低风险。如果一切正常,您可以从整体中删除旧代码并继续使用微服务。
通过扩展 HystrixCommand class 这可以很容易地完成。
public class MicroserviceCommand extends HystrixCommand<String>
{
@Override
protected String run()
{
//return response from your new microservice
}
@Override
protected String getFallback()
{
//microservice is not available,
//so execute old code which was not removed from application yet
}
}
我正在使用 spring 引导和 spring 云开发微服务。我开始了解 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 提供的功能之一。
如果您重构整体式应用程序以将其拆分为多个微服务,Hystrix 也可能很有用。 将其投入生产时,您可能希望将旧的单体代码保留一段时间作为备用响应。因此,如果微服务不可用,则只会执行旧代码,因此基本上可以降低风险。如果一切正常,您可以从整体中删除旧代码并继续使用微服务。
通过扩展 HystrixCommand class 这可以很容易地完成。
public class MicroserviceCommand extends HystrixCommand<String>
{
@Override
protected String run()
{
//return response from your new microservice
}
@Override
protected String getFallback()
{
//microservice is not available,
//so execute old code which was not removed from application yet
}
}