Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
程序猿小郑
microservices-platform
提交
8f7f11e1
microservices-platform
项目概览
程序猿小郑
/
microservices-platform
与 Fork 源项目一致
Fork自
zlt2000 / microservices-platform
通知
4
Star
1
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,发现更多精彩内容 >>
提交
8f7f11e1
编写于
8月 04, 2020
作者:
zlt2000
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
优化分布式锁增加自动解锁功能
上级
0deb6ca2
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
76 addition
and
82 deletion
+76
-82
zlt-commons/zlt-common-core/src/main/java/com/central/common/config/LoginArgResolverConfig.java
...ava/com/central/common/config/LoginArgResolverConfig.java
+0
-35
zlt-commons/zlt-common-core/src/main/java/com/central/common/lock/DistributedLock.java
...rc/main/java/com/central/common/lock/DistributedLock.java
+30
-8
zlt-commons/zlt-common-core/src/main/java/com/central/common/lock/ZLock.java
...mon-core/src/main/java/com/central/common/lock/ZLock.java
+26
-0
zlt-commons/zlt-common-core/src/main/java/com/central/common/service/impl/SuperServiceImpl.java
...ava/com/central/common/service/impl/SuperServiceImpl.java
+4
-6
zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/lock/RedissonDistributedLock.java
...om/central/common/redis/lock/RedissonDistributedLock.java
+16
-33
未找到文件。
zlt-commons/zlt-common-core/src/main/java/com/central/common/config/LoginArgResolverConfig.java
已删除
100644 → 0
浏览文件 @
0deb6ca2
package
com.central.common.config
;
import
com.central.common.feign.UserService
;
import
com.central.common.resolver.ClientArgumentResolver
;
import
com.central.common.resolver.TokenArgumentResolver
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Lazy
;
import
org.springframework.web.method.support.HandlerMethodArgumentResolver
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
import
java.util.List
;
/**
* 公共配置类, 一些公共工具配置
*
* @author zlt
* @date 2018/8/25
*/
public
class
LoginArgResolverConfig
implements
WebMvcConfigurer
{
@Lazy
@Autowired
private
UserService
userService
;
/**
* Token参数解析
*
* @param argumentResolvers 解析类
*/
@Override
public
void
addArgumentResolvers
(
List
<
HandlerMethodArgumentResolver
>
argumentResolvers
)
{
//注入用户信息
argumentResolvers
.
add
(
new
TokenArgumentResolver
(
userService
));
//注入应用信息
argumentResolvers
.
add
(
new
ClientArgumentResolver
());
}
}
zlt-commons/zlt-common-core/src/main/java/com/central/common/lock/DistributedLock.java
浏览文件 @
8f7f11e1
...
...
@@ -21,10 +21,17 @@ public interface DistributedLock {
* @param isFair 是否公平锁
* @return 锁对象
*/
Object
lock
(
String
key
,
long
leaseTime
,
TimeUnit
unit
,
boolean
isFair
)
throws
Exception
;
Object
lock
(
String
key
,
long
leaseTime
,
TimeUnit
unit
)
throws
Exception
;
Object
lock
(
String
key
,
boolean
isFair
)
throws
Exception
;
Object
lock
(
String
key
)
throws
Exception
;
ZLock
lock
(
String
key
,
long
leaseTime
,
TimeUnit
unit
,
boolean
isFair
)
throws
Exception
;
default
ZLock
lock
(
String
key
,
long
leaseTime
,
TimeUnit
unit
)
throws
Exception
{
return
this
.
lock
(
key
,
leaseTime
,
unit
,
false
);
}
default
ZLock
lock
(
String
key
,
boolean
isFair
)
throws
Exception
{
return
this
.
lock
(
key
,
-
1
,
null
,
isFair
);
}
default
ZLock
lock
(
String
key
)
throws
Exception
{
return
this
.
lock
(
key
,
-
1
,
null
,
false
);
}
/**
* 尝试获取锁,如果锁不可用则等待最多waitTime时间后放弃
...
...
@@ -35,14 +42,29 @@ public interface DistributedLock {
* @param unit {@code waitTime} 和 {@code leaseTime} 参数的时间单位
* @return 锁对象,如果获取锁失败则为null
*/
Object
tryLock
(
String
key
,
long
waitTime
,
long
leaseTime
,
TimeUnit
unit
,
boolean
isFair
)
throws
Exception
;
Object
tryLock
(
String
key
,
long
waitTime
,
long
leaseTime
,
TimeUnit
unit
)
throws
Exception
;
Object
tryLock
(
String
key
,
long
waitTime
,
TimeUnit
unit
,
boolean
isFair
)
throws
Exception
;
Object
tryLock
(
String
key
,
long
waitTime
,
TimeUnit
unit
)
throws
Exception
;
ZLock
tryLock
(
String
key
,
long
waitTime
,
long
leaseTime
,
TimeUnit
unit
,
boolean
isFair
)
throws
Exception
;
default
ZLock
tryLock
(
String
key
,
long
waitTime
,
long
leaseTime
,
TimeUnit
unit
)
throws
Exception
{
return
this
.
tryLock
(
key
,
waitTime
,
leaseTime
,
unit
,
false
);
}
default
ZLock
tryLock
(
String
key
,
long
waitTime
,
TimeUnit
unit
,
boolean
isFair
)
throws
Exception
{
return
this
.
tryLock
(
key
,
waitTime
,
-
1
,
unit
,
isFair
);
}
default
ZLock
tryLock
(
String
key
,
long
waitTime
,
TimeUnit
unit
)
throws
Exception
{
return
this
.
tryLock
(
key
,
waitTime
,
-
1
,
unit
,
false
);
}
/**
* 释放锁
* @param lock 锁对象
*/
void
unlock
(
Object
lock
)
throws
Exception
;
/**
* 释放锁
* @param zLock 锁抽象对象
*/
default
void
unlock
(
ZLock
zLock
)
throws
Exception
{
this
.
unlock
(
zLock
.
getLock
());
}
}
zlt-commons/zlt-common-core/src/main/java/com/central/common/lock/ZLock.java
0 → 100644
浏览文件 @
8f7f11e1
package
com.central.common.lock
;
import
lombok.AllArgsConstructor
;
import
lombok.Getter
;
/**
* 锁对象抽象
*
* @author zlt
* @date 2020/7/28
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@AllArgsConstructor
public
class
ZLock
implements
AutoCloseable
{
@Getter
private
final
Object
lock
;
private
final
DistributedLock
locker
;
@Override
public
void
close
()
throws
Exception
{
locker
.
unlock
(
lock
);
}
}
zlt-commons/zlt-common-core/src/main/java/com/central/common/service/impl/SuperServiceImpl.java
浏览文件 @
8f7f11e1
...
...
@@ -12,6 +12,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import
com.central.common.exception.IdempotencyException
;
import
com.central.common.exception.LockException
;
import
com.central.common.lock.DistributedLock
;
import
com.central.common.lock.ZLock
;
import
com.central.common.service.ISuperService
;
import
java.io.Serializable
;
...
...
@@ -36,10 +37,9 @@ public class SuperServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,
if
(
StrUtil
.
isEmpty
(
lockKey
))
{
throw
new
LockException
(
"lockKey is null"
);
}
Object
lock
=
null
;
try
{
//加锁
lock
=
locker
.
tryLock
(
lockKey
,
10
,
60
,
TimeUnit
.
SECONDS
);
try
(
ZLock
lock
=
locker
.
tryLock
(
lockKey
,
10
,
60
,
TimeUnit
.
SECONDS
);
)
{
if
(
lock
!=
null
)
{
//判断记录是否已存在
int
count
=
super
.
count
(
countWrapper
);
...
...
@@ -54,8 +54,6 @@ public class SuperServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M,
}
else
{
throw
new
LockException
(
"锁等待超时"
);
}
}
finally
{
locker
.
unlock
(
lock
);
}
}
...
...
zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/lock/RedissonDistributedLock.java
浏览文件 @
8f7f11e1
...
...
@@ -3,6 +3,7 @@ package com.central.common.redis.lock;
import
com.central.common.constant.CommonConstant
;
import
com.central.common.exception.LockException
;
import
com.central.common.lock.DistributedLock
;
import
com.central.common.lock.ZLock
;
import
org.redisson.api.RLock
;
import
org.redisson.api.RedissonClient
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -14,6 +15,7 @@ import java.util.concurrent.TimeUnit;
/**
* redisson分布式锁实现,基本锁功能的抽象实现
* 本接口能满足绝大部分的需求,高级的锁功能,请自行扩展或直接使用原生api
* https://gitbook.cn/gitchat/activity/5f02746f34b17609e14c7d5a
*
* @author zlt
* @date 2020/5/5
...
...
@@ -27,52 +29,33 @@ public class RedissonDistributedLock implements DistributedLock {
@Autowired
private
RedissonClient
redisson
;
private
RLock
getLock
(
String
key
,
boolean
isFair
)
{
private
ZLock
getLock
(
String
key
,
boolean
isFair
)
{
RLock
lock
;
if
(
isFair
)
{
return
redisson
.
getFairLock
(
CommonConstant
.
LOCK_KEY_PREFIX
+
key
);
lock
=
redisson
.
getFairLock
(
CommonConstant
.
LOCK_KEY_PREFIX
+
key
);
}
else
{
lock
=
redisson
.
getLock
(
CommonConstant
.
LOCK_KEY_PREFIX
+
key
);
}
return
redisson
.
getLock
(
CommonConstant
.
LOCK_KEY_PREFIX
+
key
);
return
new
ZLock
(
lock
,
this
);
}
@Override
public
RLock
lock
(
String
key
,
long
leaseTime
,
TimeUnit
unit
,
boolean
isFair
)
{
RLock
lock
=
getLock
(
key
,
isFair
);
public
ZLock
lock
(
String
key
,
long
leaseTime
,
TimeUnit
unit
,
boolean
isFair
)
{
ZLock
zLock
=
getLock
(
key
,
isFair
);
RLock
lock
=
(
RLock
)
zLock
.
getLock
();
lock
.
lock
(
leaseTime
,
unit
);
return
lock
;
}
@Override
public
RLock
lock
(
String
key
,
long
leaseTime
,
TimeUnit
unit
)
{
return
lock
(
key
,
leaseTime
,
unit
,
false
);
}
@Override
public
RLock
lock
(
String
key
,
boolean
isFair
)
{
return
lock
(
key
,
-
1
,
null
,
isFair
);
}
@Override
public
RLock
lock
(
String
key
)
{
return
lock
(
key
,
-
1
,
null
,
false
);
return
zLock
;
}
@Override
public
RLock
tryLock
(
String
key
,
long
waitTime
,
long
leaseTime
,
TimeUnit
unit
,
boolean
isFair
)
throws
InterruptedException
{
RLock
lock
=
getLock
(
key
,
isFair
);
public
ZLock
tryLock
(
String
key
,
long
waitTime
,
long
leaseTime
,
TimeUnit
unit
,
boolean
isFair
)
throws
InterruptedException
{
ZLock
zLock
=
getLock
(
key
,
isFair
);
RLock
lock
=
(
RLock
)
zLock
.
getLock
();
if
(
lock
.
tryLock
(
waitTime
,
leaseTime
,
unit
))
{
return
l
ock
;
return
zL
ock
;
}
return
null
;
}
@Override
public
RLock
tryLock
(
String
key
,
long
waitTime
,
long
leaseTime
,
TimeUnit
unit
)
throws
InterruptedException
{
return
tryLock
(
key
,
waitTime
,
leaseTime
,
unit
,
false
);
}
@Override
public
RLock
tryLock
(
String
key
,
long
waitTime
,
TimeUnit
unit
,
boolean
isFair
)
throws
InterruptedException
{
return
tryLock
(
key
,
waitTime
,
-
1
,
unit
,
isFair
);
}
@Override
public
RLock
tryLock
(
String
key
,
long
waitTime
,
TimeUnit
unit
)
throws
InterruptedException
{
return
tryLock
(
key
,
waitTime
,
-
1
,
unit
,
false
);
}
@Override
public
void
unlock
(
Object
lock
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录