Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
spring-boot-starter-dsp
Project
Project
Details
Activity
Releases
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
shenjiaqing
spring-boot-starter-dsp
Commits
a0fe2e6f
Commit
a0fe2e6f
authored
Apr 25, 2022
by
shenjiaqing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
提交代码
parent
9a24774e
Changes
11
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
218 additions
and
2 deletions
+218
-2
build.gradle
build.gradle
+5
-1
settings.gradle
settings.gradle
+1
-0
build.gradle
spring-boot-starter-dsp-rateLimiter/build.gradle
+20
-0
Commons.java
...om/duiba/spring/boot/starter/dsp/rateLimiter/Commons.java
+21
-0
DubboRpcRateLimitFilter.java
...boot/starter/dsp/rateLimiter/DubboRpcRateLimitFilter.java
+18
-0
LimitAspect.java
...uiba/spring/boot/starter/dsp/rateLimiter/LimitAspect.java
+108
-0
RateLimit.java
.../duiba/spring/boot/starter/dsp/rateLimiter/RateLimit.java
+18
-0
RateLimitProperties.java
...ing/boot/starter/dsp/rateLimiter/RateLimitProperties.java
+16
-0
org.apache.dubbo.rpc.Filter
...main/resources/META-INF.dubbo/org.apache.dubbo.rpc.Filter
+1
-0
limit.lua
...boot-starter-dsp-rateLimiter/src/main/resources/limit.lua
+10
-0
SamplerLogAutoConfiguration.java
...boot/starter/dsp/sampler/SamplerLogAutoConfiguration.java
+0
-1
No files found.
build.gradle
View file @
a0fe2e6f
...
...
@@ -38,7 +38,7 @@ allprojects {
}
group
=
"cn.com.duiba.boot"
version
=
"0.0.
41-jyy
-SNAPSHOT"
version
=
"0.0.
60-sjq
-SNAPSHOT"
}
subprojects
{
...
...
@@ -87,6 +87,10 @@ subprojects {
//log4j fix
dependency
(
'org.apache.logging.log4j:log4j-api:2.16.0'
)
dependency
(
'org.apache.logging.log4j:log4j-core:2.16.0'
)
dependency
(
'cn.hutool:hutool-all:4.1.19'
)
dependency
'org.aspectj:aspectjtools:1.9.5'
dependency
'org.aspectj:aspectjweaver:1.9.5'
}
}
...
...
settings.gradle
View file @
a0fe2e6f
...
...
@@ -3,4 +3,5 @@ include 'spring-boot-starter-dsp-model'
include
'spring-boot-starter-dsp-sampler'
include
'spring-boot-starter-dsp-warmup'
include
'spring-boot-starter-dsp-util'
include
'spring-boot-starter-dsp-rateLimiter'
spring-boot-starter-dsp-rateLimiter/build.gradle
0 → 100644
View file @
a0fe2e6f
dependencies
{
testCompile
group:
'junit'
,
name:
'junit'
,
version:
'4.12'
compile
"org.springframework.boot:spring-boot-starter-web"
compile
'org.springframework.boot:spring-boot-autoconfigure'
compile
"org.apache.dubbo:dubbo-spring-boot-starter"
compile
'org.aspectj:aspectjtools'
compile
'org.aspectj:aspectjweaver'
compile
"cn.com.duiba.boot:spring-boot-starter-redis"
compile
(
"redis.clients:jedis"
)
compileOnly
'org.projectlombok:lombok'
annotationProcessor
'org.projectlombok:lombok'
testCompileOnly
'org.projectlombok:lombok'
testAnnotationProcessor
'org.projectlombok:lombok'
}
spring-boot-starter-dsp-rateLimiter/src/main/java/cn/com/duiba/spring/boot/starter/dsp/rateLimiter/Commons.java
0 → 100644
View file @
a0fe2e6f
package
cn
.
com
.
duiba
.
spring
.
boot
.
starter
.
dsp
.
rateLimiter
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.data.redis.core.script.DefaultRedisScript
;
import
org.springframework.scripting.support.ResourceScriptSource
;
@Configuration
public
class
Commons
{
@Bean
public
DefaultRedisScript
<
Long
>
redisLuaScript
()
{
DefaultRedisScript
<
Long
>
redisScript
=
new
DefaultRedisScript
<>();
//读取 lua 脚本
redisScript
.
setScriptSource
(
new
ResourceScriptSource
(
new
ClassPathResource
(
"limit.lua"
)));
redisScript
.
setResultType
(
Long
.
class
);
return
redisScript
;
}
}
spring-boot-starter-dsp-rateLimiter/src/main/java/cn/com/duiba/spring/boot/starter/dsp/rateLimiter/DubboRpcRateLimitFilter.java
0 → 100644
View file @
a0fe2e6f
package
cn
.
com
.
duiba
.
spring
.
boot
.
starter
.
dsp
.
rateLimiter
;
import
org.apache.dubbo.common.constants.CommonConstants
;
import
org.apache.dubbo.common.extension.Activate
;
import
org.apache.dubbo.rpc.*
;
@Activate
(
group
=
{
CommonConstants
.
CONSUMER
})
public
class
DubboRpcRateLimitFilter
implements
Filter
{
@Override
public
Result
invoke
(
Invoker
<?>
invoker
,
Invocation
invocation
)
throws
RpcException
{
return
invoker
.
invoke
(
invocation
);
}
}
spring-boot-starter-dsp-rateLimiter/src/main/java/cn/com/duiba/spring/boot/starter/dsp/rateLimiter/LimitAspect.java
0 → 100644
View file @
a0fe2e6f
package
cn
.
com
.
duiba
.
spring
.
boot
.
starter
.
dsp
.
rateLimiter
;
import
org.aspectj.lang.ProceedingJoinPoint
;
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.beans.factory.annotation.Autowired
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.data.redis.core.script.DefaultRedisScript
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
javax.annotation.Resource
;
import
javax.servlet.http.HttpServletRequest
;
import
java.lang.reflect.Method
;
import
java.util.Collections
;
import
java.util.List
;
@Aspect
@Component
public
class
LimitAspect
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
LimitAspect
.
class
);
@Resource
(
name
=
"redis03StringRedisTemplate"
)
private
StringRedisTemplate
stringRedisTemplate
;
@Autowired
private
DefaultRedisScript
<
Long
>
redisLuaScript
;
@Autowired
private
RateLimitProperties
rateLimitProperties
;
@Pointcut
(
value
=
"@annotation(cn.com.duiba.spring.boot.starter.dsp.rateLimiter.RateLimit)"
)
public
void
rateLimitPointcut
()
{
// 点击注解切入.
}
@Around
(
value
=
"rateLimitPointcut()"
)
public
Object
interceptor
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
if
(!
rateLimitProperties
.
isAdxRateLimitSwitch
())
{
return
joinPoint
.
proceed
();
}
MethodSignature
signature
=
(
MethodSignature
)
joinPoint
.
getSignature
();
Method
method
=
signature
.
getMethod
();
Class
<?>
targetClass
=
method
.
getDeclaringClass
();
RateLimit
rateLimit
=
method
.
getAnnotation
(
RateLimit
.
class
);
if
(
rateLimit
==
null
)
{
return
joinPoint
.
proceed
();
}
HttpServletRequest
request
=
((
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
()).
getRequest
();
String
ipAddress
=
getIpAddress
(
request
);
StringBuffer
stringBuffer
=
new
StringBuffer
();
stringBuffer
.
append
(
ipAddress
).
append
(
"-"
)
.
append
(
targetClass
.
getName
()).
append
(
"-"
)
.
append
(
method
.
getName
()).
append
(
"-"
)
.
append
(
rateLimit
.
key
());
logger
.
info
(
"限流啦, redis key{}"
,
stringBuffer
.
toString
());
List
<
String
>
keys
=
Collections
.
singletonList
(
stringBuffer
.
toString
());
Long
number
=
stringRedisTemplate
.
execute
(
redisLuaScript
,
keys
,
String
.
valueOf
(
rateLimit
.
count
()),
String
.
valueOf
(
rateLimit
.
time
()));
if
(
number
!=
null
&&
number
!=
0
&&
number
<=
rateLimit
.
count
())
{
logger
.
info
(
"限流时间段内访问第:{} 次"
,
number
);
return
joinPoint
.
proceed
();
}
//由于本文没有配置公共异常类,如果配置可替换
throw
new
RuntimeException
(
"已经到设置限流次数"
);
}
private
static
String
getIpAddress
(
HttpServletRequest
request
)
{
String
ipAddress
;
try
{
ipAddress
=
request
.
getHeader
(
"x-forwarded-for"
);
if
(
ipAddress
==
null
||
ipAddress
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ipAddress
))
{
ipAddress
=
request
.
getHeader
(
"Proxy-Client-IP"
);
}
if
(
ipAddress
==
null
||
ipAddress
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ipAddress
))
{
ipAddress
=
request
.
getHeader
(
"WL-Proxy-Client-IP"
);
}
if
(
ipAddress
==
null
||
ipAddress
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ipAddress
))
{
ipAddress
=
request
.
getRemoteAddr
();
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if
(
ipAddress
!=
null
&&
ipAddress
.
length
()
>
15
)
{
// "***.***.***.***".length()
// = 15
if
(
ipAddress
.
indexOf
(
","
)
>
0
)
{
ipAddress
=
ipAddress
.
substring
(
0
,
ipAddress
.
indexOf
(
","
));
}
}
}
catch
(
Exception
e
)
{
ipAddress
=
""
;
}
return
ipAddress
;
}
}
spring-boot-starter-dsp-rateLimiter/src/main/java/cn/com/duiba/spring/boot/starter/dsp/rateLimiter/RateLimit.java
0 → 100644
View file @
a0fe2e6f
package
cn
.
com
.
duiba
.
spring
.
boot
.
starter
.
dsp
.
rateLimiter
;
import
java.lang.annotation.ElementType
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
import
java.lang.annotation.Target
;
@Target
({
ElementType
.
TYPE
,
ElementType
.
METHOD
})
@Retention
(
RetentionPolicy
.
RUNTIME
)
public
@interface
RateLimit
{
String
key
()
default
"limit"
;
int
time
()
default
5
;
int
count
()
default
5
;
}
spring-boot-starter-dsp-rateLimiter/src/main/java/cn/com/duiba/spring/boot/starter/dsp/rateLimiter/RateLimitProperties.java
0 → 100644
View file @
a0fe2e6f
package
cn
.
com
.
duiba
.
spring
.
boot
.
starter
.
dsp
.
rateLimiter
;
import
lombok.Getter
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.cloud.context.config.annotation.RefreshScope
;
import
org.springframework.stereotype.Component
;
@Component
@RefreshScope
public
class
RateLimitProperties
{
@Getter
@Value
(
"${adx.rate.limit.switch:false}"
)
private
boolean
adxRateLimitSwitch
;
}
spring-boot-starter-dsp-rateLimiter/src/main/resources/META-INF.dubbo/org.apache.dubbo.rpc.Filter
0 → 100644
View file @
a0fe2e6f
dubboRpcRateLimitFilter=cn.com.duiba.spring.boot.starter.dsp.rateLimiter.DubboRpcRateLimitFilter
\ No newline at end of file
spring-boot-starter-dsp-rateLimiter/src/main/resources/limit.lua
0 → 100644
View file @
a0fe2e6f
local
key
=
"rate.limit:"
..
KEYS
[
1
]
--限流KEY
local
limit
=
tonumber
(
ARGV
[
1
])
--限流大小
local
current
=
tonumber
(
redis
.
call
(
'get'
,
key
)
or
"0"
)
if
current
+
1
>
limit
then
--如果超出限流大小
return
0
else
--请求数+1,并设置2秒过期
redis
.
call
(
"INCRBY"
,
key
,
"1"
)
redis
.
call
(
"expire"
,
key
,
"2"
)
return
current
+
1
end
spring-boot-starter-dsp-sampler/src/main/java/cn/com/duiba/spring/boot/starter/dsp/sampler/SamplerLogAutoConfiguration.java
View file @
a0fe2e6f
...
...
@@ -3,7 +3,6 @@ package cn.com.duiba.spring.boot.starter.dsp.sampler;
import
cn.com.duiba.spring.boot.starter.dsp.sampler.converter.ApolloPanGuConverter
;
import
cn.com.duiba.spring.boot.starter.dsp.sampler.converter.SamplerLogConverter
;
import
cn.com.duiba.spring.boot.starter.dsp.sampler.feign.SamplerLogRequestInterceptor
;
import
cn.com.duiba.spring.boot.starter.dsp.sampler.filter.DubboLogSamplerContextFilter
;
import
cn.com.duiba.spring.boot.starter.dsp.sampler.filter.RpcLogSamplerContextFilter
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.context.annotation.Bean
;
...
...
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