Skip to content

Commit 8f12439

Browse files
committed
ApiBoot RateLimiter 的Redis、Nacos配置示例
1 parent 37c4b78 commit 8f12439

File tree

3 files changed

+142
-3
lines changed

3 files changed

+142
-3
lines changed

api-boot-samples/api-boot-sample-rate-limiter/README.md

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333

3434
### 相关配置
3535

36-
| 配置名称 | 默认值 | 描述 |
37-
| --------------------------------------- | ------ | ---------------------------------- |
38-
| `api.boot.rate-limiter.interceptor-url` | /** | 数组类型,可配置多个限流的路径地址 |
36+
| 配置名称 | 默认值 | 描述 |
37+
| ----------------------------------------- | ------ | ---------------------------------- |
38+
| `api.boot.rate-limiter.interceptor-url` | /** | 数组类型,可配置多个限流的路径地址 |
39+
| `api.boot.rate-limiter.enable-global-qps` | false | 是否开启全局限流配置 |
40+
| `api.boot.rate-limiter.global-qps` | 10 | 全局限流QPS |
3941

4042
### QPS定义
4143

@@ -59,3 +61,115 @@ public class ResourceSampleController {
5961
}
6062
```
6163

64+
### 单服务限流
65+
66+
对于单个服务的场景使用限流时,`ApiBoot RateLimiter`内部使用`Google Guava`采用令牌桶的方式实现,具体源码实现详见`GoogleGuavaRateLimiter`类。
67+
68+
> 引用`ApiBoot RateLimiter`的项目如果并未添加`spring-boot-starter-data-redis`依赖,项目中并未初始化`RedisTemplate`时则会采用`Google Guava`方式来进行限流。
69+
70+
### 分布式服务限流
71+
72+
如果你采用微服务、负载均衡的方式进行部署服务时,单服务限流是无法完成预期的效果的,`ApiBoot RateLimiter`内部集成了`Redis`方式来自动完成限流,使用`Redis`后期也利于扩展,如果应用程序过大也可以搭建`Redis 集群`完成限流。
73+
74+
`ApiBoot RateLimiter`内部的`Redis`使用了`SpringCloud GateWay`官方用的`Lua`脚本来保证限流的原子性、线程安全性。
75+
76+
使用`Redis`方式很简单,只需要在项目中添加`Redis`依赖后进行配置后`ApiBoot RateLimiter`就会自动通过`RedisTemplate`来操作`Lua`脚本,步骤如下所示:
77+
78+
##### 第一步:添加Redis依赖
79+
80+
```xml
81+
<!--Redis-->
82+
<dependency>
83+
<groupId>org.springframework.boot</groupId>
84+
<artifactId>spring-boot-starter-data-redis</artifactId>
85+
</dependency>
86+
```
87+
88+
##### 第二步:配置Redis
89+
90+
```yaml
91+
spring:
92+
# redis 相关配置
93+
redis:
94+
password: 123456
95+
```
96+
97+
> 如果你所使用的`Redis`有密码,这里需要进行配置,其他配置参数使用默认值。
98+
99+
#### 测试限流
100+
101+
当我们访问被限流的接口时,`ApiBoot RateLimiter`会自动向`Redis`写入两条数据,`key`如下所示:
102+
103+
```java
104+
// redis key list
105+
qps_rate_limiter.{/test/}.timestamp
106+
qps_rate_limiter.{/test/}.tokens
107+
```
108+
109+
- `qps_rate_limiter.{/test/}.timestamp`:存放上一次请求的时间戳
110+
- `qps_rate_limiter.{/test/}.tokens`:存放剩余的请求令牌数量
111+
112+
`key`的格式为`qps_rate_limiter.{xxx}.timestamp`、`qps_rate_limiter.{xxx}.tokens`。
113+
114+
其中`xxx`为请求接口的路径。
115+
116+
**当然单服务实例也可以使用`Redis`方式**
117+
118+
### 配置中心支持
119+
120+
为了保证有前瞻性突发流量的处理,`ApiBoot RateLimiter`支持了外部配置中心,在配置中心修改接口限流`QPS`后会实时更新到应用程序内。
121+
122+
#### Nacos 配置中心
123+
124+
如果你想使用`Nacos Config`作为`ApiBoot RateLimiter`的外部`QPS`配置方式,那么我们需要进行下面的步骤来完成:
125+
126+
##### 添加依赖
127+
128+
```xml
129+
<!--Nacos-->
130+
<dependency>
131+
<groupId>com.alibaba.boot</groupId>
132+
<artifactId>nacos-config-spring-boot-starter</artifactId>
133+
</dependency>
134+
```
135+
136+
> `ApiBoot`内置了`Nacos Starter`的版本,这里无需添加版本号。
137+
138+
##### 配置参数
139+
140+
我们添加了`Nacos`依赖,那需要进行配置`Nacos Server`的地址,具体`Nacos Server`怎么安装,可以去看我的博客文章或者直接访问[Nacos 快速开始](<https://nacos.io/zh-cn/docs/quick-start.html>)
141+
142+
`Nacos在application.yml`配置如下所示:
143+
144+
```yaml
145+
# nacos config
146+
nacos:
147+
config:
148+
server-addr: 127.0.0.1:8848
149+
```
150+
151+
`8848`是`Nacos Server`的默认端口号,这里直接配置使用。
152+
153+
##### 测试动态修改
154+
155+
我们在启动应用程序时,`ApiBoot RateLimiter`会自动从`Nacos`读取`DATA_ID`为`apiboot-rate-limiter-config`的配置,分组为`DEFAULT_GROUP`的`Properties`类型的配置文件,然后**缓存到本地**。
156+
157+
如果`Nacos`并没有该配置文件,则在**第一次访问接口时自动创建**。
158+
159+
![](<http://image.yuqiyu.com/apiboot/rate-limiter-nacos-config.png>)
160+
161+
`apiboot-rate-limiter-config`配置文件是`Properties`形式的存储的,那么`Key`的生成规则则是把请求接口地址的`/`替换为了`.`,如下所示:
162+
163+
```properties
164+
.resource.=50
165+
.resource.detail=10
166+
```
167+
168+
下面我们来测试修改配置后,`ApiBoot RateLimiter`是否可以实时生效,将`.resource.`修改为20后,控制台会打印如下日志信息:
169+
170+
```sh
171+
Update local current RateLimiter configuration is complete,content:{.resource.=20, .resource.detail=10}
172+
```
173+
174+
当我们再次访问`/resource/`接口时就会发现限流的`QPS`一秒内只允许访问`20`次。
175+

api-boot-samples/api-boot-sample-rate-limiter/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,16 @@
4242
<groupId>org.minbox.framework</groupId>
4343
<artifactId>api-boot-starter-rate-limiter</artifactId>
4444
</dependency>
45+
<!--Redis-->
4546
<dependency>
4647
<groupId>org.springframework.boot</groupId>
4748
<artifactId>spring-boot-starter-data-redis</artifactId>
4849
</dependency>
50+
<!--Nacos-->
51+
<dependency>
52+
<groupId>com.alibaba.boot</groupId>
53+
<artifactId>nacos-config-spring-boot-starter</artifactId>
54+
</dependency>
4955
</dependencies>
5056

5157
<!--ApiBoot版本依赖-->
@@ -60,4 +66,13 @@
6066
</dependency>
6167
</dependencies>
6268
</dependencyManagement>
69+
<build>
70+
<plugins>
71+
<plugin>
72+
<groupId>org.springframework.boot</groupId>
73+
<artifactId>spring-boot-maven-plugin</artifactId>
74+
<version>2.1.4.RELEASE</version>
75+
</plugin>
76+
</plugins>
77+
</build>
6378
</project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
spring:
22
application:
33
name: api-boot-sample-rate-limiter
4+
# redis 相关配置
5+
redis:
6+
password: 123456
47

58
api:
69
boot:
710
# 配置限流的拦截路径
811
rate-limiter:
912
interceptor-url: /**
13+
enable-global-qps: true
14+
global-qps: 20
15+
16+
# nacos config
17+
nacos:
18+
config:
19+
server-addr: 127.0.0.1:8848

0 commit comments

Comments
 (0)