

rate-limit 一款 java 开源渐进式分布式限流框架使用介绍 - 老马啸西风
source link: https://www.cnblogs.com/houbbBlogs/p/16979488.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

rate-limit 一款 java 开源渐进式分布式限流框架使用介绍
posts - 120,comments - 23,views -
58522
rate-limit 是一个为 java 设计的渐进式限流工具。
目的是为了深入学习和使用限流,后续将会持续迭代。
-
渐进式实现
-
支持独立于 spring 使用
-
支持整合 spring
-
支持整合 spring-boot
-
内置多种限流策略

-
jdk 1.7
-
maven 3.x+
maven 导入
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>rate-limit-core</artifactId>
<version>1.1.0</version>
</dependency>
@RateLimit
限流注解放在方法上,指定对应的限制频率。
也可以定义在类上,默认下面的所有方法生效。方法上的优先级高于类。
属性 | 说明 | 默认值 |
---|---|---|
value | 方法访问一次消耗的令牌数 | 1 |
timeUnit | 时间单位 | TimeUnit.SECONDS |
interval | 时间间隔 | 60 |
count | 可调用次数 | 1000 |
enable | 是否启用 | true |
默认为 60S 内,可以调用 1000 次。
public class UserService {
@RateLimit(interval = 2, count = 5)
public void limitCount() {
log.info("{}", Thread.currentThread().getName());
}
}
这个例子中我们 2S 内最多调用 5 次。
RateLimitProxy.getProxy(xxx)
通过字节码获取方法对应的方法代理。
@Test(expected = RateLimitRuntimeException.class)
public void limitCountErrorTest() {
UserService userService = RateLimitProxy.getProxy(new UserService());
for(int i = 0; i < 3; i++) {
userService.limitCount();
}
}
当调用超出限制时,默认抛出 RateLimitRuntimeException
异常。
这里默认使用的是令牌桶算法,所以会出现异常。
重复注解 @RateLimits
有时候我们希望同时做多个的限制:
(1)一分钟不超过 10 次
(2)一小时不超过 30 次
为了支持多个配置,我们引入了新的注解 @RateLimits
,可以指定一个 @RateLimit
数组。
方法上同时使用 @RateLimits
+ @RateLimit
是可以同时生效的,不过为了简单,一般不建议混合使用。
@RateLimits({@RateLimit(interval = 2, count = 5)})
public void limitCount() {
//...
}
指定引导类
RateLimitProxy.getProxy(new UserService());
RateLimitProxy.getProxy(new UserService(), RateLimitBs.newInstance());
下面我们来一起看一下 RateLimitBs 引导类。
RateLimitBs
作为引导类,便于用户自定义配置。
方法 | 说明 | 默认值 |
---|---|---|
rateLimit | 限流策略 | RateLimits.tokenBucket() 令牌桶算法 |
timer | 时间策略 | Timers.system() 系统时间 |
cacheService | 缓存策略 | CommonCacheServiceMap 基于本地 map 的缓存策略 |
cacheKeyNamespace | 缓存KEY命名空间 | RATE-LIMIT 避免不同的应用,命名冲突。 |
configService | 限制配置策略 | RateLimitConfigService 默认基于方法上的注解 |
tokenService | 身份标识策略 | RateLimitTokenService 默认基于 IP |
methodService | 方法标识策略 | RateLimitMethodService 默认基于方法名+参数类型 |
rejectListener | 拒绝策略 | RateLimitRejectListenerException 限流时抛出异常 |
其中 rateLimit 内置 RateLimits
工具中的策略如下:
方法 | 说明 |
---|---|
fixedWindow() | 固定窗口 |
slideWindow(int windowNum) | 滑动窗口,可指定窗口大小 |
slideWindow() | 滑动窗口,默认为 10 |
slideWindowQueue() | 滑动窗口,基于队列的实现 |
leakyBucket() | 漏桶算法 |
tokenBucket() | 令牌桶算法 |
-
分布式系统,cacheService 建议使用基于 redis 的集中式缓存策略。
-
configService 如果想更加灵活,可以基于数据库的配置查询
RateLimitBs 引导类
RateLimitBs 默认配置如下:
RateLimitBs.newInstance()
.timer(Timers.system())
.methodService(new RateLimitMethodService())
.tokenService(new RateLimitTokenService())
.rejectListener(new RateLimitRejectListenerException())
.configService(new RateLimitConfigService())
.cacheService(new CommonCacheServiceMap())
.rateLimit(RateLimits.tokenBucket())
.cacheKeyNamespace(RateLimitConst.DEFAULT_CACHE_KEY_NAMESPACE);
spring 整合
maven 引入
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>rate-limit-spring</artifactId>
<version>1.1.0</version>
</dependency>
和上面使用类似,直接在方法上声明 @RateLimit
注解即可。
@Service
public class UserService {
private static final Log log = LogFactory.getLog(UserService.class);
@RateLimit(interval = 2, count = 5)
public void limitCount() {
log.info("{}", Thread.currentThread().getName());
}
}
通过 @EnableRateLimit
声明启用限流。
@Configuration
@ComponentScan("com.github.houbb.rate.limit.test.core")
@EnableRateLimit
public class SpringConfig {
}
@EnableRateLimit
的属性配置和 RateLimitBs 属性是以一一对应的。
方法 | 说明 | 默认值 |
---|---|---|
rateLimit | 限流策略 | 令牌桶算法 |
timer | 时间策略 | 系统时间 |
cacheService | 缓存策略 | 基于本地 map 的缓存策略 |
cacheKeyNamespace | 缓存KEY命名空间 | RATE-LIMIT 避免不同的应用,命名冲突。 |
configService | 限制配置策略 | 默认基于方法上的注解 |
tokenService | 身份标识策略 | 默认基于 IP |
methodService | 方法标识策略 | 默认基于方法名+参数类型 |
rejectListener | 拒绝策略 | 限流时抛出异常 |
这里的属性值,都是对应的 spring bean 名称,支持用户自定义。
spring-boot 整合
maven 引入
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>rate-limit-springboot-starter</artifactId>
<version>1.1.0</version>
</dependency>
其他和 spring 保持一致。
缓存相关工具
Recommend
-
30
什么是 Malagu Malagu ...
-
7
Serlina: 渐进式的 React 服务器渲染框架 2018-08-13 副标题: 《可能是最适合 Egg 的 React Serverside-rendering 方案》 上一周周末我花了些时间来完成了一个 R...
-
8
Hyperf 2.2 版发布!| 企业级的渐进式 PHP 协程框架首先感谢所有 Hyperf 的支持者,从发布至今两年的时间里,我们坚持每周发布一个小版本,截止至今已经发布了超过 106 个版本,这是 Hyper...
-
12
使用渐进式JPEG来提升用户体验 浏览:7301次 出处信息 今天才认识到原来JPEG文件有两种保存方式他们分别是...
-
8
使用托管服务网格实现应用在多集群中的 GitOps 全自动化渐进式发布 · Service Mesh|服务网格中文社区 2020年3月19日...
-
11
Axie 更新治理框架:社区财政与渐进式分权 陀螺财经 原创 2022-03-26 02:15 热度 917943 分享 微信扫一扫:分享 ...
-
13
V2EX › 开源软件 [项目推广] 开箱即用的渐进式前端框架 dagger.js
-
11
V2EX › 前端开发 开箱即用的渐进式前端开发框架 dagger.js
-
9
resubmit resubmit 是一款为 java 设计的渐进式防止重复提交框架。 推荐阅读:
-
6
2023-01-12 14:13 a16z 发文介绍渐进式权力下放框架,先将产品分割为 MDU 逐步进行调整 DeFi 之道讯,1 月 12 日,a16z Crypto 发文介绍渐进式权力下放框架,称去中心化是将控制和决策从一个集中的实体转移到去中心...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK