Skip to content

Commit 0595a07

Browse files
committed
docs: 增加文章[5种限流算法,7种限流方式,挡住突发流量?](https://www.wdbyte.com/java/rate-limiter.html)
1 parent 1a65e5f commit 0595a07

4 files changed

Lines changed: 42 additions & 15 deletions

File tree

README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@
1818
文章内容也都可以访问网站 [https://www.wdbyte.com](https://www.wdbyte.com) 进行阅读。
1919

2020

21+
## ⏳ Java 开发
22+
- [5种限流算法,7种限流方式,挡住突发流量?](https://www.wdbyte.com/java/rate-limiter.html)
23+
- [Java 中拼接 String 的 N 种方式](https://www.wdbyte.com/java/string-concat.html)
24+
- [字符图案,我用字符画个冰墩墩](https://www.wdbyte.com/java/char-image.html)
25+
- [Java 中 RMI 的使用](https://www.wdbyte.com/2021/05/java/java-rmi/)
26+
- [如何使用 Github Actions 自动抓取每日必应壁纸?](https://www.wdbyte.com/2021/03/bing-wallpaper-github-action/)
27+
- [三种骚操作绕过迭代器遍历时的数据修改异常](https://www.wdbyte.com/2021/02/develop/interator-update/)
28+
- [Guava - 拯救垃圾代码,写出优雅高效,效率提升N倍](https://www.wdbyte.com/2020/10/develop/google-guava/)
29+
- [「1024」专属序猿的快乐,惊奇迷惑代码大赏](https://www.wdbyte.com/2020/10/2020-1024/)
30+
- [一篇有趣的负载均衡算法实现](https://www.wdbyte.com/2020/05/algorithm/load-balancing/)
31+
- [撸了个多线程断点续传下载器,我从中学习到了这些知识](https://www.wdbyte.com/2020/07/tool/java-download/)
32+
33+
- [Java 开发的编程噩梦,这些坑你没踩过算我输](https://www.wdbyte.com/2020/08/java/java-code-standards/)
34+
- [如何使用 Lombok 进行优雅的编码](https://www.wdbyte.com/2018/12/develop/tool-lombok/)
35+
- [使用MyBatis Generator自动生成Model、Dao、Mapper相关代码](https://www.wdbyte.com/2017/11/develop/tool-mybatis-generator/)
36+
37+
2138
## 🌿 SpringBoot 2.x 教程
2239

2340
使用 **Spring Boot** 可以快速的创建一个基于Spring 的、独立的、生产级的应用程序,并且可以直接运行。Spring Boot 采用习惯性配置,整合大量 Spring 组建和第三方库,让你只需要少量的修改就可以轻松上手。
@@ -140,21 +157,6 @@ Java 版本任你发,我用 Java 8 。但是多学点这种装x技巧总没错
140157
-
141158
-
142159

143-
## ⏳ Java 开发
144-
- [Java 中拼接 String 的 N 种方式](https://www.wdbyte.com/java/string-concat.html)
145-
- [字符图案,我用字符画个冰墩墩](https://www.wdbyte.com/java/char-image.html)
146-
- [Java 中 RMI 的使用](https://www.wdbyte.com/2021/05/java/java-rmi/)
147-
- [如何使用 Github Actions 自动抓取每日必应壁纸?](https://www.wdbyte.com/2021/03/bing-wallpaper-github-action/)
148-
- [三种骚操作绕过迭代器遍历时的数据修改异常](https://www.wdbyte.com/2021/02/develop/interator-update/)
149-
- [Guava - 拯救垃圾代码,写出优雅高效,效率提升N倍](https://www.wdbyte.com/2020/10/develop/google-guava/)
150-
- [「1024」专属序猿的快乐,惊奇迷惑代码大赏](https://www.wdbyte.com/2020/10/2020-1024/)
151-
- [一篇有趣的负载均衡算法实现](https://www.wdbyte.com/2020/05/algorithm/load-balancing/)
152-
- [撸了个多线程断点续传下载器,我从中学习到了这些知识](https://www.wdbyte.com/2020/07/tool/java-download/)
153-
154-
- [Java 开发的编程噩梦,这些坑你没踩过算我输](https://www.wdbyte.com/2020/08/java/java-code-standards/)
155-
- [如何使用 Lombok 进行优雅的编码](https://www.wdbyte.com/2018/12/develop/tool-lombok/)
156-
- [使用MyBatis Generator自动生成Model、Dao、Mapper相关代码](https://www.wdbyte.com/2017/11/develop/tool-mybatis-generator/)
157-
158160
## 🧰 工具技巧
159161

160162
>“工欲善其事,必先利其器”
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
local count = redis.call("incr",KEYS[1])
2+
if count == 1 then
3+
redis.call('expire',KEYS[1],ARGV[2])
4+
end
5+
if count > tonumber(ARGV[1]) then
6+
return 0
7+
end
8+
return 1
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--KEYS[1]: 限流 key
2+
--ARGV[1]: 时间戳 - 时间窗口
3+
--ARGV[2]: 当前时间戳(作为score)
4+
--ARGV[3]: 阈值
5+
--ARGV[4]: score 对应的唯一value
6+
-- 1. 移除时间窗口之前的数据
7+
redis.call('zremrangeByScore', KEYS[1], 0, ARGV[1])
8+
-- 2. 统计当前元素数量
9+
local res = redis.call('zcard', KEYS[1])
10+
-- 3. 是否超过阈值
11+
if (res == nil) or (res < tonumber(ARGV[3])) then
12+
redis.call('zadd', KEYS[1], ARGV[2], ARGV[4])
13+
return 1
14+
else
15+
return 0
16+
end

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<version>1.0.0-SNAPSHOT</version>
1010
<modules>
1111
<module>core-java-modules</module>
12+
<module>core-java-rate-limiter</module>
1213
</modules>
1314
<name>parent-modules</name>
1415
<description>Parent for all java modules</description>

0 commit comments

Comments
 (0)