Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zhangjian1949
microservices-platform
提交
3d6cd0dd
microservices-platform
项目概览
zhangjian1949
/
microservices-platform
与 Fork 源项目一致
Fork自
zlt2000 / microservices-platform
通知
6
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
microservices-platform
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
3d6cd0dd
编写于
6月 06, 2020
作者:
zlt2000
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
分布式锁增加注解调用方式
上级
f32bd3c4
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
143 addition
and
1 deletion
+143
-1
zlt-commons/zlt-common-core/src/main/java/com/central/common/lock/Lock.java
...mmon-core/src/main/java/com/central/common/lock/Lock.java
+39
-0
zlt-commons/zlt-common-core/src/main/java/com/central/common/lock/LockAspect.java
...ore/src/main/java/com/central/common/lock/LockAspect.java
+100
-0
zlt-commons/zlt-common-core/src/main/resources/META-INF/spring.factories
...-common-core/src/main/resources/META-INF/spring.factories
+2
-1
zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/lock/RedissonDistributedLock.java
...om/central/common/redis/lock/RedissonDistributedLock.java
+2
-0
未找到文件。
zlt-commons/zlt-common-core/src/main/java/com/central/common/lock/Lock.java
0 → 100644
浏览文件 @
3d6cd0dd
package
com.central.common.lock
;
import
java.lang.annotation.*
;
import
java.util.concurrent.TimeUnit
;
/**
* @author zlt
* @date 2020/6/6
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@Target
({
ElementType
.
METHOD
,
ElementType
.
TYPE
})
@Retention
(
RetentionPolicy
.
RUNTIME
)
@Documented
public
@interface
Lock
{
/**
* 锁的key
*/
String
key
();
/**
* 获取锁的最大尝试时间(单位毫秒)
* 该值大于0则使用 locker.tryLock 方法加锁,否则使用 locker.lock 方法
*/
long
waitTime
()
default
0
;
/**
* 加锁的时间,超过这个时间后锁便自动解锁;
* 如果leaseTime为-1,则保持锁定直到显式解锁
*/
long
leaseTime
()
default
-
1
;
/**
* 参数的时间单位
*/
TimeUnit
unit
()
default
TimeUnit
.
SECONDS
;
/**
* 是否公平锁
*/
boolean
isFair
()
default
false
;
}
zlt-commons/zlt-common-core/src/main/java/com/central/common/lock/LockAspect.java
0 → 100644
浏览文件 @
3d6cd0dd
package
com.central.common.lock
;
import
cn.hutool.core.util.StrUtil
;
import
com.central.common.exception.LockException
;
import
lombok.extern.slf4j.Slf4j
;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.reflect.MethodSignature
;
import
org.springframework.core.DefaultParameterNameDiscoverer
;
import
org.springframework.expression.EvaluationContext
;
import
org.springframework.expression.Expression
;
import
org.springframework.expression.spel.standard.SpelExpressionParser
;
import
org.springframework.expression.spel.support.StandardEvaluationContext
;
/**
* 分布式锁切面
*
* @author zlt
* @date 2020/6/6
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@Slf4j
@Aspect
public
class
LockAspect
{
private
DistributedLock
locker
;
public
LockAspect
(
DistributedLock
locker
)
{
this
.
locker
=
locker
;
}
/**
* 用于SpEL表达式解析.
*/
private
SpelExpressionParser
spelExpressionParser
=
new
SpelExpressionParser
();
/**
* 用于获取方法参数定义名字.
*/
private
DefaultParameterNameDiscoverer
nameDiscoverer
=
new
DefaultParameterNameDiscoverer
();
@Around
(
"@within(lock) || @annotation(lock)"
)
public
Object
aroundLock
(
ProceedingJoinPoint
point
,
Lock
lock
)
throws
Throwable
{
Object
lockObj
=
null
;
if
(
lock
==
null
)
{
// 获取类上的注解
lock
=
point
.
getTarget
().
getClass
().
getDeclaredAnnotation
(
Lock
.
class
);
}
String
lockKey
=
lock
.
key
();
if
(
locker
==
null
)
{
throw
new
LockException
(
"DistributedLock is null"
);
}
if
(
StrUtil
.
isEmpty
(
lockKey
))
{
throw
new
LockException
(
"lockKey is null"
);
}
if
(
lockKey
.
contains
(
"#"
))
{
MethodSignature
methodSignature
=
(
MethodSignature
)
point
.
getSignature
();
//获取方法参数值
Object
[]
args
=
point
.
getArgs
();
lockKey
=
getValBySpEL
(
lockKey
,
methodSignature
,
args
);
}
try
{
//加锁
if
(
lock
.
waitTime
()
>
0
)
{
lockObj
=
locker
.
tryLock
(
lockKey
,
lock
.
waitTime
(),
lock
.
leaseTime
(),
lock
.
unit
(),
lock
.
isFair
());
}
else
{
lockObj
=
locker
.
lock
(
lockKey
,
lock
.
leaseTime
(),
lock
.
unit
(),
lock
.
isFair
());
}
if
(
lockObj
!=
null
)
{
return
point
.
proceed
();
}
else
{
throw
new
LockException
(
"锁等待超时"
);
}
}
finally
{
locker
.
unlock
(
lockObj
);
}
}
/**
* 解析spEL表达式
*/
private
String
getValBySpEL
(
String
spEL
,
MethodSignature
methodSignature
,
Object
[]
args
)
{
//获取方法形参名数组
String
[]
paramNames
=
nameDiscoverer
.
getParameterNames
(
methodSignature
.
getMethod
());
if
(
paramNames
!=
null
&&
paramNames
.
length
>
0
)
{
Expression
expression
=
spelExpressionParser
.
parseExpression
(
spEL
);
// spring的表达式上下文对象
EvaluationContext
context
=
new
StandardEvaluationContext
();
// 给上下文赋值
for
(
int
i
=
0
;
i
<
args
.
length
;
i
++)
{
context
.
setVariable
(
paramNames
[
i
],
args
[
i
]);
}
return
expression
.
getValue
(
context
).
toString
();
}
return
null
;
}
}
zlt-commons/zlt-common-core/src/main/resources/META-INF/spring.factories
浏览文件 @
3d6cd0dd
...
...
@@ -2,4 +2,5 @@ org.springframework.context.ApplicationContextInitializer=\
com.central.common.config.BannerInitializer
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.central.common.feign.fallback.UserServiceFallbackFactory
\ No newline at end of file
com.central.common.feign.fallback.UserServiceFallbackFactory,\
com.central.common.lock.LockAspect
\ No newline at end of file
zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/lock/RedissonDistributedLock.java
浏览文件 @
3d6cd0dd
...
...
@@ -7,6 +7,7 @@ import org.redisson.api.RLock;
import
org.redisson.api.RedissonClient
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
java.util.concurrent.TimeUnit
;
...
...
@@ -21,6 +22,7 @@ import java.util.concurrent.TimeUnit;
* Github: https://github.com/zlt2000
*/
@ConditionalOnClass
(
RedissonClient
.
class
)
@ConditionalOnProperty
(
prefix
=
"zlt.lock"
,
name
=
"lockerType"
,
havingValue
=
"REDIS"
,
matchIfMissing
=
true
)
public
class
RedissonDistributedLock
implements
DistributedLock
{
@Autowired
private
RedissonClient
redisson
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录