基于 Redis 的分布式锁实现。
Go to file
李炎 0455094b6b 修改pom和README 2021-12-10 11:08:30 +08:00
src init commit 2021-12-09 20:54:40 +08:00
.gitignore init commit 2021-12-09 20:54:40 +08:00
README.md 修改pom和README 2021-12-10 11:08:30 +08:00
pom.xml 修改pom和README 2021-12-10 11:08:30 +08:00

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关于锁的全局设置目前只有两个属性 enabletimeout

  • 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. 下一步

再说吧...