1313
1414#### 实现方式
1515
16- - 控制单位时间内的请求数量
16+ 控制单位时间内的请求数量。
1717
1818``` java
1919
@@ -54,16 +54,17 @@ public class Counter {
5454
5555```
5656
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 个请求),远远大于了每秒钟处理数量的阈值。
6062
6163### 滑动窗口
6264
6365#### 实现方式
6466
65- - 滑动窗口是对计数器方式的改进, 增加一个时间粒度的度量单位
66- - 把一分钟分成若干等分(6 份,每份 10 秒), 在每一份上设置独立计数器,在 00:00-00:09 之间发生请求计数器累加 1.当等分数量越大限流统计就越详细
67+ 滑动窗口是对计数器方式的改进,增加一个时间粒度的度量单位,把一分钟分成若干等分(6 份,每份 10 秒),在每一份上设置独立计数器,在 00:00-00:09 之间发生请求计数器累加 1。当等分数量越大限流统计就越详细。
6768
6869``` java
6970package com.example.demo1.service ;
@@ -86,7 +87,7 @@ public class TimeWindow {
8687 */
8788 private int max;
8889
89- public TimeWindow (int max , int seconds ) {
90+ public TimeWindow (int max , int seconds ) {
9091 this . seconds = seconds;
9192 this . max = max;
9293
@@ -109,10 +110,10 @@ public class TimeWindow {
109110
110111 public static void main (String [] args ) throws Exception {
111112
112- final TimeWindow timeWindow = new TimeWindow (10 , 1 );
113+ final TimeWindow timeWindow = new TimeWindow (10 , 1 );
113114
114115 // 测试3个线程
115- IntStream . range(0 , 3 ). forEach((i) - > {
116+ IntStream . range(0 , 3 ). forEach((i) - > {
116117 new Thread (() - > {
117118
118119 while (true ) {
@@ -193,7 +194,7 @@ public class TimeWindow {
193194
194195#### 实现方式
195196
196- - 规定固定容量的桶, 有水进入, 有水流出. 对于流进的水我们无法估计进来的数量、速度, 对于流出的水我们可以控制速度.
197+ 规定固定容量的桶, 有水进入, 有水流出。 对于流进的水我们无法估计进来的数量、速度, 对于流出的水我们可以控制速度。
197198
198199``` java
199200public class LeakBucket {
@@ -216,7 +217,7 @@ public class LeakBucket {
216217
217218 public boolean limit () {
218219 long now = System . currentTimeMillis();
219- nowSize = Math . max(0 , (nowSize - (now - time) * rate));
220+ nowSize = Math . max(0 , (nowSize - (now - time) * rate));
220221 time = now;
221222 if ((nowSize + 1 ) < total) {
222223 nowSize++ ;
@@ -233,7 +234,7 @@ public class LeakBucket {
233234
234235#### 实现方式
235236
236- - 规定固定容量的桶, token 以固定速度往桶内填充, 当桶满时 token 不会被继续放入, 每过来一个请求把 token 从桶中移除, 如果桶中没有 token 不能请求
237+ 规定固定容量的桶, token 以固定速度往桶内填充, 当桶满时 token 不会被继续放入, 每过来一个请求把 token 从桶中移除, 如果桶中没有 token 不能请求。
237238
238239``` java
239240public class TokenBucket {
@@ -256,7 +257,7 @@ public class TokenBucket {
256257
257258 public boolean limit () {
258259 long now = System . currentTimeMillis();
259- nowSize = Math . min(total, nowSize + (now - time) * rate);
260+ nowSize = Math . min(total, nowSize + (now - time) * rate);
260261 time = now;
261262 if (nowSize < 1 ) {
262263 // 桶里没有token
@@ -275,7 +276,7 @@ public class TokenBucket {
275276
276277### spring cloud gateway
277278
278- - spring cloud gateway 默认使用 redis 进行限流, 笔者一般只是修改修改参数属于拿来即用. 并没有去从头实现上述那些算法.
279+ - spring cloud gateway 默认使用 redis 进行限流, 笔者一般只是修改修改参数属于拿来即用, 并没有去从头实现上述那些算法。
279280
280281``` xml
281282<dependency >
@@ -354,12 +355,12 @@ spring:
354355` ` ` json
355356[
356357 {
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,
363364 " clusterMode " : false
364365 }
365366]
@@ -375,4 +376,4 @@ spring:
375376
376377### 总结
377378
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