优化分布式锁增加自动解锁功能
This commit is contained in:
-35
@@ -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<HandlerMethodArgumentResolver> argumentResolvers) {
|
||||
//注入用户信息
|
||||
argumentResolvers.add(new TokenArgumentResolver(userService));
|
||||
//注入应用信息
|
||||
argumentResolvers.add(new ClientArgumentResolver());
|
||||
}
|
||||
}
|
||||
+30
-8
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
+4
-6
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user