From 8f7f11e14618ab03b6622cab06d6327f59c7aaab Mon Sep 17 00:00:00 2001 From: zlt2000 Date: Tue, 4 Aug 2020 08:50:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=88=86=E5=B8=83=E5=BC=8F?= =?UTF-8?q?=E9=94=81=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=8A=A8=E8=A7=A3=E9=94=81?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/config/LoginArgResolverConfig.java | 35 ------------- .../central/common/lock/DistributedLock.java | 38 +++++++++++--- .../java/com/central/common/lock/ZLock.java | 26 ++++++++++ .../common/service/impl/SuperServiceImpl.java | 10 ++-- .../redis/lock/RedissonDistributedLock.java | 49 ++++++------------- 5 files changed, 76 insertions(+), 82 deletions(-) delete mode 100644 zlt-commons/zlt-common-core/src/main/java/com/central/common/config/LoginArgResolverConfig.java create mode 100644 zlt-commons/zlt-common-core/src/main/java/com/central/common/lock/ZLock.java diff --git a/zlt-commons/zlt-common-core/src/main/java/com/central/common/config/LoginArgResolverConfig.java b/zlt-commons/zlt-common-core/src/main/java/com/central/common/config/LoginArgResolverConfig.java deleted file mode 100644 index bbffbf4f..00000000 --- a/zlt-commons/zlt-common-core/src/main/java/com/central/common/config/LoginArgResolverConfig.java +++ /dev/null @@ -1,35 +0,0 @@ -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 argumentResolvers) { - //注入用户信息 - argumentResolvers.add(new TokenArgumentResolver(userService)); - //注入应用信息 - argumentResolvers.add(new ClientArgumentResolver()); - } -} diff --git a/zlt-commons/zlt-common-core/src/main/java/com/central/common/lock/DistributedLock.java b/zlt-commons/zlt-common-core/src/main/java/com/central/common/lock/DistributedLock.java index a1f321a8..db86968e 100644 --- a/zlt-commons/zlt-common-core/src/main/java/com/central/common/lock/DistributedLock.java +++ b/zlt-commons/zlt-common-core/src/main/java/com/central/common/lock/DistributedLock.java @@ -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()); + } } diff --git a/zlt-commons/zlt-common-core/src/main/java/com/central/common/lock/ZLock.java b/zlt-commons/zlt-common-core/src/main/java/com/central/common/lock/ZLock.java new file mode 100644 index 00000000..ffe60769 --- /dev/null +++ b/zlt-commons/zlt-common-core/src/main/java/com/central/common/lock/ZLock.java @@ -0,0 +1,26 @@ +package com.central.common.lock; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * 锁对象抽象 + * + * @author zlt + * @date 2020/7/28 + *

+ * 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); + } +} diff --git a/zlt-commons/zlt-common-core/src/main/java/com/central/common/service/impl/SuperServiceImpl.java b/zlt-commons/zlt-common-core/src/main/java/com/central/common/service/impl/SuperServiceImpl.java index e0ee5623..30ef9508 100644 --- a/zlt-commons/zlt-common-core/src/main/java/com/central/common/service/impl/SuperServiceImpl.java +++ b/zlt-commons/zlt-common-core/src/main/java/com/central/common/service/impl/SuperServiceImpl.java @@ -12,6 +12,7 @@ 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 boolean saveIdempotency(T entity, DistributedLock locker, String lockKey, 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 boolean saveIdempotency(T entity, DistributedLock locker, String lockKey, } else { throw new LockException("锁等待超时"); } - } finally { - locker.unlock(lock); } } diff --git a/zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/lock/RedissonDistributedLock.java b/zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/lock/RedissonDistributedLock.java index d090b984..37d96d63 100644 --- a/zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/lock/RedissonDistributedLock.java +++ b/zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/lock/RedissonDistributedLock.java @@ -3,6 +3,7 @@ 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 @@ /** * 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 lock; + return zLock; } 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) {