File tree Expand file tree Collapse file tree 3 files changed +102
-0
lines changed
api-boot-samples/api-boot-sample-rate-limiter
src/main/java/org/minbox/framework/api/boot/sample Expand file tree Collapse file tree 3 files changed +102
-0
lines changed Original file line number Diff line number Diff line change 1+
2+
3+ ## ApiBoot RateLimiter
4+
5+ ` ApiBoot RateLimiter ` 基于拦截器的实现,封装了` Google ` 的` 令牌桶方式 ` 的限流实现,可通过注解配置每个接口的流量` QPS ` (每秒允许的流量请求)。
6+
7+ > 注意:目前不支持分布式系统的流量限制,会在下个版本进行更新添加。
8+
9+ ### 添加依赖
10+
11+ ``` xml
12+ <dependencies >
13+ <!-- ApiBoot RateLimiter-->
14+ <dependency >
15+ <groupId >org.minbox.framework</groupId >
16+ <artifactId >api-boot-starter-rate-limiter</artifactId >
17+ </dependency >
18+ <!-- 省略其他依赖-->
19+ </dependencies >
20+ <dependencyManagement >
21+ <dependencies >
22+ <!-- ApiBoot 版本依赖-->
23+ <dependency >
24+ <groupId >org.minbox.framework</groupId >
25+ <artifactId >api-boot-dependencies</artifactId >
26+ <version >2.0.6.RELEASE</version >
27+ <type >pom</type >
28+ <scope >import</scope >
29+ </dependency >
30+ </dependencies >
31+ </dependencyManagement >
32+ ```
33+
34+ ### 相关配置
35+
36+ | 配置名称 | 默认值 | 描述 |
37+ | --------------------------------------- | ------ | ---------------------------------- |
38+ | ` api.boot.rate-limiter.interceptor-url ` | /** | 数组类型,可配置多个限流的路径地址 |
39+
40+ ### QPS定义
41+
42+ 限流注解` RateLimiter ` 配置使用如下所示:
43+
44+ ``` java
45+ @RestController
46+ @RequestMapping (value = " /resource" )
47+ public class ResourceSampleController {
48+ /**
49+ * QPS = 10
50+ * 配置该接口每秒只允许访问10次
51+ *
52+ * @return
53+ */
54+ @RequestMapping (value = " /" )
55+ @RateLimiter (QPS = 10 )
56+ public UserInfo user () {
57+ return new UserInfo (" admin" );
58+ }
59+ }
60+ ```
61+
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright [2019] [恒宇少年 - 于起宇]
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ *
16+ */
17+
18+ package org .minbox .framework .api .boot .sample ;
19+
20+ import org .springframework .boot .SpringApplication ;
21+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
22+
23+ /**
24+ * ApiBoot Rate Limiter Application
25+ *
26+ * @author:恒宇少年 - 于起宇
27+ * <p>
28+ * DateTime:2019-04-26 16:09
29+ * Blog:http://blog.yuqiyu.com
30+ * WebSite:http://www.jianshu.com/u/092df3f77bca
31+ * Gitee:https://gitee.com/hengboy
32+ * GitHub:https://github.com/hengboy
33+ */
34+ @ SpringBootApplication
35+ public class ApiBootRateLimiterApplication {
36+
37+ public static void main (String [] args ) {
38+ SpringApplication .run (ApiBootRateLimiterApplication .class , args );
39+ }
40+ }
Original file line number Diff line number Diff line change 3636public class RateLimiterSampleController {
3737 /**
3838 * QPS默认为1
39+ * 每秒限制该接口访问10次
3940 *
4041 * @return
4142 */
You can’t perform that action at this time.
0 commit comments