🔨 优化锁逻辑.
This commit is contained in:
@@ -24,8 +24,8 @@ package org.dromara.visor.framework.encrypt.configuration;
|
||||
|
||||
import org.dromara.visor.common.config.ConfigStore;
|
||||
import org.dromara.visor.common.constant.AutoConfigureOrderConst;
|
||||
import org.dromara.visor.common.interfaces.AesEncryptor;
|
||||
import org.dromara.visor.common.interfaces.RsaDecryptor;
|
||||
import org.dromara.visor.common.cipher.AesEncryptor;
|
||||
import org.dromara.visor.common.cipher.RsaDecryptor;
|
||||
import org.dromara.visor.common.utils.AesEncryptUtils;
|
||||
import org.dromara.visor.common.utils.RsaParamDecryptUtils;
|
||||
import org.dromara.visor.framework.encrypt.configuration.config.AesEncryptConfig;
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
*/
|
||||
package org.dromara.visor.framework.encrypt.core;
|
||||
|
||||
import org.dromara.visor.common.interfaces.AesEncryptor;
|
||||
import org.dromara.visor.common.cipher.AesEncryptor;
|
||||
|
||||
/**
|
||||
* 数据加密器
|
||||
|
||||
@@ -26,7 +26,7 @@ import cn.orionsec.kit.lang.utils.crypto.RSA;
|
||||
import org.dromara.visor.common.config.ConfigRef;
|
||||
import org.dromara.visor.common.config.ConfigStore;
|
||||
import org.dromara.visor.common.constant.ConfigKeys;
|
||||
import org.dromara.visor.common.interfaces.RsaDecryptor;
|
||||
import org.dromara.visor.common.cipher.RsaDecryptor;
|
||||
|
||||
import java.security.interfaces.RSAPrivateKey;
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -24,7 +24,8 @@ package org.dromara.visor.framework.redis.configuration;
|
||||
|
||||
import com.github.fppt.jedismock.RedisServer;
|
||||
import org.dromara.visor.common.constant.AutoConfigureOrderConst;
|
||||
import org.dromara.visor.common.interfaces.Locker;
|
||||
import org.dromara.visor.common.lock.EmptyLocker;
|
||||
import org.dromara.visor.common.lock.Locker;
|
||||
import org.dromara.visor.common.utils.LockerUtils;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
|
||||
@@ -35,7 +36,6 @@ import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* MockRedis
|
||||
@@ -79,18 +79,7 @@ public class OrionMockRedisAutoConfiguration {
|
||||
*/
|
||||
@Bean
|
||||
public Locker redisLocker() {
|
||||
Locker locker = new Locker() {
|
||||
@Override
|
||||
public boolean tryLock(String key, Runnable run) {
|
||||
run.run();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T tryLock(String key, Supplier<T> call) {
|
||||
return call.get();
|
||||
}
|
||||
};
|
||||
EmptyLocker locker = new EmptyLocker();
|
||||
LockerUtils.setDelegate(locker);
|
||||
return locker;
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ package org.dromara.visor.framework.redis.configuration;
|
||||
|
||||
import cn.orionsec.kit.lang.define.cache.key.CacheKeyDefine;
|
||||
import org.dromara.visor.common.constant.AutoConfigureOrderConst;
|
||||
import org.dromara.visor.common.interfaces.Locker;
|
||||
import org.dromara.visor.common.lock.Locker;
|
||||
import org.dromara.visor.common.utils.LockerUtils;
|
||||
import org.dromara.visor.framework.redis.configuration.config.RedissonConfig;
|
||||
import org.dromara.visor.framework.redis.core.lock.RedisLocker;
|
||||
|
||||
@@ -22,12 +22,14 @@
|
||||
*/
|
||||
package org.dromara.visor.framework.redis.core.lock;
|
||||
|
||||
import cn.orionsec.kit.lang.able.Executable;
|
||||
import cn.orionsec.kit.lang.utils.Exceptions;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.visor.common.interfaces.Locker;
|
||||
import org.dromara.visor.common.lock.Locker;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
@@ -40,6 +42,8 @@ import java.util.function.Supplier;
|
||||
@Slf4j
|
||||
public class RedisLocker implements Locker {
|
||||
|
||||
private static final String LOCK_KEY_PREFIX = "lock:";
|
||||
|
||||
private final RedissonClient redissonClient;
|
||||
|
||||
public RedisLocker(RedissonClient redissonClient) {
|
||||
@@ -47,37 +51,154 @@ public class RedisLocker implements Locker {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryLock(String key, Runnable run) {
|
||||
public boolean tryLockExecute(String key, Executable executable) {
|
||||
return this.tryLockExecute(key, 0, executable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryLockExecute(String key, long timeout, Executable executable) {
|
||||
// 获取锁
|
||||
RLock lock = redissonClient.getLock(key);
|
||||
RLock lock = this.getLock(key);
|
||||
// 未获取到直接返回
|
||||
if (!lock.tryLock()) {
|
||||
log.info("RedisLocker.tryLock failed {}", key);
|
||||
if (this.tryLock(lock, timeout)) {
|
||||
return false;
|
||||
}
|
||||
// 执行
|
||||
try {
|
||||
run.run();
|
||||
executable.exec();
|
||||
} finally {
|
||||
lock.unlock();
|
||||
this.unlockSafe(lock);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T tryLock(String key, Supplier<T> call) {
|
||||
public <T> T tryLockExecute(String key, Supplier<T> callable) {
|
||||
return this.tryLockExecute(key, 0, callable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T tryLockExecute(String key, long timeout, Supplier<T> callable) {
|
||||
// 获取锁
|
||||
RLock lock = redissonClient.getLock(key);
|
||||
RLock lock = this.getLock(key);
|
||||
// 未获取到直接返回
|
||||
if (!lock.tryLock()) {
|
||||
log.info("RedisLocker.tryLock failed {}", key);
|
||||
if (this.tryLock(lock, timeout)) {
|
||||
throw Exceptions.lock();
|
||||
}
|
||||
// 执行
|
||||
try {
|
||||
return call.get();
|
||||
return callable.get();
|
||||
} finally {
|
||||
this.unlockSafe(lock);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lockExecute(String key, Executable executable) {
|
||||
this.lockExecute(key, 0, executable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lockExecute(String key, long timeout, Executable executable) {
|
||||
// 获取锁
|
||||
RLock lock = this.getLock(key);
|
||||
this.lock(lock, timeout);
|
||||
// 执行
|
||||
try {
|
||||
executable.exec();
|
||||
} finally {
|
||||
this.unlockSafe(lock);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T lockExecute(String key, Supplier<T> callable) {
|
||||
return this.lockExecute(key, 0, callable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T lockExecute(String key, long timeout, Supplier<T> callable) {
|
||||
// 获取锁
|
||||
RLock lock = this.getLock(key);
|
||||
this.lock(lock, timeout);
|
||||
// 执行
|
||||
try {
|
||||
return callable.get();
|
||||
} finally {
|
||||
this.unlockSafe(lock);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取锁
|
||||
*
|
||||
* @param key key
|
||||
* @return lock
|
||||
*/
|
||||
private RLock getLock(String key) {
|
||||
return redissonClient.getLock(LOCK_KEY_PREFIX + key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试上锁
|
||||
*
|
||||
* @param lock lock
|
||||
* @param timeout timeout
|
||||
* @return locked
|
||||
*/
|
||||
private boolean tryLock(RLock lock, long timeout) {
|
||||
boolean result;
|
||||
try {
|
||||
if (timeout == 0) {
|
||||
result = lock.tryLock();
|
||||
} else {
|
||||
result = lock.tryLock(timeout, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
if (!result) {
|
||||
log.warn("RedisLocker.tryLock failed {}", lock.getName());
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
log.error("RedisLocker.tryLock timed out {}", lock.getName(), e);
|
||||
throw Exceptions.lock(e);
|
||||
} catch (Exception e) {
|
||||
log.error("RedisLocker.tryLock error {}", lock.getName(), e);
|
||||
throw Exceptions.lock(e);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上锁
|
||||
*
|
||||
* @param lock lock
|
||||
* @param timeout timeout
|
||||
*/
|
||||
private void lock(RLock lock, long timeout) {
|
||||
try {
|
||||
if (timeout == 0) {
|
||||
lock.lock();
|
||||
} else {
|
||||
lock.lock(timeout, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("RedisLocker.lock lock error {}", lock.getName(), e);
|
||||
throw Exceptions.lock(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全的释放锁
|
||||
*
|
||||
* @param lock lock
|
||||
*/
|
||||
private void unlockSafe(RLock lock) {
|
||||
if (!lock.isHeldByCurrentThread()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
lock.unlock();
|
||||
} catch (Exception e) {
|
||||
log.warn("RedisLocker.unlock failed {}", lock.getName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
package org.dromara.visor.framework.storage.configuration;
|
||||
|
||||
import org.dromara.visor.common.constant.AutoConfigureOrderConst;
|
||||
import org.dromara.visor.common.interfaces.FileClient;
|
||||
import org.dromara.visor.common.file.FileClient;
|
||||
import org.dromara.visor.framework.storage.configuration.config.LocalStorageConfig;
|
||||
import org.dromara.visor.framework.storage.configuration.config.LogsStorageConfig;
|
||||
import org.dromara.visor.framework.storage.core.client.local.LocalFileClient;
|
||||
|
||||
@@ -26,7 +26,7 @@ import cn.orionsec.kit.lang.utils.io.Files1;
|
||||
import cn.orionsec.kit.lang.utils.io.Streams;
|
||||
import cn.orionsec.kit.lang.utils.time.Dates;
|
||||
import org.dromara.visor.common.constant.Const;
|
||||
import org.dromara.visor.common.interfaces.FileClient;
|
||||
import org.dromara.visor.common.file.FileClient;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
@@ -23,7 +23,8 @@
|
||||
package org.dromara.visor.framework.test.configuration;
|
||||
|
||||
import com.github.fppt.jedismock.RedisServer;
|
||||
import org.dromara.visor.common.interfaces.Locker;
|
||||
import org.dromara.visor.common.lock.EmptyLocker;
|
||||
import org.dromara.visor.common.lock.Locker;
|
||||
import org.dromara.visor.common.utils.LockerUtils;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
@@ -33,7 +34,6 @@ import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 单元测试 redis mock server 初始化
|
||||
@@ -66,18 +66,7 @@ public class OrionMockRedisTestConfiguration {
|
||||
*/
|
||||
@Bean
|
||||
public Locker unitTestLocker() {
|
||||
Locker locker = new Locker() {
|
||||
@Override
|
||||
public boolean tryLock(String key, Runnable run) {
|
||||
run.run();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T tryLock(String key, Supplier<T> call) {
|
||||
return call.get();
|
||||
}
|
||||
};
|
||||
EmptyLocker locker = new EmptyLocker();
|
||||
LockerUtils.setDelegate(locker);
|
||||
return locker;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user