🔨 优化锁逻辑.
This commit is contained in:
@@ -20,7 +20,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.dromara.visor.common.interfaces;
|
package org.dromara.visor.common.cipher;
|
||||||
|
|
||||||
import cn.orionsec.kit.lang.utils.codec.Base62s;
|
import cn.orionsec.kit.lang.utils.codec.Base62s;
|
||||||
import cn.orionsec.kit.lang.utils.crypto.symmetric.SymmetricCrypto;
|
import cn.orionsec.kit.lang.utils.crypto.symmetric.SymmetricCrypto;
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.dromara.visor.common.interfaces;
|
package org.dromara.visor.common.cipher;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* rsa 解密器
|
* rsa 解密器
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.dromara.visor.common.interfaces;
|
package org.dromara.visor.common.file;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2023 - present Dromara, All rights reserved.
|
|
||||||
*
|
|
||||||
* https://visor.dromara.org
|
|
||||||
* https://visor.dromara.org.cn
|
|
||||||
* https://visor.orionsec.cn
|
|
||||||
*
|
|
||||||
* Members:
|
|
||||||
* Jiahang Li - ljh1553488six@139.com - author
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package org.dromara.visor.common.interfaces;
|
|
||||||
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 分布式锁
|
|
||||||
*
|
|
||||||
* @author Jiahang Li
|
|
||||||
* @version 1.0.0
|
|
||||||
* @since 2024/5/16 12:24
|
|
||||||
*/
|
|
||||||
public interface Locker {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 尝试获取锁
|
|
||||||
*
|
|
||||||
* @param key key
|
|
||||||
* @param run run
|
|
||||||
* @return 是否获取到锁
|
|
||||||
*/
|
|
||||||
boolean tryLock(String key, Runnable run);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 尝试获取锁
|
|
||||||
*
|
|
||||||
* @param key key
|
|
||||||
* @param call call
|
|
||||||
* @param <T> T
|
|
||||||
* @return 执行结果
|
|
||||||
*/
|
|
||||||
<T> T tryLock(String key, Supplier<T> call);
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2023 - present Dromara, All rights reserved.
|
||||||
|
*
|
||||||
|
* https://visor.dromara.org
|
||||||
|
* https://visor.dromara.org.cn
|
||||||
|
* https://visor.orionsec.cn
|
||||||
|
*
|
||||||
|
* Members:
|
||||||
|
* Jiahang Li - ljh1553488six@139.com - author
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.dromara.visor.common.lock;
|
||||||
|
|
||||||
|
import cn.orionsec.kit.lang.able.Executable;
|
||||||
|
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 空实现的锁
|
||||||
|
*
|
||||||
|
* @author Jiahang Li
|
||||||
|
* @version 1.0.0
|
||||||
|
* @since 2025/8/23 13:59
|
||||||
|
*/
|
||||||
|
public class EmptyLocker implements Locker {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean tryLockExecute(String key, Executable executable) {
|
||||||
|
executable.exec();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean tryLockExecute(String key, long timeout, Executable executable) {
|
||||||
|
executable.exec();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T tryLockExecute(String key, Supplier<T> callable) {
|
||||||
|
return callable.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T tryLockExecute(String key, long timeout, Supplier<T> callable) {
|
||||||
|
return callable.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void lockExecute(String key, Executable executable) {
|
||||||
|
executable.exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void lockExecute(String key, long timeout, Executable executable) {
|
||||||
|
executable.exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T lockExecute(String key, Supplier<T> callable) {
|
||||||
|
return callable.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T lockExecute(String key, long timeout, Supplier<T> callable) {
|
||||||
|
return callable.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2023 - present Dromara, All rights reserved.
|
||||||
|
*
|
||||||
|
* https://visor.dromara.org
|
||||||
|
* https://visor.dromara.org.cn
|
||||||
|
* https://visor.orionsec.cn
|
||||||
|
*
|
||||||
|
* Members:
|
||||||
|
* Jiahang Li - ljh1553488six@139.com - author
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.dromara.visor.common.lock;
|
||||||
|
|
||||||
|
import cn.orionsec.kit.lang.able.Executable;
|
||||||
|
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分布式锁
|
||||||
|
*
|
||||||
|
* @author Jiahang Li
|
||||||
|
* @version 1.0.0
|
||||||
|
* @since 2024/5/16 12:24
|
||||||
|
*/
|
||||||
|
public interface Locker {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 尝试获取锁并执行
|
||||||
|
*
|
||||||
|
* @param key key
|
||||||
|
* @param executable exec
|
||||||
|
* @return 是否获取到锁
|
||||||
|
*/
|
||||||
|
boolean tryLockExecute(String key, Executable executable);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 尝试获取锁并执行
|
||||||
|
*
|
||||||
|
* @param key key
|
||||||
|
* @param timeout timeout
|
||||||
|
* @param executable exec
|
||||||
|
* @return 是否获取到锁
|
||||||
|
*/
|
||||||
|
boolean tryLockExecute(String key, long timeout, Executable executable);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 尝试获取锁并执行 未获取到锁则抛出异常
|
||||||
|
*
|
||||||
|
* @param key key
|
||||||
|
* @param callable callable
|
||||||
|
* @param <T> T
|
||||||
|
* @return 执行结果
|
||||||
|
*/
|
||||||
|
<T> T tryLockExecute(String key, Supplier<T> callable);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 尝试获取锁并执行 未获取到锁则抛出异常
|
||||||
|
*
|
||||||
|
* @param key key
|
||||||
|
* @param timeout timeout
|
||||||
|
* @param callable callable
|
||||||
|
* @param <T> T
|
||||||
|
* @return 执行结果
|
||||||
|
*/
|
||||||
|
<T> T tryLockExecute(String key, long timeout, Supplier<T> callable);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 阻塞获取锁并执行
|
||||||
|
*
|
||||||
|
* @param key key
|
||||||
|
* @param executable exec
|
||||||
|
*/
|
||||||
|
void lockExecute(String key, Executable executable);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 阻塞获取锁并执行
|
||||||
|
*
|
||||||
|
* @param key key
|
||||||
|
* @param timeout timeout
|
||||||
|
* @param executable exec
|
||||||
|
*/
|
||||||
|
void lockExecute(String key, long timeout, Executable executable);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 阻塞获取锁并执行
|
||||||
|
*
|
||||||
|
* @param key key
|
||||||
|
* @param callable callable
|
||||||
|
* @param <T> T
|
||||||
|
* @return 执行结果
|
||||||
|
*/
|
||||||
|
<T> T lockExecute(String key, Supplier<T> callable);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 阻塞获取锁并执行
|
||||||
|
*
|
||||||
|
* @param key key
|
||||||
|
* @param timeout timeout
|
||||||
|
* @param callable callable
|
||||||
|
* @param <T> T
|
||||||
|
* @return 执行结果
|
||||||
|
*/
|
||||||
|
<T> T lockExecute(String key, long timeout, Supplier<T> callable);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
package org.dromara.visor.common.utils;
|
package org.dromara.visor.common.utils;
|
||||||
|
|
||||||
import cn.orionsec.kit.lang.utils.Exceptions;
|
import cn.orionsec.kit.lang.utils.Exceptions;
|
||||||
import org.dromara.visor.common.interfaces.AesEncryptor;
|
import org.dromara.visor.common.cipher.AesEncryptor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* aes 数据加密工具类
|
* aes 数据加密工具类
|
||||||
|
|||||||
@@ -22,9 +22,10 @@
|
|||||||
*/
|
*/
|
||||||
package org.dromara.visor.common.utils;
|
package org.dromara.visor.common.utils;
|
||||||
|
|
||||||
|
import cn.orionsec.kit.lang.able.Executable;
|
||||||
import cn.orionsec.kit.lang.utils.Exceptions;
|
import cn.orionsec.kit.lang.utils.Exceptions;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.dromara.visor.common.interfaces.Locker;
|
import org.dromara.visor.common.lock.Locker;
|
||||||
|
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
@@ -44,26 +45,97 @@ public class LockerUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 尝试获取锁
|
* 尝试获取锁并执行
|
||||||
*
|
*
|
||||||
* @param key key
|
* @param key key
|
||||||
* @param run run
|
* @param executable exec
|
||||||
* @return 是否获取到锁
|
* @return 是否获取到锁
|
||||||
*/
|
*/
|
||||||
public static boolean tryLock(String key, Runnable run) {
|
public static boolean tryLockExecute(String key, Executable executable) {
|
||||||
return delegate.tryLock(key, run);
|
return delegate.tryLockExecute(key, executable);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 尝试获取锁
|
* 尝试获取锁并执行
|
||||||
*
|
*
|
||||||
* @param key key
|
* @param key key
|
||||||
* @param call call
|
* @param timeout timeout
|
||||||
* @param <T> T
|
* @param executable exec
|
||||||
|
* @return 是否获取到锁
|
||||||
|
*/
|
||||||
|
public static boolean tryLockExecute(String key, long timeout, Executable executable) {
|
||||||
|
return delegate.tryLockExecute(key, timeout, executable);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 尝试获取锁并执行 未获取到锁则抛出异常
|
||||||
|
*
|
||||||
|
* @param key key
|
||||||
|
* @param callable callable
|
||||||
|
* @param <T> T
|
||||||
* @return 执行结果
|
* @return 执行结果
|
||||||
*/
|
*/
|
||||||
public static <T> T tryLock(String key, Supplier<T> call) {
|
public static <T> T tryLockExecute(String key, Supplier<T> callable) {
|
||||||
return delegate.tryLock(key, call);
|
return delegate.tryLockExecute(key, callable);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 尝试获取锁并执行 未获取到锁则抛出异常
|
||||||
|
*
|
||||||
|
* @param key key
|
||||||
|
* @param timeout timeout
|
||||||
|
* @param callable callable
|
||||||
|
* @param <T> T
|
||||||
|
* @return 执行结果
|
||||||
|
*/
|
||||||
|
public static <T> T tryLockExecute(String key, long timeout, Supplier<T> callable) {
|
||||||
|
return delegate.tryLockExecute(key, timeout, callable);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 阻塞获取锁并执行
|
||||||
|
*
|
||||||
|
* @param key key
|
||||||
|
* @param executable exec
|
||||||
|
*/
|
||||||
|
public static void lockExecute(String key, Executable executable) {
|
||||||
|
delegate.lockExecute(key, executable);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 阻塞获取锁并执行
|
||||||
|
*
|
||||||
|
* @param key key
|
||||||
|
* @param timeout timeout
|
||||||
|
* @param executable exec
|
||||||
|
*/
|
||||||
|
public static void lockExecute(String key, long timeout, Executable executable) {
|
||||||
|
delegate.lockExecute(key, timeout, executable);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 阻塞获取锁并执行
|
||||||
|
*
|
||||||
|
* @param key key
|
||||||
|
* @param callable callable
|
||||||
|
* @param <T> T
|
||||||
|
* @return 执行结果
|
||||||
|
*/
|
||||||
|
public static <T> T lockExecute(String key, Supplier<T> callable) {
|
||||||
|
return delegate.lockExecute(key, callable);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 阻塞获取锁并执行
|
||||||
|
*
|
||||||
|
* @param key key
|
||||||
|
* @param timeout timeout
|
||||||
|
* @param callable callable
|
||||||
|
* @param <T> T
|
||||||
|
* @return 执行结果
|
||||||
|
*/
|
||||||
|
public static <T> T lockExecute(String key, long timeout, Supplier<T> callable) {
|
||||||
|
return delegate.lockExecute(key, timeout, callable);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setDelegate(Locker delegate) {
|
public static void setDelegate(Locker delegate) {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
package org.dromara.visor.common.utils;
|
package org.dromara.visor.common.utils;
|
||||||
|
|
||||||
import cn.orionsec.kit.lang.utils.Exceptions;
|
import cn.orionsec.kit.lang.utils.Exceptions;
|
||||||
import org.dromara.visor.common.interfaces.RsaDecryptor;
|
import org.dromara.visor.common.cipher.RsaDecryptor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* rsa 参数解密工具类
|
* rsa 参数解密工具类
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ package org.dromara.visor.framework.encrypt.configuration;
|
|||||||
|
|
||||||
import org.dromara.visor.common.config.ConfigStore;
|
import org.dromara.visor.common.config.ConfigStore;
|
||||||
import org.dromara.visor.common.constant.AutoConfigureOrderConst;
|
import org.dromara.visor.common.constant.AutoConfigureOrderConst;
|
||||||
import org.dromara.visor.common.interfaces.AesEncryptor;
|
import org.dromara.visor.common.cipher.AesEncryptor;
|
||||||
import org.dromara.visor.common.interfaces.RsaDecryptor;
|
import org.dromara.visor.common.cipher.RsaDecryptor;
|
||||||
import org.dromara.visor.common.utils.AesEncryptUtils;
|
import org.dromara.visor.common.utils.AesEncryptUtils;
|
||||||
import org.dromara.visor.common.utils.RsaParamDecryptUtils;
|
import org.dromara.visor.common.utils.RsaParamDecryptUtils;
|
||||||
import org.dromara.visor.framework.encrypt.configuration.config.AesEncryptConfig;
|
import org.dromara.visor.framework.encrypt.configuration.config.AesEncryptConfig;
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.dromara.visor.framework.encrypt.core;
|
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.ConfigRef;
|
||||||
import org.dromara.visor.common.config.ConfigStore;
|
import org.dromara.visor.common.config.ConfigStore;
|
||||||
import org.dromara.visor.common.constant.ConfigKeys;
|
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.security.interfaces.RSAPrivateKey;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ package org.dromara.visor.framework.redis.configuration;
|
|||||||
|
|
||||||
import com.github.fppt.jedismock.RedisServer;
|
import com.github.fppt.jedismock.RedisServer;
|
||||||
import org.dromara.visor.common.constant.AutoConfigureOrderConst;
|
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.dromara.visor.common.utils.LockerUtils;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
|
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 org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||||
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MockRedis
|
* MockRedis
|
||||||
@@ -79,18 +79,7 @@ public class OrionMockRedisAutoConfiguration {
|
|||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public Locker redisLocker() {
|
public Locker redisLocker() {
|
||||||
Locker locker = new Locker() {
|
EmptyLocker locker = new EmptyLocker();
|
||||||
@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();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
LockerUtils.setDelegate(locker);
|
LockerUtils.setDelegate(locker);
|
||||||
return locker;
|
return locker;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ package org.dromara.visor.framework.redis.configuration;
|
|||||||
|
|
||||||
import cn.orionsec.kit.lang.define.cache.key.CacheKeyDefine;
|
import cn.orionsec.kit.lang.define.cache.key.CacheKeyDefine;
|
||||||
import org.dromara.visor.common.constant.AutoConfigureOrderConst;
|
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.common.utils.LockerUtils;
|
||||||
import org.dromara.visor.framework.redis.configuration.config.RedissonConfig;
|
import org.dromara.visor.framework.redis.configuration.config.RedissonConfig;
|
||||||
import org.dromara.visor.framework.redis.core.lock.RedisLocker;
|
import org.dromara.visor.framework.redis.core.lock.RedisLocker;
|
||||||
|
|||||||
@@ -22,12 +22,14 @@
|
|||||||
*/
|
*/
|
||||||
package org.dromara.visor.framework.redis.core.lock;
|
package org.dromara.visor.framework.redis.core.lock;
|
||||||
|
|
||||||
|
import cn.orionsec.kit.lang.able.Executable;
|
||||||
import cn.orionsec.kit.lang.utils.Exceptions;
|
import cn.orionsec.kit.lang.utils.Exceptions;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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.RLock;
|
||||||
import org.redisson.api.RedissonClient;
|
import org.redisson.api.RedissonClient;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -40,6 +42,8 @@ import java.util.function.Supplier;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class RedisLocker implements Locker {
|
public class RedisLocker implements Locker {
|
||||||
|
|
||||||
|
private static final String LOCK_KEY_PREFIX = "lock:";
|
||||||
|
|
||||||
private final RedissonClient redissonClient;
|
private final RedissonClient redissonClient;
|
||||||
|
|
||||||
public RedisLocker(RedissonClient redissonClient) {
|
public RedisLocker(RedissonClient redissonClient) {
|
||||||
@@ -47,37 +51,154 @@ public class RedisLocker implements Locker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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()) {
|
if (this.tryLock(lock, timeout)) {
|
||||||
log.info("RedisLocker.tryLock failed {}", key);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// 执行
|
// 执行
|
||||||
try {
|
try {
|
||||||
run.run();
|
executable.exec();
|
||||||
} finally {
|
} finally {
|
||||||
lock.unlock();
|
this.unlockSafe(lock);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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()) {
|
if (this.tryLock(lock, timeout)) {
|
||||||
log.info("RedisLocker.tryLock failed {}", key);
|
|
||||||
throw Exceptions.lock();
|
throw Exceptions.lock();
|
||||||
}
|
}
|
||||||
// 执行
|
// 执行
|
||||||
try {
|
try {
|
||||||
return call.get();
|
return callable.get();
|
||||||
} finally {
|
} 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();
|
lock.unlock();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("RedisLocker.unlock failed {}", lock.getName(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
package org.dromara.visor.framework.storage.configuration;
|
package org.dromara.visor.framework.storage.configuration;
|
||||||
|
|
||||||
import org.dromara.visor.common.constant.AutoConfigureOrderConst;
|
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.LocalStorageConfig;
|
||||||
import org.dromara.visor.framework.storage.configuration.config.LogsStorageConfig;
|
import org.dromara.visor.framework.storage.configuration.config.LogsStorageConfig;
|
||||||
import org.dromara.visor.framework.storage.core.client.local.LocalFileClient;
|
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.io.Streams;
|
||||||
import cn.orionsec.kit.lang.utils.time.Dates;
|
import cn.orionsec.kit.lang.utils.time.Dates;
|
||||||
import org.dromara.visor.common.constant.Const;
|
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.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|||||||
@@ -23,7 +23,8 @@
|
|||||||
package org.dromara.visor.framework.test.configuration;
|
package org.dromara.visor.framework.test.configuration;
|
||||||
|
|
||||||
import com.github.fppt.jedismock.RedisServer;
|
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.dromara.visor.common.utils.LockerUtils;
|
||||||
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
@@ -33,7 +34,6 @@ import org.springframework.context.annotation.Lazy;
|
|||||||
import org.springframework.context.annotation.Profile;
|
import org.springframework.context.annotation.Profile;
|
||||||
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 单元测试 redis mock server 初始化
|
* 单元测试 redis mock server 初始化
|
||||||
@@ -66,18 +66,7 @@ public class OrionMockRedisTestConfiguration {
|
|||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public Locker unitTestLocker() {
|
public Locker unitTestLocker() {
|
||||||
Locker locker = new Locker() {
|
EmptyLocker locker = new EmptyLocker();
|
||||||
@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();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
LockerUtils.setDelegate(locker);
|
LockerUtils.setDelegate(locker);
|
||||||
return locker;
|
return locker;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ import org.dromara.visor.common.constant.ErrorMessage;
|
|||||||
import org.dromara.visor.common.constant.FileConst;
|
import org.dromara.visor.common.constant.FileConst;
|
||||||
import org.dromara.visor.common.enums.BooleanBit;
|
import org.dromara.visor.common.enums.BooleanBit;
|
||||||
import org.dromara.visor.common.enums.EndpointDefine;
|
import org.dromara.visor.common.enums.EndpointDefine;
|
||||||
import org.dromara.visor.common.interfaces.FileClient;
|
import org.dromara.visor.common.file.FileClient;
|
||||||
import org.dromara.visor.common.session.config.SshConnectConfig;
|
import org.dromara.visor.common.session.config.SshConnectConfig;
|
||||||
import org.dromara.visor.common.session.ssh.SessionStores;
|
import org.dromara.visor.common.session.ssh.SessionStores;
|
||||||
import org.dromara.visor.common.utils.PathUtils;
|
import org.dromara.visor.common.utils.PathUtils;
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ import lombok.Getter;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.dromara.visor.common.constant.Const;
|
import org.dromara.visor.common.constant.Const;
|
||||||
import org.dromara.visor.common.constant.ErrorMessage;
|
import org.dromara.visor.common.constant.ErrorMessage;
|
||||||
import org.dromara.visor.common.interfaces.FileClient;
|
import org.dromara.visor.common.file.FileClient;
|
||||||
import org.dromara.visor.common.utils.Valid;
|
import org.dromara.visor.common.utils.Valid;
|
||||||
import org.dromara.visor.framework.websocket.core.utils.WebSockets;
|
import org.dromara.visor.framework.websocket.core.utils.WebSockets;
|
||||||
import org.dromara.visor.module.common.config.AppLogConfig;
|
import org.dromara.visor.module.common.config.AppLogConfig;
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ import org.dromara.visor.common.constant.Const;
|
|||||||
import org.dromara.visor.common.constant.ErrorMessage;
|
import org.dromara.visor.common.constant.ErrorMessage;
|
||||||
import org.dromara.visor.common.constant.ExtraFieldConst;
|
import org.dromara.visor.common.constant.ExtraFieldConst;
|
||||||
import org.dromara.visor.common.enums.EndpointDefine;
|
import org.dromara.visor.common.enums.EndpointDefine;
|
||||||
import org.dromara.visor.common.interfaces.FileClient;
|
import org.dromara.visor.common.file.FileClient;
|
||||||
import org.dromara.visor.common.session.config.SshConnectConfig;
|
import org.dromara.visor.common.session.config.SshConnectConfig;
|
||||||
import org.dromara.visor.common.session.ssh.SessionStores;
|
import org.dromara.visor.common.session.ssh.SessionStores;
|
||||||
import org.dromara.visor.common.utils.PathUtils;
|
import org.dromara.visor.common.utils.PathUtils;
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ import org.dromara.visor.common.constant.Const;
|
|||||||
import org.dromara.visor.common.constant.ErrorMessage;
|
import org.dromara.visor.common.constant.ErrorMessage;
|
||||||
import org.dromara.visor.common.constant.FileConst;
|
import org.dromara.visor.common.constant.FileConst;
|
||||||
import org.dromara.visor.common.enums.EndpointDefine;
|
import org.dromara.visor.common.enums.EndpointDefine;
|
||||||
import org.dromara.visor.common.interfaces.FileClient;
|
import org.dromara.visor.common.file.FileClient;
|
||||||
import org.dromara.visor.common.utils.SqlUtils;
|
import org.dromara.visor.common.utils.SqlUtils;
|
||||||
import org.dromara.visor.common.utils.Valid;
|
import org.dromara.visor.common.utils.Valid;
|
||||||
import org.dromara.visor.framework.biz.operator.log.core.utils.OperatorLogs;
|
import org.dromara.visor.framework.biz.operator.log.core.utils.OperatorLogs;
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.dromara.visor.common.constant.Const;
|
import org.dromara.visor.common.constant.Const;
|
||||||
import org.dromara.visor.common.constant.ErrorMessage;
|
import org.dromara.visor.common.constant.ErrorMessage;
|
||||||
import org.dromara.visor.common.enums.EndpointDefine;
|
import org.dromara.visor.common.enums.EndpointDefine;
|
||||||
import org.dromara.visor.common.interfaces.FileClient;
|
import org.dromara.visor.common.file.FileClient;
|
||||||
import org.dromara.visor.common.security.LoginUser;
|
import org.dromara.visor.common.security.LoginUser;
|
||||||
import org.dromara.visor.common.utils.SqlUtils;
|
import org.dromara.visor.common.utils.SqlUtils;
|
||||||
import org.dromara.visor.framework.biz.operator.log.core.utils.OperatorLogs;
|
import org.dromara.visor.framework.biz.operator.log.core.utils.OperatorLogs;
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ public class ExecLogFileAutoClearTask {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 获取锁并执行
|
// 获取锁并执行
|
||||||
LockerUtils.tryLock(LOCK_KEY, this::doClear);
|
LockerUtils.tryLockExecute(LOCK_KEY, this::doClear);
|
||||||
log.info("ExecLogFileAutoClearTask.clear finish");
|
log.info("ExecLogFileAutoClearTask.clear finish");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ import cn.orionsec.kit.lang.utils.io.Streams;
|
|||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.dromara.visor.common.constant.ExtraFieldConst;
|
import org.dromara.visor.common.constant.ExtraFieldConst;
|
||||||
import org.dromara.visor.common.interfaces.FileClient;
|
import org.dromara.visor.common.file.FileClient;
|
||||||
import org.dromara.visor.framework.websocket.core.utils.WebSockets;
|
import org.dromara.visor.framework.websocket.core.utils.WebSockets;
|
||||||
import org.dromara.visor.module.infra.entity.dto.FileUploadTokenDTO;
|
import org.dromara.visor.module.infra.entity.dto.FileUploadTokenDTO;
|
||||||
import org.dromara.visor.module.infra.handler.upload.enums.FileUploadOperatorType;
|
import org.dromara.visor.module.infra.handler.upload.enums.FileUploadOperatorType;
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ package org.dromara.visor.module.infra.handler.upload.handler;
|
|||||||
import cn.orionsec.kit.lang.utils.io.Streams;
|
import cn.orionsec.kit.lang.utils.io.Streams;
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import org.dromara.visor.common.constant.Const;
|
import org.dromara.visor.common.constant.Const;
|
||||||
import org.dromara.visor.common.interfaces.FileClient;
|
import org.dromara.visor.common.file.FileClient;
|
||||||
import org.dromara.visor.framework.websocket.core.utils.WebSockets;
|
import org.dromara.visor.framework.websocket.core.utils.WebSockets;
|
||||||
import org.dromara.visor.module.infra.handler.upload.enums.FileUploadReceiverType;
|
import org.dromara.visor.module.infra.handler.upload.enums.FileUploadReceiverType;
|
||||||
import org.dromara.visor.module.infra.handler.upload.model.FileUploadResponse;
|
import org.dromara.visor.module.infra.handler.upload.model.FileUploadResponse;
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ public class TagAutoClearTask {
|
|||||||
public void clear() {
|
public void clear() {
|
||||||
log.info("TagAutoClearTask.clear start");
|
log.info("TagAutoClearTask.clear start");
|
||||||
// 获取锁并执行
|
// 获取锁并执行
|
||||||
LockerUtils.tryLock(LOCK_KEY, tagService::clearUnusedTag);
|
LockerUtils.tryLockExecute(LOCK_KEY, tagService::clearUnusedTag);
|
||||||
log.info("TagAutoClearTask.clear finish");
|
log.info("TagAutoClearTask.clear finish");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ public class CommandSnippetGroupAutoClearTask {
|
|||||||
public void clear() {
|
public void clear() {
|
||||||
log.info("CommandSnippetGroupAutoClearTask.clear start");
|
log.info("CommandSnippetGroupAutoClearTask.clear start");
|
||||||
// 获取锁并执行
|
// 获取锁并执行
|
||||||
LockerUtils.tryLock(LOCK_KEY, commandSnippetGroupService::clearUnusedGroup);
|
LockerUtils.tryLockExecute(LOCK_KEY, commandSnippetGroupService::clearUnusedGroup);
|
||||||
log.info("CommandSnippetGroupAutoClearTask.clear finish");
|
log.info("CommandSnippetGroupAutoClearTask.clear finish");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ public class PathBookmarkGroupAutoClearTask {
|
|||||||
public void clear() {
|
public void clear() {
|
||||||
log.info("PathBookmarkGroupAutoClearTask.clear start");
|
log.info("PathBookmarkGroupAutoClearTask.clear start");
|
||||||
// 获取锁并执行
|
// 获取锁并执行
|
||||||
LockerUtils.tryLock(LOCK_KEY, pathBookmarkGroupService::clearUnusedGroup);
|
LockerUtils.tryLockExecute(LOCK_KEY, pathBookmarkGroupService::clearUnusedGroup);
|
||||||
log.info("PathBookmarkGroupAutoClearTask.clear finish");
|
log.info("PathBookmarkGroupAutoClearTask.clear finish");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ public class TerminalConnectLogAutoClearTask {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 获取锁并执行
|
// 获取锁并执行
|
||||||
LockerUtils.tryLock(LOCK_KEY, this::doClear);
|
LockerUtils.tryLockExecute(LOCK_KEY, this::doClear);
|
||||||
log.info("TerminalConnectLogAutoClearTask.clear finish");
|
log.info("TerminalConnectLogAutoClearTask.clear finish");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user