Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mica
mica
提交
961f0c3e
mica
项目概览
mica
/
mica
通知
10
Star
2
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
mica
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
961f0c3e
编写于
8月 10, 2019
作者:
如梦技术
🐛
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
⚡
redis 限流组件毕业.
上级
c3f29084
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
57 addition
and
21 deletion
+57
-21
mica-plus-redis/README.md
mica-plus-redis/README.md
+37
-15
mica-plus-redis/src/main/java/net/dreamlu/mica/redis/ratelimiter/RateLimiterClient.java
...net/dreamlu/mica/redis/ratelimiter/RateLimiterClient.java
+1
-2
mica-plus-redis/src/main/java/net/dreamlu/mica/redis/ratelimiter/RateLimiterException.java
.../dreamlu/mica/redis/ratelimiter/RateLimiterException.java
+15
-2
mica-plus-redis/src/main/java/net/dreamlu/mica/redis/ratelimiter/RedisRateLimiterClient.java
...reamlu/mica/redis/ratelimiter/RedisRateLimiterClient.java
+3
-1
mica-plus-redis/src/main/resources/META-INF/scripts/mica_rate_limiter.lua
...src/main/resources/META-INF/scripts/mica_rate_limiter.lua
+1
-1
未找到文件。
mica-plus-redis/README.md
浏览文件 @
961f0c3e
# redis-plus-redis
redis cache 增强
-
redis cache 增强
-
分布式限流组件
## 功能
1.
支持 # 号分隔 cachename 和 超时 timeOut。
示例:
```
java
@Cacheable
(
value
=
"user#300"
,
key
=
"#id"
)
public
String
selectById
(
Serializable
id
)
{
log
.
info
(
"selectById"
);
return
"selectById:"
+
id
;
}
```
## 使用
## 依赖引用
### maven
```
xml
<dependency>
...
...
@@ -28,6 +17,18 @@ public String selectById(Serializable id) {
compile
(
"net.dreamlu:redis-plus-redis:${version}"
)
```
##1. redis cache 增强
1.
支持 # 号分隔 cachename 和 超时 timeOut。
示例:
```
java
@Cacheable
(
value
=
"user#300"
,
key
=
"#id"
)
public
String
selectById
(
Serializable
id
)
{
log
.
info
(
"selectById"
);
return
"selectById:"
+
id
;
}
```
### MicaRedisCache
MicaRedisCache 为简化 redis 使用的 bean。
```
java
...
...
@@ -40,7 +41,7 @@ public String findById(Serializable id) {
}
```
## 注意
##
#
注意
使用
`protostuff`
做的
`redis`
序列化和反序列化。
`protostuff`
默认是根据 Model
`属性顺序`
进行序列化,如果在
`中间插入`
或者
`删除字段`
。
...
...
@@ -54,3 +55,24 @@ public String findById(Serializable id) {
| @Tag(1) | Model
`属性顺序`
标注 |
| @Exclude | 排除字段,不进行序列化和反序列化 |
| @Morph | Set、List、Map 等集合添加 |
##2. 分布式限流
### 2.1 开启限流组件
```
yaml
mica
:
redis
:
rate-limiter
:
enable
:
true
```
### 2.2 使用注解
```
java
@RateLimiter
```
### 2.3 使用 Client
```
java
@Autowired
private
RateLimiterClient
rateLimiterClient
;
```
mica-plus-redis/src/main/java/net/dreamlu/mica/redis/ratelimiter/RateLimiterClient.java
浏览文件 @
961f0c3e
...
...
@@ -82,7 +82,6 @@ public interface RateLimiterClient {
throw
Exceptions
.
unchecked
(
e
);
}
}
long
seconds
=
timeUnit
.
toSeconds
(
ttl
);
throw
new
RateLimiterException
(
"您的访问次数已超限:"
+
key
+
",速率:"
+
max
+
"/"
+
seconds
+
"s"
);
throw
new
RateLimiterException
(
key
,
max
,
ttl
,
timeUnit
);
}
}
mica-plus-redis/src/main/java/net/dreamlu/mica/redis/ratelimiter/RateLimiterException.java
浏览文件 @
961f0c3e
...
...
@@ -16,14 +16,27 @@
package
net.dreamlu.mica.redis.ratelimiter
;
import
lombok.Getter
;
import
java.util.concurrent.TimeUnit
;
/**
* 限流异常
*
* @author L.cm
*/
@Getter
public
class
RateLimiterException
extends
RuntimeException
{
private
final
String
key
;
private
final
long
max
;
private
final
long
ttl
;
private
final
TimeUnit
timeUnit
;
public
RateLimiterException
(
String
message
)
{
super
(
message
);
public
RateLimiterException
(
String
key
,
long
max
,
long
ttl
,
TimeUnit
timeUnit
)
{
super
(
String
.
format
(
"您的访问次数已超限:%s,速率:%d/%ds"
,
key
,
max
,
timeUnit
.
toSeconds
(
ttl
)));
this
.
key
=
key
;
this
.
max
=
max
;
this
.
ttl
=
ttl
;
this
.
timeUnit
=
timeUnit
;
}
}
mica-plus-redis/src/main/java/net/dreamlu/mica/redis/ratelimiter/RedisRateLimiterClient.java
浏览文件 @
961f0c3e
...
...
@@ -62,8 +62,10 @@ public class RedisRateLimiterClient implements RateLimiterClient {
List
<
String
>
keys
=
Collections
.
singletonList
(
redisKeyBuilder
);
// 毫秒,考虑主从策略和脚本回放机制,这个time由客户端获取传入
long
now
=
System
.
currentTimeMillis
();
// 转为毫秒,pexpire
long
ttlMillis
=
timeUnit
.
toMillis
(
ttl
);
// 执行命令
List
<
Long
>
results
=
this
.
redisTemplate
.
execute
(
this
.
script
,
keys
,
max
+
""
,
ttl
+
""
,
now
+
""
);
List
<
Long
>
results
=
this
.
redisTemplate
.
execute
(
this
.
script
,
keys
,
max
+
""
,
ttl
Millis
+
""
,
now
+
""
);
// 结果为空返回失败
if
(
results
==
null
||
results
.
isEmpty
())
{
return
false
;
...
...
mica-plus-redis/src/main/resources/META-INF/scripts/mica_rate_limiter.lua
浏览文件 @
961f0c3e
...
...
@@ -23,6 +23,6 @@ else
-- 没有达到阈值 value + 1
redis
.
call
(
"zadd"
,
key
,
now
,
now
)
-- 秒为单位设置 key 的生存时间
redis
.
call
(
"expire"
,
key
,
ttl
)
redis
.
call
(
"
p
expire"
,
key
,
ttl
)
return
nextLimit
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录