Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
platform
/
platform-cdp-server
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 3f4cf438
authored
Oct 12, 2023
by
ThomasAdv
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
获取oneId时的分布式锁超时时间灵活可控
1 parent
bbfc6c91
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
13 deletions
platform-cdp/src/main/java/cn/com/glsx/cdp/common/distributeLock/DistributeLockMethodInterceptor.java
platform-cdp/src/main/java/cn/com/glsx/cdp/config/InteceptorConfig.java
platform-cdp/src/main/java/cn/com/glsx/cdp/common/distributeLock/DistributeLockMethodInterceptor.java
View file @
3f4cf43
package
cn
.
com
.
glsx
.
cdp
.
common
.
distributeLock
;
package
cn
.
com
.
glsx
.
cdp
.
common
.
distributeLock
;
import
com.alibaba.nacos.api.config.annotation.NacosValue
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.aopalliance.intercept.MethodInterceptor
;
import
org.aopalliance.intercept.MethodInterceptor
;
import
org.aopalliance.intercept.MethodInvocation
;
import
org.aopalliance.intercept.MethodInvocation
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.reflect.FieldUtils
;
import
org.apache.commons.lang3.reflect.FieldUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.core.LocalVariableTableParameterNameDiscoverer
;
import
org.springframework.core.LocalVariableTableParameterNameDiscoverer
;
import
org.springframework.data.redis.connection.RedisConnectionFactory
;
import
org.springframework.expression.Expression
;
import
org.springframework.expression.Expression
;
import
org.springframework.expression.ExpressionParser
;
import
org.springframework.expression.ExpressionParser
;
import
org.springframework.expression.spel.standard.SpelExpressionParser
;
import
org.springframework.expression.spel.standard.SpelExpressionParser
;
import
org.springframework.expression.spel.support.StandardEvaluationContext
;
import
org.springframework.expression.spel.support.StandardEvaluationContext
;
import
org.springframework.integration.support.locks.LockRegistry
;
import
org.springframework.integration.redis.util.RedisLockRegistry
;
import
org.springframework.stereotype.Component
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Method
;
import
java.text.MessageFormat
;
import
java.text.MessageFormat
;
import
java.util.Optional
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.locks.Lock
;
import
java.util.concurrent.locks.Lock
;
@Slf4j
@Slf4j
@Component
public
class
DistributeLockMethodInterceptor
implements
MethodInterceptor
{
public
class
DistributeLockMethodInterceptor
implements
MethodInterceptor
{
private
LockRegistry
lockRegistry
;
@Autowired
private
RedisConnectionFactory
redisConnectionFactory
;
private
RedisLockRegistry
lockRegistry
;
@NacosValue
(
value
=
"${distributeLock.timeout.seconds:10}"
,
autoRefreshed
=
true
)
private
Integer
globalLockSecondsTime
;
private
Integer
globalLockSecondsTime
;
public
DistributeLockMethodInterceptor
(
LockRegistry
lockRegistry
,
Integer
globalLockSecondsTime
)
{
private
static
final
Integer
DEFAULT_LOCK_TIMEOUT_SECONDS
=
10
;
this
.
lockRegistry
=
lockRegistry
;
this
.
globalLockSecondsTime
=
globalLockSecondsTime
;
}
@Override
@Override
public
Object
invoke
(
MethodInvocation
invocation
)
throws
Throwable
{
public
Object
invoke
(
MethodInvocation
invocation
)
throws
Throwable
{
if
(
lockRegistry
==
null
)
{
lockRegistry
=
new
RedisLockRegistry
(
redisConnectionFactory
,
"lock"
);
}
String
className
=
invocation
.
getThis
().
getClass
().
getName
();
String
className
=
invocation
.
getThis
().
getClass
().
getName
();
String
methodName
=
invocation
.
getMethod
().
getName
();
String
methodName
=
invocation
.
getMethod
().
getName
();
GlsxDistributeLockable
axLockable
=
invocation
.
getThis
().
getClass
().
getMethod
(
invocation
.
getMethod
().
getName
(),
invocation
.
getMethod
().
getParameterTypes
()).
getAnnotation
(
GlsxDistributeLockable
.
class
);
GlsxDistributeLockable
axLockable
=
invocation
.
getThis
().
getClass
().
getMethod
(
invocation
.
getMethod
().
getName
(),
invocation
.
getMethod
().
getParameterTypes
()).
getAnnotation
(
GlsxDistributeLockable
.
class
);
...
@@ -38,8 +49,10 @@ public class DistributeLockMethodInterceptor implements MethodInterceptor {
...
@@ -38,8 +49,10 @@ public class DistributeLockMethodInterceptor implements MethodInterceptor {
}
else
{
}
else
{
int
time
=
axLockable
.
time
();
int
time
=
axLockable
.
time
();
TimeUnit
timeUnit
=
axLockable
.
timeUnit
();
TimeUnit
timeUnit
=
axLockable
.
timeUnit
();
if
(
time
<
0
&&
this
.
globalLockSecondsTime
!=
null
&&
this
.
globalLockSecondsTime
>
0
)
{
log
.
info
(
"globalLockSecondsTime:{}"
,
globalLockSecondsTime
);
time
=
this
.
globalLockSecondsTime
;
globalLockSecondsTime
=
Optional
.
ofNullable
(
globalLockSecondsTime
).
orElse
(
DEFAULT_LOCK_TIMEOUT_SECONDS
);
if
(
time
<
0
&&
globalLockSecondsTime
!=
null
&&
globalLockSecondsTime
>
0
)
{
time
=
globalLockSecondsTime
;
timeUnit
=
TimeUnit
.
SECONDS
;
timeUnit
=
TimeUnit
.
SECONDS
;
}
}
...
...
platform-cdp/src/main/java/cn/com/glsx/cdp/config/InteceptorConfig.java
View file @
3f4cf43
...
@@ -3,22 +3,23 @@ package cn.com.glsx.cdp.config;
...
@@ -3,22 +3,23 @@ package cn.com.glsx.cdp.config;
import
cn.com.glsx.cdp.common.distributeLock.DistributeLockMethodInterceptor
;
import
cn.com.glsx.cdp.common.distributeLock.DistributeLockMethodInterceptor
;
import
org.springframework.aop.aspectj.AspectJExpressionPointcut
;
import
org.springframework.aop.aspectj.AspectJExpressionPointcut
;
import
org.springframework.aop.support.DefaultPointcutAdvisor
;
import
org.springframework.aop.support.DefaultPointcutAdvisor
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.data.redis.connection.RedisConnectionFactory
;
import
org.springframework.integration.redis.util.RedisLockRegistry
;
@Configuration
@Configuration
public
class
InteceptorConfig
{
public
class
InteceptorConfig
{
@Autowired
private
DistributeLockMethodInterceptor
distributeLockMethodInterceptor
;
@Bean
@Bean
public
DefaultPointcutAdvisor
glsxDistributeLockAdvisor
(
RedisConnectionFactory
redisConnectionFactory
){
public
DefaultPointcutAdvisor
glsxDistributeLockAdvisor
(){
DefaultPointcutAdvisor
advisor
=
new
DefaultPointcutAdvisor
();
DefaultPointcutAdvisor
advisor
=
new
DefaultPointcutAdvisor
();
RedisLockRegistry
lockRegistry
=
new
RedisLockRegistry
(
redisConnectionFactory
,
"lock"
);
AspectJExpressionPointcut
pointcut
=
new
AspectJExpressionPointcut
();
AspectJExpressionPointcut
pointcut
=
new
AspectJExpressionPointcut
();
pointcut
.
setExpression
(
"@annotation(cn.com.glsx.cdp.common.distributeLock.GlsxDistributeLockable)"
);
pointcut
.
setExpression
(
"@annotation(cn.com.glsx.cdp.common.distributeLock.GlsxDistributeLockable)"
);
advisor
.
setPointcut
(
pointcut
);
advisor
.
setPointcut
(
pointcut
);
advisor
.
setAdvice
(
new
DistributeLockMethodInterceptor
(
lockRegistry
,
10
)
);
advisor
.
setAdvice
(
distributeLockMethodInterceptor
);
return
advisor
;
return
advisor
;
}
}
}
}
Write
Preview
Markdown
is supported
Attach a file
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 post a comment