1 Star 0 Fork 20

秋天的童话 / spring-redis-distributed-lock

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

spring-redis-distributed-lock

介绍

spring-redis-distributed-lock 为redisLock项目扩展而来 https://gitee.com/billion/redisLock

一个应用于springboot项目的,基于redis的分布式锁 可用于多节点项目防重复业务调用

通过方法注解开启 简单、易用

可以通过简单的注解配置达到同机器或不同机器竞争锁

使用步骤

  1. 在SpringBootApplication上添加@EnableLock注解引入锁。
  2. 在需要开启分布式锁的方法上添加@Lock注解。
  3. 分布式锁支持粒度为方法参数的级别,通过@LockKey注解到方法参数上,被注解的参数会添加作为redis key的后缀。 具有相同redis key的方法调用会竞争同一把锁。一个方法可以添加多个@LockKey。
@EnableLock
public class XXServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(XXServiceApplication.class, args);
    }
}
@Lock(policy = LockPolicy.Lock, featureParams = MacAddressFeatureParam.class)
public boolean service(@LockKey int id, @LockKey String name) {
    //业务代码
}

@Lock参数介绍

名称 类型 默认值 备注
policy LockPolicy Lock 锁策略 默认为Lock:获取锁失败立即返回;SpinLock:自旋锁,达到timeout后返回;BlockLock:持续自旋直到获取锁返回
keyPrex String 类名+方法名 redis锁的key前缀 如果为空,则默认为类名+方法名
featureParams LockFeatureParam 特征字参数方法集,可以增加不在业务方法中的特征参数用于区分锁,防止竞争锁 可实现LockFeatureParam 扩展自定义非业务参数
lockTime int 60 锁持续时间,将设定在redis中用于值存在的时间 单位s
timetOut long 300 SpinLock超时时间 单位ms

代码样例

直接使用
@Lock
public boolean service() {
    //业务代码
}
参数粒度
@Lock
public boolean service(@LockKey int id) {
    //业务代码
}
同机器不同进程竞争,注入mac地址作为非业务参数特征
@Lock(featureParams = MacAddressFeatureParam.class)
public boolean service(@LockKey int id) {
    //业务代码
}
自旋锁,超时放弃 默认300 单位ms
@Lock(policy = LockPolicy.SpinLock, timetOut = 500L)
public boolean service(@LockKey int id) {
    //业务代码
}
阻塞锁 持续自旋直到成功
@Lock(policy = LockPolicy.BlockLock)
public boolean service(@LockKey int id) {
    //业务代码
}
MIT License Copyright (c) 2019 Stan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

springboot 分布式锁,可直接注解使用。基于redis,支持多节点;支持阻塞,自旋;支持多种特征参数,包括但不限于同机器不同进程竞争。 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/skygf163/spring-redis-distributed-lock.git
git@gitee.com:skygf163/spring-redis-distributed-lock.git
skygf163
spring-redis-distributed-lock
spring-redis-distributed-lock
master

搜索帮助