基于redisson框架,以注解的形式实现springboot项目的分布式锁
使用方式
@RedissonLock(lockName = CacheConst.BATCH_MESSAGE_PUSH_LOCK, key = "#userId", expire = 10, autoPrefix = 2)
package com.meta.service.api.common.annotion;
import java.lang.annotation.*;
import java.util.concurrent.TimeUnit;
/**
* 使用redis构建分布式锁
* @RedisLock(lockName = "deductionStock", key = "#deductionStockDto.purchaseId")
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RedissonLock {
//redis锁的名称
String lockName() default "";
//redis锁 key支持spring spel表达式
String key() default "";
//锁前缀 1:默认加前缀 2:key()为准
int autoPrefix() default 1;
//锁过期时间 默认时间为秒单位
int expire() default 120;
//超时时间单位为秒
TimeUnit timeUnit() default Time