1
- ---
2
- title : " 熔断器"
3
- date : 2021-08-26
4
- weight : 5
5
- keywords : ["Kitex", "熔断", "熔断器"]
6
- description : " Kitex 熔断使用指南、原理介绍。"
7
- ---
1
+ # 熔断
2
+
3
+ ## 介绍
8
4
9
5
Kitex 提供了熔断器的实现,但是没有默认开启,需要用户主动使用。下面简单介绍一下如何使用以及 Kitex 熔断器的策略。
10
6
11
- ## 如何使用
7
+ ## 使用方式
12
8
13
9
### 使用示例:
14
10
15
11
``` go
16
-
17
12
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"
22
17
)
23
18
24
19
// GenServiceCBKeyFunc returns a key which determines the granularity of the CBSuite
25
20
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)
28
23
}
29
24
30
25
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
+ ...
52
47
}
48
+
53
49
```
54
50
55
51
### 使用说明
@@ -58,19 +54,14 @@ Kitex 大部分服务治理模块都是通过 middleware 集成,熔断也是
58
54
59
55
- 服务粒度熔断
60
56
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 ` ,即按照方法级别的异常做熔断统计。
63
58
- 实例粒度熔断
64
59
65
- 按照实例粒度进行熔断统计,主要用于解决单实例异常问题,如果触发了实例级别熔断,框架会自动重试。
66
-
67
- 注意,框架自动重试的前提是需要通过 ** WithInstanceMW** 添加,WithInstanceMW 添加的 middleware 会在负载均衡后执行。
68
-
60
+ - 按照实例粒度进行熔断统计,主要用于解决单实例异常问题,如果触发了实例级别熔断,框架会自动重试。
61
+ - 注意,框架自动重试的前提是需要通过 ** WithInstanceMW** 添加,WithInstanceMW 添加的 middleware 会在负载均衡后执行。
69
62
- 熔断阈值及** 阈值变更**
70
63
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 的阈值。
74
65
75
66
## 熔断器作用
76
67
@@ -88,7 +79,7 @@ Kitex 大部分服务治理模块都是通过 middleware 集成,熔断也是
88
79
89
80
### 熔断策略
90
81
91
- ** 熔断器的思路很简单:根据 RPC 的成功失败情况,限制对下游的访问;**
82
+ ** 熔断器的思路很简单:根据**** RPC**** 的成功失败情况,限制对下游的访问;**
92
83
93
84
通常熔断器分为三个时期: CLOSED、OPEN、HALFOPEN;
94
85
@@ -112,16 +103,15 @@ HALFOPEN 时会对下游进行一些有策略的访问,然后根据结果决
112
103
^ | ^
113
104
| v |
114
105
+--- detect succeed --<-[HALFOPEN]-->--+
106
+
115
107
```
116
108
117
109
### 触发策略
118
110
119
111
Kitex 默认提供了三个基本的熔断触发策略:
120
112
121
113
- 连续错误数达到阈值 (ConsecutiveTripFunc)
122
-
123
114
- 错误数达到阈值 (ThresholdTripFunc)
124
-
125
115
- 错误率达到阈值 (RateTripFunc)
126
116
127
117
当然,你可以通过实现 TripFunc 函数来写自己的熔断触发策略;
@@ -142,9 +132,9 @@ Circuitbreaker 会在每次 Fail 或者 Timeout 时,去调用 TripFunc,来
142
132
143
133
该过程是一个逐渐试探下游,并打开的过程;
144
134
145
- 上述的 " 一段时间 " (DetectTimeout) 和 " 若干数目 " (DEFAULT_HALFOPEN_SUCCESSES) 都是可以配置的;
135
+ 上述的 " 一段时间 “ (DetectTimeout) 和 " 若干数目 “ (DEFAULT_HALFOPEN_SUCCESSES) 都是可以配置的;
146
136
147
- ## 统计
137
+ ## 统计算法
148
138
149
139
### 默认参数
150
140
@@ -168,14 +158,12 @@ Options 中的 BucketTime 和 BucketNums,就分别对应了每个桶维护的
168
158
169
159
举个例子:
170
160
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);
173
162
- 在 10.1S 时,执行一次 Succ,则 circuitbreaker 内会发生下述的操作;
174
163
175
- - (1) 检测到 0 号桶已经过期,将其丢弃;
164
+ - (1) 检测到 0 号桶已经过期,将其丢弃;
176
165
- (2) 创建新的 10 号桶,对应 [ 10S,11S);
177
166
- (3) 将该次 Succ 放入 10 号桶内;
178
-
179
167
- 在 10.2S 时,你执行 Successes() 查询窗口内成功数,则你得到的实际统计值是 [ 1S,10.2S) 的数据,而不是 [ 0.2S,10.2S);
180
168
181
169
如果使用分桶计数的办法,这样的抖动是无法避免的,比较折中的一个办法是将桶的个数增多,可以降低抖动的影响;
0 commit comments