Skip to content

Commit db757ae

Browse files
committed
1 parent ac9fe54 commit db757ae

File tree

1 file changed

+42
-54
lines changed

1 file changed

+42
-54
lines changed

content/zh/docs/kitex/Tutorials/service-governance/circuitbreaker.md

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,51 @@
1-
---
2-
title: "熔断器"
3-
date: 2021-08-26
4-
weight: 5
5-
keywords: ["Kitex", "熔断", "熔断器"]
6-
description: "Kitex 熔断使用指南、原理介绍。"
7-
---
1+
# 熔断
2+
3+
## 介绍
84

95
Kitex 提供了熔断器的实现,但是没有默认开启,需要用户主动使用。下面简单介绍一下如何使用以及 Kitex 熔断器的策略。
106

11-
## 如何使用
7+
## 使用方式
128

139
### 使用示例:
1410

1511
```go
16-
1712
import (
18-
...
19-
"github.com/cloudwego/kitex/client"
20-
"github.com/cloudwego/kitex/pkg/circuitbreak"
21-
"github.com/cloudwego/kitex/pkg/rpcinfo"
13+
...
14+
"github.com/cloudwego/kitex/client"
15+
"github.com/cloudwego/kitex/pkg/circuitbreak"
16+
"github.com/cloudwego/kitex/pkg/rpcinfo"
2217
)
2318

2419
// GenServiceCBKeyFunc returns a key which determines the granularity of the CBSuite
2520
func GenServiceCBKeyFunc(ri rpcinfo.RPCInfo) string {
26-
// circuitbreak.RPCInfo2Key returns "$fromServiceName/$toServiceName/$method"
27-
return circuitbreak.RPCInfo2Key(ri)
21+
// circuitbreak.RPCInfo2Key returns "$fromServiceName/$toServiceName/$method"
22+
return circuitbreak.RPCInfo2Key(ri)
2823
}
2924

3025
func main() {
31-
// build a new CBSuite with
32-
cbs := circuitbreak.NewCBSuite(GenServiceCBKeyFunc)
33-
34-
var opts []client.Option
35-
36-
// add to the client options
37-
opts = append(opts, client.WithCircuitBreaker(cbs))
38-
39-
// init client
40-
cli, err := echoservice.NewClient(targetService, opts...)
41-
42-
// update circuit breaker config for a certain key (should be consistent with GenServiceCBKeyFunc)
43-
// this can be called at any time, and will take effect for following requests
44-
cbs.UpdateServiceCBConfig("fromServiceName/toServiceName/method", circuitbreak.CBConfig{
45-
Enable: true,
46-
ErrRate: 0.3, // requests will be blocked if error rate >= 30%
47-
MinSample: 200, // this config takes effect if sampled requests are more than `MinSample`
48-
})
49-
50-
// send requests with the client above
51-
...
26+
// build a new CBSuite with
27+
cbs := circuitbreak.NewCBSuite(GenServiceCBKeyFunc)
28+
29+
var opts []client.Option
30+
31+
// add to the client options
32+
opts = append(opts, client.WithCircuitBreaker(cbs))
33+
34+
// init client
35+
cli, err := echoservice.NewClient(targetService, opts...)
36+
37+
// update circuit breaker config for a certain key (should be consistent with GenServiceCBKeyFunc)
38+
// this can be called at any time, and will take effect for following requests
39+
cbs.UpdateServiceCBConfig("fromServiceName/toServiceName/method", circuitbreak.CBConfig{
40+
Enable: true,
41+
ErrRate: 0.3, // requests will be blocked if error rate >= 30%
42+
MinSample: 200, // this config takes effect if sampled requests are more than `MinSample`
43+
})
44+
45+
// send requests with the client above
46+
...
5247
}
48+
5349
```
5450

5551
### 使用说明
@@ -58,19 +54,14 @@ Kitex 大部分服务治理模块都是通过 middleware 集成,熔断也是
5854

5955
- 服务粒度熔断
6056

61-
按照服务粒度进行熔断统计,通过 WithMiddleware 添加。服务粒度的具体划分取决于 Circuit Breaker Key,既熔断统计的 key,初始化 CBSuite 时需要传入 **GenServiceCBKeyFunc**,默认提供的是 circuitbreak.RPCInfo2Key ,该 key 的格式是 `fromServiceName/toServiceName/method`,即按照方法级别的异常做熔断统计。
62-
57+
- 按照服务粒度进行熔断统计,通过 WithMiddleware 添加。服务粒度的具体划分取决于 Circuit Breaker Key,既熔断统计的 key,初始化 CBSuite 时需要传入 **GenServiceCBKeyFunc**,默认提供的是 circuitbreak.RPCInfo2Key ,该 key 的格式是 `fromServiceName/toServiceName/method`,即按照方法级别的异常做熔断统计。
6358
- 实例粒度熔断
6459

65-
按照实例粒度进行熔断统计,主要用于解决单实例异常问题,如果触发了实例级别熔断,框架会自动重试。
66-
67-
注意,框架自动重试的前提是需要通过 **WithInstanceMW** 添加,WithInstanceMW 添加的 middleware 会在负载均衡后执行。
68-
60+
- 按照实例粒度进行熔断统计,主要用于解决单实例异常问题,如果触发了实例级别熔断,框架会自动重试。
61+
- 注意,框架自动重试的前提是需要通过 **WithInstanceMW** 添加,WithInstanceMW 添加的 middleware 会在负载均衡后执行。
6962
- 熔断阈值及**阈值变更**
7063

71-
默认的熔断阈值是 `ErrRate: 0.5, MinSample: 200`,错误率达到 50% 触发熔断,同时要求统计量 >200。若要调整阈值,调用 CBSuite 的 `UpdateServiceCBConfig``UpdateInstanceCBConfig` 来更新 Key 的阈值。
72-
73-
***
64+
- 默认的熔断阈值是 `ErrRate: 0.5, MinSample: 200`,错误率达到 50% 触发熔断,同时要求统计量 >200。若要调整阈值,调用 CBSuite 的 `UpdateServiceCBConfig``UpdateInstanceCBConfig` 来更新 Key 的阈值。
7465

7566
## 熔断器作用
7667

@@ -88,7 +79,7 @@ Kitex 大部分服务治理模块都是通过 middleware 集成,熔断也是
8879

8980
### 熔断策略
9081

91-
**熔断器的思路很简单:根据 RPC 的成功失败情况,限制对下游的访问;**
82+
**熔断器的思路很简单:根据****RPC****的成功失败情况,限制对下游的访问;**
9283

9384
通常熔断器分为三个时期: CLOSED、OPEN、HALFOPEN;
9485

@@ -112,16 +103,15 @@ HALFOPEN 时会对下游进行一些有策略的访问,然后根据结果决
112103
^ | ^
113104
| v |
114105
+--- detect succeed --<-[HALFOPEN]-->--+
106+
115107
```
116108

117109
### 触发策略
118110

119111
Kitex 默认提供了三个基本的熔断触发策略:
120112

121113
- 连续错误数达到阈值 (ConsecutiveTripFunc)
122-
123114
- 错误数达到阈值 (ThresholdTripFunc)
124-
125115
- 错误率达到阈值 (RateTripFunc)
126116

127117
当然,你可以通过实现 TripFunc 函数来写自己的熔断触发策略;
@@ -142,9 +132,9 @@ Circuitbreaker 会在每次 Fail 或者 Timeout 时,去调用 TripFunc,来
142132

143133
该过程是一个逐渐试探下游,并打开的过程;
144134

145-
上述的 " 一段时间 "(DetectTimeout) 和 " 若干数目 "(DEFAULT_HALFOPEN_SUCCESSES) 都是可以配置的;
135+
上述的 " 一段时间 (DetectTimeout) 和 " 若干数目 (DEFAULT_HALFOPEN_SUCCESSES) 都是可以配置的;
146136

147-
## 统计
137+
## 统计算法
148138

149139
### 默认参数
150140

@@ -168,14 +158,12 @@ Options 中的 BucketTime 和 BucketNums,就分别对应了每个桶维护的
168158

169159
举个例子:
170160

171-
- 你将 10 秒分为了 10 个桶,0 号桶对应了 [0S,1S) 的时间,1 号桶对应 [1S,2S),...,9 号桶对应 [9S,10S);
172-
161+
- 你将 10 秒分为了 10 个桶,0 号桶对应了 [0S,1S) 的时间,1 号桶对应 [1S,2S),…,9 号桶对应 [9S,10S);
173162
- 在 10.1S 时,执行一次 Succ,则 circuitbreaker 内会发生下述的操作;
174163

175-
- (1) 检测到 0 号桶已经过期,将其丢弃;
164+
- (1) 检测到 0 号桶已经过期,将其丢弃;
176165
- (2) 创建新的 10 号桶,对应 [10S,11S);
177166
- (3) 将该次 Succ 放入 10 号桶内;
178-
179167
- 在 10.2S 时,你执行 Successes() 查询窗口内成功数,则你得到的实际统计值是 [1S,10.2S) 的数据,而不是 [0.2S,10.2S);
180168

181169
如果使用分桶计数的办法,这样的抖动是无法避免的,比较折中的一个办法是将桶的个数增多,可以降低抖动的影响;

0 commit comments

Comments
 (0)