# DistributedLock **Repository Path**: heycm/distributed-lock ## Basic Information - **Project Name**: DistributedLock - **Description**: 对 Redisson 进行封装 SpringBoot Starter 包,引入依赖即可直接使用注解对接口上锁解锁 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2023-01-28 - **Last Updated**: 2023-05-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # DistributedLock #### 介绍 对 Redisson 进行封装 SpringBoot Starter 包,引入依赖即可直接使用注解对接口上锁解锁 #### 安装教程 ```bash mvn clear && mvn install ``` #### 使用教程     以 SpringBoot 3.0.2 为例 1. 引入依赖 ```xml org.springframework.boot spring-boot-starter-data-redis online.heycm online-heycm-lock 1.0 ``` 2. 配置(支持两种模式) ```properties # 单机模式 spring.data.redis.host=127.0.0.1 spring.data.redis.port=6379 spring.data.redis.database=0 spring.data.redis.username= spring.data.redis.password= # 哨兵模式 spring.data.redis.sentinel.master= spring.data.redis.sentinel.nodes= spring.data.redis.sentinel.username= spring.data.redis.sentinel.password= ``` 3. 注解启动 ```java @SpringBootApplication @EnableDistributedLock public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` #### 使用说明 1. 注解说明 > @DistributedLock         作用于方法上,执行线程进入该方法前,需要申请并获得锁,后锁定该方法,直到方法执行完成或锁超时后释放。         属性说明:             `prefix`             锁的key值前缀             `leaseTime`        锁超时时长,默认3000ms             `waitTime`         申请锁等待超时时长,默认300ms > @DistributedLockKey         作用于方法参数或方法参数属性上,标识该参数值或属性值为锁的key值,一般以竞争资源ID为标识。 2. 方式一 ```java @DistributedLock(prefix = "lock:order:") @GetMapping("/testLock/{orderId}") public String testLock(@DistributedLockKey @PathVariable Long orderId) throws InterruptedException { log.info("order: {}", orderId); Thread.sleep(2000); return "ok"; } ``` 3. 方式二 ```java @Data public class LockDO { @DistributedLockKey private Long orderId; private String other; } @DistributedLock(prefix = "lock:order:") @GetMapping("/testLock") public String testLock(LockDO lockDO) throws InterruptedException { log.info("order: {}", lockDO.getOrderId()); Thread.sleep(2000); return "ok"; } ``` #### 流程图示 ![流程图示](distributed-lock.jpg)