⚡ 单元测试添加锁.
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package com.orion.visor.framework.common.lock;
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
@@ -1,24 +1,24 @@
|
||||
package com.orion.visor.framework.redis.core.utils;
|
||||
package com.orion.visor.framework.common.utils;
|
||||
|
||||
import com.orion.lang.utils.Exceptions;
|
||||
import com.orion.visor.framework.redis.core.lock.RedisLocker;
|
||||
import com.orion.visor.framework.common.lock.Locker;
|
||||
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 {
|
||||
public class LockerUtils {
|
||||
|
||||
private static RedisLocker redisLocker;
|
||||
private static Locker delegate;
|
||||
|
||||
private RedisLocks() {
|
||||
private LockerUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -29,7 +29,7 @@ public class RedisLocks {
|
||||
* @return 是否获取到锁
|
||||
*/
|
||||
public static boolean tryLock(String key, Runnable run) {
|
||||
return redisLocker.tryLock(key, run);
|
||||
return delegate.tryLock(key, run);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -41,15 +41,15 @@ public class RedisLocks {
|
||||
* @return 执行结果
|
||||
*/
|
||||
public static <T> T tryLock(String key, Supplier<T> call) {
|
||||
return redisLocker.tryLock(key, call);
|
||||
return delegate.tryLock(key, call);
|
||||
}
|
||||
|
||||
public static void setRedisLocker(RedisLocker redisLocker) {
|
||||
if (RedisLocks.redisLocker != null) {
|
||||
public static void setDelegate(Locker delegate) {
|
||||
if (LockerUtils.delegate != null) {
|
||||
// unmodified
|
||||
throw Exceptions.state();
|
||||
}
|
||||
RedisLocks.redisLocker = redisLocker;
|
||||
LockerUtils.delegate = delegate;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,9 +2,12 @@ package ${currentPackage};
|
||||
|
||||
import com.orion.lang.define.cache.key.model.LongCacheIdModel;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import java.io.Serializable;
|
||||
import lombok.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
import java.math.*;
|
||||
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package ${currentPackage};
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package ${currentPackage};
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import java.io.Serializable;
|
||||
import lombok.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
import java.math.*;
|
||||
|
||||
|
||||
@@ -60,7 +60,7 @@
|
||||
#if(${dictMap.containsKey(${field.propertyName})})
|
||||
<!-- $field.comment -->
|
||||
<template #${field.propertyName}="{ record }">
|
||||
{{ getDictValue($dictMap.get(${field.propertyName}).keyField, record.${field.propertyName}}) }}
|
||||
{{ getDictValue($dictMap.get(${field.propertyName}).keyField, record.${field.propertyName}) }}
|
||||
</template>
|
||||
#end
|
||||
#end
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
package com.orion.visor.framework.redis.configuration;
|
||||
|
||||
import com.orion.visor.framework.common.constant.AutoConfigureOrderConst;
|
||||
import com.orion.visor.framework.common.lock.Locker;
|
||||
import com.orion.visor.framework.common.utils.LockerUtils;
|
||||
import com.orion.visor.framework.redis.configuration.config.RedissonConfig;
|
||||
import com.orion.visor.framework.redis.core.lock.RedisLocker;
|
||||
import com.orion.visor.framework.redis.core.utils.RedisLocks;
|
||||
import com.orion.visor.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;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
@@ -23,7 +25,7 @@ import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
* @version 1.0.0
|
||||
* @since 2023/6/28 14:44
|
||||
*/
|
||||
@Lazy(value = false)
|
||||
@Lazy(false)
|
||||
@AutoConfiguration
|
||||
@AutoConfigureOrder(AutoConfigureOrderConst.FRAMEWORK_REDIS)
|
||||
@EnableConfigurationProperties(RedissonConfig.class)
|
||||
@@ -64,9 +66,10 @@ public class OrionRedisAutoConfiguration {
|
||||
* @return redis 分布式锁
|
||||
*/
|
||||
@Bean
|
||||
public RedisLocker redisLocker(RedissonClient redissonClient) {
|
||||
@ConditionalOnMissingBean
|
||||
public Locker redisLocker(RedissonClient redissonClient) {
|
||||
RedisLocker redisLocker = new RedisLocker(redissonClient);
|
||||
RedisLocks.setRedisLocker(redisLocker);
|
||||
LockerUtils.setDelegate(redisLocker);
|
||||
return redisLocker;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,4 +24,9 @@ public class RedissonConfig {
|
||||
*/
|
||||
private Integer nettyThreads;
|
||||
|
||||
public RedissonConfig() {
|
||||
this.threads = 16;
|
||||
this.nettyThreads = 16;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.orion.visor.framework.redis.core.lock;
|
||||
|
||||
import com.orion.lang.utils.Exceptions;
|
||||
import com.orion.visor.framework.common.lock.Locker;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
@@ -15,7 +16,7 @@ import java.util.function.Supplier;
|
||||
* @since 2024/4/25 16:42
|
||||
*/
|
||||
@Slf4j
|
||||
public class RedisLocker {
|
||||
public class RedisLocker implements Locker {
|
||||
|
||||
private final RedissonClient redissonClient;
|
||||
|
||||
@@ -23,19 +24,13 @@ public class RedisLocker {
|
||||
this.redissonClient = redissonClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试获取锁
|
||||
*
|
||||
* @param key key
|
||||
* @param run run
|
||||
* @return 是否获取到锁
|
||||
*/
|
||||
@Override
|
||||
public boolean tryLock(String key, Runnable run) {
|
||||
// 获取锁
|
||||
RLock lock = redissonClient.getLock(key);
|
||||
// 未获取到直接返回
|
||||
if (!lock.tryLock()) {
|
||||
log.info("RedisLocks.tryLock failed {}", key);
|
||||
log.info("RedisLocker.tryLock failed {}", key);
|
||||
return false;
|
||||
}
|
||||
// 执行
|
||||
@@ -47,20 +42,13 @@ public class RedisLocker {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试获取锁
|
||||
*
|
||||
* @param key key
|
||||
* @param call call
|
||||
* @param <T> T
|
||||
* @return 执行结果
|
||||
*/
|
||||
@Override
|
||||
public <T> T tryLock(String key, Supplier<T> call) {
|
||||
// 获取锁
|
||||
RLock lock = redissonClient.getLock(key);
|
||||
// 未获取到直接返回
|
||||
if (!lock.tryLock()) {
|
||||
log.info("RedisLocks.tryLock failed {}", key);
|
||||
log.info("RedisLocker.tryLock failed {}", key);
|
||||
throw Exceptions.lock();
|
||||
}
|
||||
// 执行
|
||||
|
||||
@@ -10,12 +10,14 @@
|
||||
{
|
||||
"name": "spring.redisson.threads",
|
||||
"type": "java.lang.Integer",
|
||||
"description": "任务线程数."
|
||||
"description": "任务线程数.",
|
||||
"defaultValue": "16"
|
||||
},
|
||||
{
|
||||
"name": "spring.redisson.netty-threads",
|
||||
"type": "java.lang.Integer",
|
||||
"description": "netty 线程数."
|
||||
"description": "netty 线程数.",
|
||||
"defaultValue": "16"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -22,8 +22,8 @@ import javax.sql.DataSource;
|
||||
* @version 1.0.0
|
||||
* @since 2023/8/23 17:17
|
||||
*/
|
||||
@Lazy(false)
|
||||
@Profile("unit-test")
|
||||
@Lazy(value = false)
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnMissingBean(AbstractScriptDatabaseInitializer.class)
|
||||
@ConditionalOnSingleCandidate(DataSource.class)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.orion.visor.framework.test.configuration;
|
||||
|
||||
import com.github.fppt.jedismock.RedisServer;
|
||||
import com.orion.visor.framework.common.lock.Locker;
|
||||
import com.orion.visor.framework.common.utils.LockerUtils;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -8,6 +10,8 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 单元测试 redis mock server 初始化
|
||||
*
|
||||
@@ -34,4 +38,25 @@ public class OrionMockRedisTestConfiguration {
|
||||
return server;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 单元测试分布式锁
|
||||
*/
|
||||
@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();
|
||||
}
|
||||
};
|
||||
LockerUtils.setDelegate(locker);
|
||||
return locker;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user