13
13
14
14
#### 实现方式
15
15
16
- - 控制单位时间内的请求数量
16
+ 控制单位时间内的请求数量。
17
17
18
18
``` java
19
19
@@ -54,16 +54,17 @@ public class Counter {
54
54
55
55
```
56
56
57
- - 劣势
58
- - 假设在 00:01 时发生一个请求,在 00:01-00:58 之间不在发送请求,在 00:59 时发送剩下的所有请求 ` n-1 ` (n 为限流请求数量),在下一分钟的 00:01 发送 n 个请求,这样在 2 秒钟内请求到达了 ` 2n - 1 ` 个.
59
- - 设每分钟请求数量为 60 个,每秒可以处理 1 个请求,用户在 00:59 发送 60 个请求,在 01:00 发送 60 个请求 此时 2 秒钟有 120 个请求(每秒 60 个请求),远远大于了每秒钟处理数量的阈值
57
+ 劣势:
58
+
59
+ 假设在 00:01 时发生一个请求,在 00:01-00:58 之间不在发送请求,在 00:59 时发送剩下的所有请求 ` n-1 ` (n 为限流请求数量),在下一分钟的 00:01 发送 n 个请求,这样在 2 秒钟内请求到达了 ` 2n - 1 ` 个。
60
+
61
+ 设每分钟请求数量为 60 个,每秒可以处理 1 个请求,用户在 00:59 发送 60 个请求,在 01:00 发送 60 个请求 此时 2 秒钟有 120 个请求(每秒 60 个请求),远远大于了每秒钟处理数量的阈值。
60
62
61
63
### 滑动窗口
62
64
63
65
#### 实现方式
64
66
65
- - 滑动窗口是对计数器方式的改进, 增加一个时间粒度的度量单位
66
- - 把一分钟分成若干等分(6 份,每份 10 秒), 在每一份上设置独立计数器,在 00:00-00:09 之间发生请求计数器累加 1.当等分数量越大限流统计就越详细
67
+ 滑动窗口是对计数器方式的改进,增加一个时间粒度的度量单位,把一分钟分成若干等分(6 份,每份 10 秒),在每一份上设置独立计数器,在 00:00-00:09 之间发生请求计数器累加 1。当等分数量越大限流统计就越详细。
67
68
68
69
``` java
69
70
package com.example.demo1.service ;
@@ -86,7 +87,7 @@ public class TimeWindow {
86
87
*/
87
88
private int max;
88
89
89
- public TimeWindow (int max , int seconds ) {
90
+ public TimeWindow (int max , int seconds ) {
90
91
this . seconds = seconds;
91
92
this . max = max;
92
93
@@ -109,10 +110,10 @@ public class TimeWindow {
109
110
110
111
public static void main (String [] args ) throws Exception {
111
112
112
- final TimeWindow timeWindow = new TimeWindow (10 , 1 );
113
+ final TimeWindow timeWindow = new TimeWindow (10 , 1 );
113
114
114
115
// 测试3个线程
115
- IntStream . range(0 , 3 ). forEach((i) - > {
116
+ IntStream . range(0 , 3 ). forEach((i) - > {
116
117
new Thread (() - > {
117
118
118
119
while (true ) {
@@ -193,7 +194,7 @@ public class TimeWindow {
193
194
194
195
#### 实现方式
195
196
196
- - 规定固定容量的桶, 有水进入, 有水流出. 对于流进的水我们无法估计进来的数量、速度, 对于流出的水我们可以控制速度.
197
+ 规定固定容量的桶, 有水进入, 有水流出。 对于流进的水我们无法估计进来的数量、速度, 对于流出的水我们可以控制速度。
197
198
198
199
``` java
199
200
public class LeakBucket {
@@ -216,7 +217,7 @@ public class LeakBucket {
216
217
217
218
public boolean limit () {
218
219
long now = System . currentTimeMillis();
219
- nowSize = Math . max(0 , (nowSize - (now - time) * rate));
220
+ nowSize = Math . max(0 , (nowSize - (now - time) * rate));
220
221
time = now;
221
222
if ((nowSize + 1 ) < total) {
222
223
nowSize++ ;
@@ -233,7 +234,7 @@ public class LeakBucket {
233
234
234
235
#### 实现方式
235
236
236
- - 规定固定容量的桶, token 以固定速度往桶内填充, 当桶满时 token 不会被继续放入, 每过来一个请求把 token 从桶中移除, 如果桶中没有 token 不能请求
237
+ 规定固定容量的桶, token 以固定速度往桶内填充, 当桶满时 token 不会被继续放入, 每过来一个请求把 token 从桶中移除, 如果桶中没有 token 不能请求。
237
238
238
239
``` java
239
240
public class TokenBucket {
@@ -256,7 +257,7 @@ public class TokenBucket {
256
257
257
258
public boolean limit () {
258
259
long now = System . currentTimeMillis();
259
- nowSize = Math . min(total, nowSize + (now - time) * rate);
260
+ nowSize = Math . min(total, nowSize + (now - time) * rate);
260
261
time = now;
261
262
if (nowSize < 1 ) {
262
263
// 桶里没有token
@@ -275,7 +276,7 @@ public class TokenBucket {
275
276
276
277
### spring cloud gateway
277
278
278
- - spring cloud gateway 默认使用 redis 进行限流, 笔者一般只是修改修改参数属于拿来即用. 并没有去从头实现上述那些算法.
279
+ - spring cloud gateway 默认使用 redis 进行限流, 笔者一般只是修改修改参数属于拿来即用, 并没有去从头实现上述那些算法。
279
280
280
281
``` xml
281
282
<dependency >
@@ -354,12 +355,12 @@ spring:
354
355
` ` ` json
355
356
[
356
357
{
357
- " resource " : " /hello" ,
358
- " limitApp " : " default" ,
359
- " grade " : 1,
360
- " count " : 1,
361
- " strategy " : 0,
362
- " controlBehavior " : 0,
358
+ " resource " : " /hello" ,
359
+ " limitApp " : " default" ,
360
+ " grade " : 1,
361
+ " count " : 1,
362
+ " strategy " : 0,
363
+ " controlBehavior " : 0,
363
364
" clusterMode " : false
364
365
}
365
366
]
@@ -375,4 +376,4 @@ spring:
375
376
376
377
### 总结
377
378
378
- > sentinel 和 spring cloud gateway 两个框架都是很好的限流框架, 但是在我使用中还没有将[ spring-cloud-alibaba] ( https://github.com/alibaba/spring-cloud-alibaba ) 接入到项目中进行使用, 所以我会选择** spring cloud gateway** , 当接入完整的或者接入 Nacos 项目使用 setinel 会有更加好的体验.
379
+ > sentinel 和 spring cloud gateway 两个框架都是很好的限流框架, 但是在我使用中还没有将[ spring-cloud-alibaba] ( https://github.com/alibaba/spring-cloud-alibaba ) 接入到项目中进行使用, 所以我会选择** spring cloud gateway** , 当接入完整的或者接入 Nacos 项目使用 setinel 会有更加好的体验.
0 commit comments