一、背景
基于redisson的分布式锁实现,我们可以比较容易的控制竞态资源的分布式并发控制,但是使用的时候会出现很多重复的try-catch-finally代码块,获取锁、加锁和释放锁等,用法大致如下:
RLock lock = redissonClient.getLock("lock_name");
try {
if(lock.tryLock(5,10, TimeUnit.SECONDS)) {
//do something
}
} catch (Exception e) {
log.error("occur error",e);
} finally {
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
写代码讲究一个优雅和高效,作为一个有追求的程序员,项目中出现大面积重复性的手动加锁解锁以及try-catch-finally代码块是不能接受的。
从锁使用方式中,我们可以抽象出通用的部分,try-catch-finally代码块,以及获取锁、加锁和解锁逻辑,那么有没有一种方式把这些逻辑抽取到一个地方管理,然后在需要使用锁的地方,通过简单的方式引入加锁解锁逻辑?
答案是可以的,我们可以结合自定义注解和切面,把加锁逻辑封装起来,然后拦截使用了解锁注解的方法,把加锁解锁逻辑织入进去就可以了。
二、写成starter复用
新建一个starter工程,把加锁逻辑封装到切面,然后通过注解的方式提供给业务使用。
1.引入依赖
引入redis和redisson相关依赖:
org.springframework.boot
spring-boot-starter-data-redis
org.redisson
redisson-spring-boot-starter
2.定义分布式锁注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface XLock {
String key() default "";
@AliasFor("key")
String value() default "";
long waitTime() default 5;
long leaseTime() default 30;
TimeUnit unit() default TimeUnit.SECONDS;
}
定义加锁的key,等待时间,锁释放时间和时间单位。
3.定义分布式锁属性
@ConfigurationProperties(prefix = "redisson.redis")
@Data
public class RedissonProperties {
private String port;
private String password;
private int database = 0;
/**
* redis服务端类型
* @see ServerType
*/
private Integer serverType;
private SingleServer singleServer;
private ClusterServers clusterServers;
private MasterSlaveServers masterSlaveServers;
private ReplicatedServers replicatedServers;
private SentinelServers sentinelServers;
@Data
public static class SingleServer {
private String host;
}
@Data
public static class ClusterServers {
private String hosts;//多个用逗号隔开
}
@Data
public static class MasterSlaveServers {
private String masterHost;
private String slaveHosts;//多个用逗号隔开
}
@Data
public static class ReplicatedServers {
private String hosts;
}
@Data
public static class SentinelServers {
private String masterName;
private String hosts;
}
}
包含了端口,密码,默认数据库,以及redis server各种模式的支持。
在使用的时候需要在配置文件中添加如下类似的配置:
redisson:
redis:
port: 6379
password: xxxxxx
database: 0
serverType: 1 #2,3,4,5
singleServer:
host: 127.0.0.1
clusterServers:
hosts: 192.168.0.1,192.168.0.2,192.168.0.3
masterSlaveServers:
masterHost: 127.0.0.1
slaveHosts: 192.168.0.1,192.168.0.2
replicatedServers:
hosts: 192.168.0.1,192.168.0.2,192.168.0.3
sentinelServers:
masterName: masterNode
hosts: 192.168.0.1,192.168.0.2,192.168.0.3
4.编写切面逻辑
@Slf4j
@Aspect
@Order(Ordered.LOWEST_PRECEDENCE - 1)
public class XLockInterceptor {
@Autowired
private RedissonClient redissonClient;
private ExpressionParser parser = new SpelExpressionParser();
private LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();
@Around("@annotation(lock.starter.annotation.XLock)")
public Object aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
Object object = null;
MethodSignature joinPointObject = (MethodSignature) pjp.getSignature();
Method method = joinPointObject.getMethod();
XLock xlock = method.getAnnotation(XLock.class);
if(null == xlock) {
return pjp.proceed();
}
Object[] args = pjp.getArgs();
String[] params = discoverer.getParameterNames(method);
EvaluationContext context = new StandardEvaluationContext();
for (int i = 0; i < params.length; i++) {
context.setVariable(params[i],args[i]);
}
String keySpel = xlock.key();
Expression keyExpression = parser.parseExpression(keySpel);
String key = keyExpression.getValue(context,String.class);
RLock lock = redissonClient.getLock(key);
long waitTime = xlock.waitTime();
long leaseTime = xlock.leaseTime();
TimeUnit unit = xlock.unit();
try {
if(lock.tryLock(waitTime,leaseTime,unit)) {
object = pjp.proceed();
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
//锁被持有,并且被当前线程持有
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
return object;
}
}
切面逻辑使用around拦截模式,解析使用了@XLock注解的方法,然后织入加锁解锁逻辑。
5.定义自动注入配置
@Configuration
@ConditionalOnClass({RedissonClient.class, RedissonLock.class})
@EnableConfigurationProperties(RedissonProperties.class)
@Slf4j
public class XLockAutoConfiguration {
private RedissonProperties redissonProperties;
public XLockAutoConfiguration(RedissonProperties redissonProperties) {
this.redissonProperties = redissonProperties;
}
@Bean
@ConditionalOnMissingBean(RedissonClient.class)
public RedissonClient redisson() {
String password = this.redissonProperties.getPassword();
String port = this.redissonProperties.getPort();
int database = this.redissonProperties.getDatabase();
ServerType serverType = ServerType.of(this.redissonProperties.getServerType());
if(null == serverType) {
throw new RuntimeException("server type not support;serverType=" + this.redissonProperties.getServerType());
}
Config config = new Config();
if(ServerType.SINGLE_SERVER.equals(serverType)) {
String address = "redis://" + redissonProperties.getSingleServer().getHost() + ":" + port;
SingleServerConfig singleServerConfig = config.useSingleServer()
.setAddress(address)
.setDatabase(database);
if(null != password) {
singleServerConfig.setPassword(password);
}
} else if(ServerType.CLUSTER_SERVERS.equals(serverType)) {
ClusterServersConfig clusterServersConfig = config.useClusterServers();
String hosts = redissonProperties.getClusterServers().getHosts();
for (String host : hosts.split(",")) {
clusterServersConfig.addNodeAddress("redis://" + host + ":" + port);
}
if(null != password) {
clusterServersConfig.setPassword(password);
}
} else if (ServerType.MASTER_SLAVE_SERVERS.equals(serverType)) {
MasterSlaveServersConfig masterSlaveServersConfig = config.useMasterSlaveServers()
.setDatabase(database);
masterSlaveServersConfig.setMasterAddress("redis://" + redissonProperties.getMasterSlaveServers().getMasterHost() + ":" + port);
for (String slaveHost : redissonProperties.getMasterSlaveServers().getSlaveHosts().split(",")) {
masterSlaveServersConfig.addSlaveAddress("redis://" + slaveHost + ":" + port);
}
if(null != password) {
masterSlaveServersConfig.setPassword(password);
}
} else if (ServerType.REPLICATED_SERVERS.equals(serverType)) {
ReplicatedServersConfig replicatedServersConfig = config.useReplicatedServers()
.setDatabase(database);
for (String host : this.redissonProperties.getReplicatedServers().getHosts().split(",")) {
replicatedServersConfig.addNodeAddress("redis://" + host + ":" + port);
}
if(null != password) {
replicatedServersConfig.setPassword(password);
}
} else if (ServerType.SENTINEL_SERVERS.equals(serverType)) {
SentinelServersConfig sentinelServersConfig = config.useSentinelServers()
.setDatabase(database)
.setMasterName(this.redissonProperties.getSentinelServers().getMasterName());
for (String host : this.redissonProperties.getSentinelServers().getHosts().split(",")) {
sentinelServersConfig.addSentinelAddress("redis://" + host + ":" + port);
}
if(null != password) {
sentinelServersConfig.setPassword(password);
}
}
return Redisson.create(config);
}
@Bean
@ConditionalOnBean(RedissonClient.class)
public XLockInterceptor xLockInterceptor() {
return new XLockInterceptor();
}
}
在用户项目中如果没有定义或者注入RedissonClient,那么通过starter注入RedissonClient,并且支持singleServer、clusterServers、masterSlave、replicatedServer和sentinelServer等模式。
并通过@Bean方式注入暴露锁切面。
6.自动配置
在starter工程的META-INF/spring.factories中定义自动配置:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
lock.starter.config.XLockAutoConfiguration
基于spring的SPI模式在项目启动时自动加载starter的分布式锁相关配置,开启分布式锁能力。
然后打成jar包到仓库,业务项目就可以pom依赖引入,使用分布式锁相关能力了。
三、使用
业务项目中使用分布式锁starter的能力比较简单,引入依赖并定义相关配置即可。
1.引入分布式锁starter
xxx.xxx
xlock-redisson-starter
2.添加配置
在项目中添加分布式锁所需的配置,以singleServer模式为例:
redisson:
redis:
port: 6379
password: xxxxxx
database: 0
serverType: 1 #2,3,4,5
singleServer:
host: 127.0.0.1
3.使用分布式锁
在需要加锁的方法上添加@XLock注解,并填入加锁相关的属性即可.
@XLock(key = "mylock + #uid",waitTime = 5,leaseTime = 10,unit = TimeUnit.SECONDS)
public void doSomething(String uid) {
//do some business...
}
这样就实现了redisson分布式锁的使用。