李炎 0455094b6b | ||
---|---|---|
src | ||
.gitignore | ||
README.md | ||
pom.xml |
README.md
redis-lock-spring-boot-starter
1. 简介
基于 Redis 的分布式锁简单实现。
2. 快速开始
Maven 坐标
<dependency>
<groupId>cn.surcode</groupId>
<artifactId>redis-lock-spring-boot-starter</artifactId>
<version>v1.0.0</version>
</dependency>
2.1 配置文件
spring:
redis:
host: 127.0.0.1 # 主机地址
port: 6379 # 端口号
redis-lock:
timeout: 3000 # 锁超时时间,默认值1000(单位:毫秒)
enable: true # 启用锁(默认开启)
项目使用 spring-boot-starter-data-redis
连接配置 redis,关于锁的全局设置目前只有两个属性: enable
和 timeout
。
enable
启用锁,默认开启(此处开启时,注解中可细粒度设置)timeout
锁超时时间,默认值 1000ms(注解中可细粒度设置)
2.2 基本使用
在要加锁的方法上使用 @RedisLock
注解,并指定要加锁数据的属性,锁会根据属性值加锁保证数据的同步操作。
@RedisLock(paramEls = "#user.id")
public void test(User user) {
// ...
}
@RedisLock
注解中的属性:
paramEls
:选择参数列表中的属性,参数是 SpringEL 字符串数组,通过 SpringEL 选取属性给数据加锁;enable
:细粒度设置启用禁用(默认:开启);timeout
:细粒度设置锁的超时时间(单位:毫秒)
2.3 异常
项目中定义了两种异常:
LockFailException
加锁失败异常,说明此时锁被占用;RedisLockException
其它情况的异常(如:方法参数列表为空、paramEls为空、El表达式错误等)
3. 下一步
再说吧...