Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
midjourney-proxy
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
徐少华
midjourney-proxy
Commits
1781160c
Commit
1781160c
authored
Apr 13, 2022
by
吴博
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: [1006976] 自定义限流切面
parent
45ad037f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
102 additions
and
0 deletions
+102
-0
RateLimit.java
...rc/main/java/com/pcloud/common/core/aspect/RateLimit.java
+46
-0
RateLimitAspect.java
...n/java/com/pcloud/common/core/aspect/RateLimitAspect.java
+56
-0
No files found.
pcloud-common-core/src/main/java/com/pcloud/common/core/aspect/RateLimit.java
0 → 100644
View file @
1781160c
package
com
.
pcloud
.
common
.
core
.
aspect
;
import
java.lang.annotation.Documented
;
import
java.lang.annotation.ElementType
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
import
java.lang.annotation.Target
;
import
java.util.concurrent.TimeUnit
;
/**
* @author 吴博
* @date 2022/04/13 20:11
**/
@Target
(
ElementType
.
METHOD
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
@Documented
public
@interface
RateLimit
{
/**
* @return
*/
String
value
()
default
""
;
/**
* 每秒向桶中放入令牌的数量 默认最大即不做限流
*
* @return
*/
double
perSecond
()
default
Double
.
MAX_VALUE
;
/**
* 获取令牌的等待时间 默认0
*
* @return
*/
int
timeOut
()
default
0
;
/**
* 超时时间单位
*
* @return
*/
TimeUnit
timeOutUnit
()
default
TimeUnit
.
MILLISECONDS
;
}
\ No newline at end of file
pcloud-common-core/src/main/java/com/pcloud/common/core/aspect/RateLimitAspect.java
0 → 100644
View file @
1781160c
package
com
.
pcloud
.
common
.
core
.
aspect
;
import
com.google.common.util.concurrent.RateLimiter
;
import
com.pcloud.common.exceptions.BizException
;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.Signature
;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.annotation.Pointcut
;
import
org.aspectj.lang.reflect.MethodSignature
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
java.lang.reflect.Method
;
/**
* @author 吴博
* @date 2022/04/13 20:11
**/
@Aspect
@Component
public
class
RateLimitAspect
{
private
final
static
Logger
logger
=
LoggerFactory
.
getLogger
(
RateLimitAspect
.
class
);
private
RateLimiter
rateLimiter
=
RateLimiter
.
create
(
Double
.
MAX_VALUE
);
/**
* 定义切点
* 1、通过扫包切入
* 2、带有指定注解切入
*/
@Pointcut
(
"@annotation(com.pcloud.common.core.aspect.RateLimit)"
)
public
void
checkPointcut
()
{
}
@ResponseBody
@Around
(
value
=
"checkPointcut()"
)
public
Object
aroundNotice
(
ProceedingJoinPoint
pjp
)
throws
Throwable
{
logger
.
info
(
"拦截到了{}方法..."
,
pjp
.
getSignature
().
getName
());
Signature
signature
=
pjp
.
getSignature
();
MethodSignature
methodSignature
=
(
MethodSignature
)
signature
;
//获取目标方法
Method
targetMethod
=
methodSignature
.
getMethod
();
if
(
targetMethod
.
isAnnotationPresent
(
RateLimit
.
class
))
{
//获取目标方法的@RateLimit注解
RateLimit
rateLimit
=
targetMethod
.
getAnnotation
(
RateLimit
.
class
);
rateLimiter
.
setRate
(
rateLimit
.
perSecond
());
if
(!
rateLimiter
.
tryAcquire
(
rateLimit
.
timeOut
(),
rateLimit
.
timeOutUnit
()))
throw
new
BizException
(
BizException
.
PARAM_IS_NULL
.
getCode
(),
"请求过于频繁,请您稍后重试!"
);
}
return
pjp
.
proceed
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment