断路器(熔断器)
远程服务或资源的故障达到阈值后,暂时阻止访问,以避免反复失败和重试。
结构与工作原理
按步骤查看箭头所示的流程。
故障反复出现后,断路器拦截调用,再用有限的试探请求检查是否恢复。
预期收益
限制故障影响,减少对失效服务反复重试所浪费的资源。
权衡
阈值过低会增加因短暂波动而误触发熔断的概率。
不适用于输入校验等高基线错误率属于正常行为的场景。
运维与应用指南
综合说明,与有原文摘录支持的条目分开。实际应用前请先验证。
何时使用
当微服务之间的远程调用反复超时或返回 5xx 时,可以暂时拦截调用,之后进入半开状态试探依赖是否已恢复。
应用场景
大型电商订单 API 同步调用库存、支付和通知服务。支付延迟骤增时,订单 API 的线程或连接可能耗尽,导致所有订单失败。在支付调用上配置断路器,错误率超过阈值时迅速启用回退,在半开状态下用有限请求确认恢复。要求包括保护订单成功率、遏制故障扩散,并为运维提供告警和仪表板。
部署原则
- 在网关或边缘集中部署
- 将其与路由、可观测性和策略一并部署到服务网格或边车代理
- 使用客户端库嵌入调用方
- 多集群或多区域场景下,使用由共享存储支持的分布式协调器协调状态。
本地部署
将服务网格或 API 网关部署到本地 Kubernetes 或虚拟机中。按照环境差异调整各依赖的阈值、超时和回退行为。先统一集中配置发布流程和可观测性技术栈。
云环境
使用托管 Kubernetes、服务网格或网关策略来实现。在发布或升级期间监控断路器状态和回退,并结合托管指标、告警及自动扩缩容。
Kubernetes
可使用 Istio DestinationRule 的 OutlierDetection、服务网格的断路器策略,或 Hystrix、Resilience4j、opossum 等库。通过 ConfigMap 或 CRD 管理阈值和重置等待时间,并观察 Pod 或节点排空及发布期间的状态转换。
故障应对
常见故障包括依赖延迟或错误飙升、误熔断、半开恢复失败、回退失败或未告警的熔断。先用告警和指标核查断路器状态与依赖的 SLO,再检查阈值、超时、回退设置及近期发布。必要时手动熔断、绕过问题路径或分流。缓解后验证半开恢复并调整设置以防复发。
14 个维度的证据
反映调研时的证据收录情况,请另行判断来源是否准确、是否仍然有效。
定义与概览有证据
远程服务或资源的故障达到阈值后,暂时阻止访问,以避免反复失败和重试。
Temporarily block access to a remote service or resource after failures reach a threshold解决的问题有证据
缓解分布式远程调用中的短暂故障、过载、延迟或超时引发的级联故障。
In a distributed environment, calls to remote resources and services can fail because of transient faults.关键组件与资源有证据
可配置超时、错误阈值(%)、重置等待时间及回退行为。
const options = { timeout: 3000, errorThresholdPercentage: 50, resetTimeout: 30000, fallback: () => 'Service temporarily unavailable' };数据与资产放置原则有证据
在网关或边缘进行集中控制,以实现保护。
Gateway-level breaker: centralized control at edge, protects ...代表性使用场景有证据
微服务远程调用反复超时或失败时,拦截调用并检测依赖是否恢复。
When multiple microservices collaborate to handle requests, one or more services might become unavailable or exhibit high latency.应用场景示例有证据
可观测性仪表板或连接器发生故障时,拦截流水线的日志调用。
Pipelines interact with dashboards, like Sumo Logic or ELK Stack to log key performance indicators (KPIs)不适用情形与注意事项有证据
不适用于输入校验等高基线错误率属于正常行为的场景。
User input validation services may reject `30%` of requests. A circuit breaker would stay permanently open.本地部署:开发、发布与运维有证据
本地环境中,为容器或虚拟机上的工作负载使用服务网格或网关中的断路器。
Deploy Kong Mesh anywhere: on Kubernetes, in the cloud, on-premises, with containers or on virtual machines.云环境:开发、发布与运维有证据
云环境中的托管 Kubernetes 升级期间,监控断路器状态和回退行为。
// Monitor circuit breaker state during upgradesKubernetes:开发、发布与运维有证据
在 Kubernetes 上可通过 Istio DestinationRule 的 OutlierDetection 等机制配置断路器。
For configuring a circuit breaker in Istio we are using `OutlierDetection` object.故障应对有证据
静默熔断和回退失败会延迟发现故障。应监控状态转换、配置告警并检查依赖健康状况。
Silent trips: The breaker stays “Open” and serves degraded responses for an extended period while emitting no state-transition metric or alert优势有证据
限制故障影响,减少对失效服务反复重试所浪费的资源。
The benefit of this pattern is that it limits the consequences of a failure by wrapping the interface to that element不足与权衡有证据
阈值过低会增加因短暂波动而误触发熔断的概率。
The lower the `error_threshold`, the higher the probability of a false-positive circuit open.代表性技术与工具有证据
代表性工具包括 Hystrix 和 Ribbon。
This example demonstrates how to use Hystrix circuit breaker and the Ribbon Load Balancing.更多参考资料 (22)
- Circuit Breaker Pattern - Azure Architecture Center | Microsoft Learn ↗
- Circuit breaker pattern - AWS Prescriptive Guidance ↗
- What is Circuit breaker? Meaning, Architecture, Examples, Use Cases, and How to Measure It (2026 Guide) - SRE School ↗
- Circuit Breaker: How to Keep One Failure from Taking Down Everything ↗
- Implementing the Circuit Breaker pattern - .NET | Microsoft Learn ↗
- The Circuit Breaker Pattern - Dos and Don'ts — AKF Partners ↗
- docs/operations/failurehandling.md ↗
- Tactics and Patterns for Software Robustness | CMU Software Engineering Institute ↗
- Dynamic configuration management and circuit breakers in Kubernetes - rastko ↗
- Azure Kubernetes Service (AKS) production upgrade strategies - Azure Kubernetes Service | Microsoft Learn ↗
- ECS vs Kubernetes: Paying for Portability Up Front ↗
- Istio and mTLS — securing and observing your service mesh ↗
- Kong Mesh: Enterprise Service Mesh for Kubernetes | Kong Inc. ↗
- Kubernetes Circuit Breaker & Load Balancer Example ↗
- Circuit breaker and retries on Kubernetes with Istio and Spring Boot ↗
- Zero-Downtime Migration to Azure Kubernetes Service (AKS) - Azure Kubernetes Service | Microsoft Learn ↗
- Your Circuit Breaker is Misconfigured - Shopify ↗
- Circuit Breaker | arc42 Quality Model ↗
- Circuit Breaker Policy Fine-tuning Best Practice - .NET Blog ↗
- Circuit Breaker Trade Offs: When Not to Use Them ↗
- Learn Trade-Offs and Limitations of Circuit Breakers | Designing Resilient Architectures ↗
- Google SRE - Troubleshooting Methodology: A Learning Path ↗