Skip to content

Commit f4ffe3f

Browse files
committed
增加针对Session限速功能
1 parent 64e72c1 commit f4ffe3f

File tree

8 files changed

+72
-6
lines changed

8 files changed

+72
-6
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<dependency>
1717
<groupId>com.github.chqiuu</groupId>
1818
<artifactId>spring-redis-current-limit</artifactId>
19-
<version>1.0.0</version>
19+
<version>Latest Version</version>
2020
</dependency>
2121
```
2222
### 2. 注册spring-redis-current-limit
@@ -215,6 +215,11 @@ public class LimiterController {
215215
1. [个人博客](https://blog.csdn.net/QIU176161650)
216216
2. [GitHub](https://github.com/chqiuu)
217217

218+
#### 参考
219+
220+
[将JAR包发布到Maven中央仓库](https://blog.csdn.net/liaomin416100569/article/details/86494819)
221+
[在Maven中央存储库中发布github项目](https://blog.csdn.net/weixin_26711867/article/details/108946638)
222+
218223
#### 版本信息
219224

220225
##### 1.0.0

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>com.github.chqiuu</groupId>
66
<artifactId>spring-redis-current-limit</artifactId>
77
<packaging>jar</packaging>
8-
<version>1.0.0</version>
8+
<version>1.0.3</version>
99
<name>spring-redis-current-limit</name>
1010
<description>基于注解的应用级网关限流框架</description>
1111
<url>https://github.com/chqiuu/spring-redis-current-limit</url>

src/main/java/com/github/chqiuu/redis/limit/annotation/CurrentLimit.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import com.github.chqiuu.redis.limit.enums.LimitTypeEnum;
5+
import com.github.chqiuu.redis.limit.enums.TypeLimitModelEnum;
56

67
import java.lang.annotation.*;
78

@@ -55,4 +56,11 @@
5556
* 设置限流类型。默认值:LOCAL
5657
*/
5758
LimitTypeEnum limitType() default LimitTypeEnum.LOCAL;
59+
60+
/**
61+
* 设置在类上设置注解限流模式,当为ElementType.TYPE时有效
62+
*
63+
* @return 在类上设置注解限流模式
64+
*/
65+
TypeLimitModelEnum typeLimitModel() default TypeLimitModelEnum.EACH;
5866
}

src/main/java/com/github/chqiuu/redis/limit/aspect/ObjectCurrentLimitAspect.java renamed to src/main/java/com/github/chqiuu/redis/limit/aspect/TypeCurrentLimitAspect.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.chqiuu.redis.limit.aspect;
22

33
import com.github.chqiuu.redis.limit.annotation.CurrentLimit;
4+
import com.github.chqiuu.redis.limit.enums.TypeLimitModelEnum;
45
import com.github.chqiuu.redis.limit.exception.CurrentLimitException;
56
import com.github.chqiuu.redis.limit.strategy.BaseCurrentLimiter;
67
import lombok.AllArgsConstructor;
@@ -13,7 +14,7 @@
1314
import org.springframework.stereotype.Component;
1415

1516
/**
16-
* 限流切面实现(对象
17+
* 限流切面实现(
1718
* 对象切面优先于方法切面
1819
*
1920
* @author chqiu
@@ -23,7 +24,7 @@
2324
@Aspect
2425
@Component
2526
@AllArgsConstructor
26-
public class ObjectCurrentLimitAspect {
27+
public class TypeCurrentLimitAspect {
2728

2829
private final BaseCurrentLimiter currentLimiter;
2930

@@ -44,7 +45,11 @@ public void withinPointcut(CurrentLimit currentLimit) {
4445
*/
4546
@Before(value = "withinPointcut(currentLimit)", argNames = "joinPoint,currentLimit")
4647
public void doWithinBefore(JoinPoint joinPoint, CurrentLimit currentLimit) {
47-
boolean isAllowAccess = currentLimiter.check(joinPoint, true, currentLimit.key(), currentLimit.limitType(), currentLimit.limit(), currentLimit.interval(), currentLimit.step());
48+
boolean isMethod = true;
49+
if (TypeLimitModelEnum.TOTAL.equals(currentLimit.typeLimitModel())) {
50+
isMethod = false;
51+
}
52+
boolean isAllowAccess = currentLimiter.check(joinPoint, isMethod, currentLimit.key(), currentLimit.limitType(), currentLimit.limit(), currentLimit.interval(), currentLimit.step());
4853
if (!isAllowAccess) {
4954
throw new CurrentLimitException(currentLimit.message());
5055
}

src/main/java/com/github/chqiuu/redis/limit/config/EnableCurrentLimitConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*/
2828
@Slf4j
2929
@Configuration
30-
@ComponentScan(basePackages = "com.chqiuu.redis.limit")
30+
@ComponentScan(basePackages = "com.github.chqiuu.redis.limit")
3131
public class EnableCurrentLimitConfiguration {
3232

3333
/**

src/main/java/com/github/chqiuu/redis/limit/enums/LimitTypeEnum.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ public enum LimitTypeEnum {
2626
* 根据用户限流。此系统中所有限流方法(或类)单位时间内只允许此用户访问n次
2727
*/
2828
USER_GLOBAL,
29+
/**
30+
* 根据Session限流。此方法(或类)单位时间内只允许此 SessionId 访问n次
31+
*/
32+
SESSION,
33+
/**
34+
* 根据Session限流。此系统中所有限流方法(或类)单位时间内只允许此 SessionId 访问n次
35+
*/
36+
SESSION_GLOBAL,
2937
/**
3038
* 自定义限流方法,详细使用请参考使用文档
3139
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.github.chqiuu.redis.limit.enums;
2+
3+
/**
4+
* 在类上做限流的模式枚举类
5+
*
6+
* 在类上限流模式可分为两种:
7+
* 第一种是该类中所有方法都采用相同的限流方式。例如,需要该类中每个方法每30秒只允许调用10次;
8+
* 第二种是该类进行整体限流(此处类配置限流优先于方法上配置的限流)。例如,需要该类中所有方法每30秒只允许调用10次。
9+
*
10+
* @author chqiu
11+
*/
12+
public enum TypeLimitModelEnum {
13+
/**
14+
* 该类中所有方法都采用相同的限流方式。例如,需要该类中每个方法每30秒只允许调用10次
15+
*/
16+
EACH,
17+
/**
18+
* 该类进行整体限流。例如,需要该类中所有方法每30秒只允许调用10次
19+
*/
20+
TOTAL;
21+
}

src/main/java/com/github/chqiuu/redis/limit/strategy/BaseCurrentLimiter.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@ String getCurrentKey(JoinPoint joinPoint, LimitTypeEnum limitType, boolean isMet
4646
assert requestAttributes != null;
4747
HttpServletRequest request = requestAttributes.getRequest();
4848
switch (limitType) {
49+
case SESSION_GLOBAL:
50+
// 以SessionId作为key
51+
if (request.getSession() != null) {
52+
key.append(request.getSession().getId());
53+
} else {
54+
throw new CurrentLimitException("获取不到用户Session,request.getSession().getId()为空");
55+
}
56+
break;
57+
case SESSION:
58+
// 以SessionId加方法(或类)作为key
59+
if (request.getSession() != null) {
60+
key.append(request.getSession().getId());
61+
if (isMethod) {
62+
key.append(getMethodName((MethodSignature) joinPoint.getSignature()));
63+
}
64+
} else {
65+
throw new CurrentLimitException("获取不到用户Session,request.getSession().getId()为空");
66+
}
67+
break;
4968
case USER_GLOBAL:
5069
// 以用户信息作为key
5170
if (request.getUserPrincipal() != null) {

0 commit comments

Comments
 (0)