✨ 添加分布式锁组件.
This commit is contained in:
@@ -2,7 +2,10 @@ package com.orion.ops.framework.redis.configuration;
|
||||
|
||||
import com.orion.ops.framework.common.constant.AutoConfigureOrderConst;
|
||||
import com.orion.ops.framework.redis.configuration.config.RedissonConfig;
|
||||
import com.orion.ops.framework.redis.core.lock.RedisLocker;
|
||||
import com.orion.ops.framework.redis.core.utils.RedisLocks;
|
||||
import com.orion.ops.framework.redis.core.utils.RedisUtils;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
|
||||
@@ -56,5 +59,16 @@ public class OrionRedisAutoConfiguration {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param redissonClient redissonClient
|
||||
* @return redis 分布式锁
|
||||
*/
|
||||
@Bean
|
||||
public RedisLocker redisLocker(RedissonClient redissonClient) {
|
||||
RedisLocker redisLocker = new RedisLocker(redissonClient);
|
||||
RedisLocks.setRedisLocker(redisLocker);
|
||||
return redisLocker;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.orion.ops.framework.redis.core.lock;
|
||||
|
||||
import com.orion.lang.utils.Exceptions;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* redis 分布式锁
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2024/4/25 16:42
|
||||
*/
|
||||
@Slf4j
|
||||
public class RedisLocker {
|
||||
|
||||
private final RedissonClient redissonClient;
|
||||
|
||||
public RedisLocker(RedissonClient redissonClient) {
|
||||
this.redissonClient = redissonClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试获取锁
|
||||
*
|
||||
* @param key key
|
||||
* @param run run
|
||||
* @return 是否获取到锁
|
||||
*/
|
||||
public boolean tryLock(String key, Runnable run) {
|
||||
// 获取锁
|
||||
RLock lock = redissonClient.getLock(key);
|
||||
// 未获取到直接返回
|
||||
if (!lock.tryLock()) {
|
||||
log.info("RedisLocks.tryLock failed {}", key);
|
||||
return false;
|
||||
}
|
||||
// 执行
|
||||
try {
|
||||
run.run();
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试获取锁
|
||||
*
|
||||
* @param key key
|
||||
* @param call call
|
||||
* @param <T> T
|
||||
* @return 执行结果
|
||||
*/
|
||||
public <T> T tryLock(String key, Supplier<T> call) {
|
||||
// 获取锁
|
||||
RLock lock = redissonClient.getLock(key);
|
||||
// 未获取到直接返回
|
||||
if (!lock.tryLock()) {
|
||||
log.info("RedisLocks.tryLock failed {}", key);
|
||||
throw Exceptions.lock();
|
||||
}
|
||||
// 执行
|
||||
try {
|
||||
return call.get();
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.orion.ops.framework.redis.core.utils;
|
||||
|
||||
import com.orion.lang.utils.Exceptions;
|
||||
import com.orion.ops.framework.redis.core.lock.RedisLocker;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* redis 分布式锁工具类
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2024/4/25 16:42
|
||||
*/
|
||||
@Slf4j
|
||||
public class RedisLocks {
|
||||
|
||||
private static RedisLocker redisLocker;
|
||||
|
||||
private RedisLocks() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试获取锁
|
||||
*
|
||||
* @param key key
|
||||
* @param run run
|
||||
* @return 是否获取到锁
|
||||
*/
|
||||
public static boolean tryLock(String key, Runnable run) {
|
||||
return redisLocker.tryLock(key, run);
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试获取锁
|
||||
*
|
||||
* @param key key
|
||||
* @param call call
|
||||
* @param <T> T
|
||||
* @return 执行结果
|
||||
*/
|
||||
public static <T> T tryLock(String key, Supplier<T> call) {
|
||||
return redisLocker.tryLock(key, call);
|
||||
}
|
||||
|
||||
public static void setRedisLocker(RedisLocker redisLocker) {
|
||||
if (RedisLocks.redisLocker != null) {
|
||||
// unmodified
|
||||
throw Exceptions.state();
|
||||
}
|
||||
RedisLocks.redisLocker = redisLocker;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user