• Stars
    star
    312
  • Rank 134,133 (Top 3 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 6 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

redis分布式锁工具包,提供纯Java方式调用,支持传统Spring工程, 为spring boot应用提供了starter,更方便快捷的调用。

redis-distributed-lock

redis分布式锁工具包,提供纯Java方式调用,支持传统Spring工程, 为spring boot应用提供了starter,更方便快捷的调用。

项目结构

redis-distributed-lock-core              原生redis分布式锁实现,支持注解,不推荐项目中使用,仅供学习使用

redis-distributed-lock-demo-spring   redis-distributed-lock-core 调用实例,仅供学习

redis-distributed-lock-starter             基于Redisson的分布式锁spring starter实现,可用于实际项目中

redis-distributed-lock-starter-demo   redis-distributed-lock-starter调用实例

文档

springboot应用(直接编程方式)

  1. 配置文件application.properties中引入

     redisson.lock.server.address=127.0.0.1:6379
     redisson.lock.server.type=standalone
    
  2. 针对springboot应用只需要引入依赖

     <!--分布式锁redisson版本-->
     <dependency>
     	<groupId>com.snowalker</groupId>
     	<artifactId>redis-distributed-lock-starter</artifactId>
     	<version>1.0.0</version>
     </dependency>
    
  3. 直接编程方式调用如下,在需要加锁的定时任务中,注入 RedissonLock 实体,即可进行加锁、解锁等操作。
    锁自动释放时间默认为10秒,这个时间需要你根据自己的业务执行时间自行指定。

     @Autowired
     RedissonLock redissonLock;
     
     @Scheduled(cron = "${redis.lock.cron}")
     public void execute() throws InterruptedException {
         if (redissonLock.lock("redisson", 10)) {
             LOGGER.info("[ExecutorRedisson]--执行定时任务开始,休眠三秒");
             Thread.sleep(3000);
             System.out.println("=======================业务逻辑=============================");
             LOGGER.info("[ExecutorRedisson]--执行定时任务结束,休眠三秒");
             redissonLock.release("redisson");
         } else {
             LOGGER.info("[ExecutorRedisson]获取锁失败");
         }
     }
    
  4. 你可以改变测试demo的端口,起多个查看日志,能够看到同一时刻只有一个实例获取锁成功并执行业务逻辑

     2018-07-10 23:00:12.810 |-INFO  [main] org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer [198] -| Tomcat started on port(s): 8888 (http)
     2018-07-10 23:00:12.814 |-INFO  [main] com.snowalker.RedisDistributedLockStarterDemoApplication [57] -| Started RedisDistributedLockStarterDemoApplication in 2.684 seconds (JVM running for 3.038)
     2018-07-10 23:00:15.033 |-INFO  [pool-3-thread-1] com.snowalker.lock.redisson.RedissonLock [35] -| 获取Redisson分布式锁[成功],lockName=redisson
     2018-07-10 23:00:15.034 |-INFO  [pool-3-thread-1] com.snowalker.executor.ExecutorRedissonNormal [27] -| [ExecutorRedisson]--执行定时任务开始,休眠三秒
     =======================业务逻辑=============================
     2018-07-10 23:00:18.035 |-INFO  [pool-3-thread-1] com.snowalker.executor.ExecutorRedissonNormal [30] -| [ExecutorRedisson]--执行定时任务结束,休眠三秒
     2018-07-10 23:00:20.005 |-INFO  [pool-3-thread-1] com.snowalker.lock.redisson.RedissonLock [35] -| 获取Redisson分布式锁[成功],lockName=redisson
     2018-07-10 23:00:20.006 |-INFO  [pool-3-thread-1] com.snowalker.executor.ExecutorRedissonNormal [27] -| [ExecutorRedisson]--执行定时任务开始,休眠三秒
     =======================业务逻辑============================= 
    

springboot应用(注解方式)

  1. 前提条件同样是要引入依赖

     <!--分布式锁redisson版本-->
     <dependency>
     	<groupId>com.snowalker</groupId>
     	<artifactId>redis-distributed-lock-starter</artifactId>
     	<version>1.0.0</version>
     </dependency>
    
  2. 注解方式调用如下,在需要加锁的定时任务的执行方法头部,添加 @DistributedLock(value = "redis-lock", expireSeconds = 11) 即可进行加锁、解锁等操作。
    锁自动释放时间默认为10秒,这个时间需要你根据自己的业务执行时间自行指定。
    我这里以spring schedule定时任务为例,用其他的定时任务同理,只需要添加注解。

     @Scheduled(cron = "${redis.lock.cron}")
     @DistributedLock(value = "redis-lock", expireSeconds = 11)
     public void execute() throws InterruptedException {
         LOGGER.info("[ExecutorRedisson]--执行定时任务开始,休眠三秒");
         Thread.sleep(3000);
         System.out.println("=======================业务逻辑=============================");
         LOGGER.info("[ExecutorRedisson]--执行定时任务结束,休眠三秒");
     }
    
  3. 你可以改变测试demo的端口,起多个查看日志,能够看到同一时刻只有一个实例获取锁成功并执行业务逻辑

     2018-07-11 09:48:06.330 |-INFO  [main] com.snowalker.RedisDistributedLockStarterDemoApplication [57] -| Started RedisDistributedLockStarterDemoApplication in 3.901 seconds (JVM running for 4.356)
     2018-07-11 09:48:10.006 |-INFO  [pool-3-thread-1] com.snowalker.lock.redisson.annotation.DistributedLockHandler [32] -| [开始]执行RedisLock环绕通知,获取Redis分布式锁开始
     2018-07-11 09:48:10.622 |-INFO  [pool-3-thread-1] com.snowalker.lock.redisson.RedissonLock [35] -| 获取Redisson分布式锁[成功],lockName=redis-lock
     2018-07-11 09:48:10.622 |-INFO  [pool-3-thread-1] com.snowalker.lock.redisson.annotation.DistributedLockHandler [39] -| 获取Redis分布式锁[成功],加锁完成,开始执行业务逻辑...
     2018-07-11 09:48:10.625 |-INFO  [pool-3-thread-1] com.snowalker.executor.ExecutorRedissonAnnotation [22] -| [ExecutorRedisson]--执行定时任务开始,休眠三秒
     =======================业务逻辑=============================
     2018-07-11 09:48:13.625 |-INFO  [pool-3-thread-1] com.snowalker.executor.ExecutorRedissonAnnotation [25] -| [ExecutorRedisson]--执行定时任务结束,休眠三秒
     2018-07-11 09:48:13.627 |-INFO  [pool-3-thread-1] com.snowalker.lock.redisson.annotation.DistributedLockHandler [46] -| 释放Redis分布式锁[成功],解锁完成,结束业务逻辑...
     2018-07-11 09:48:13.628 |-INFO  [pool-3-thread-1] com.snowalker.lock.redisson.annotation.DistributedLockHandler [50] -| [结束]执行RedisLock环绕通知
    

application.properties配置方式(yaml同理)

redisson分布式锁配置--单机

    redisson.lock.server.address=127.0.0.1:6379
    redisson.lock.server.type=standalone
    redisson.lock.server.password=
    redisson.lock.server.database=1

redisson分布式锁配置--哨兵

redisson.lock.server.address 格式为: sentinel.conf配置里的sentinel别名,sentinel1节点的服务IP和端口,sentinel2节点的服务IP和端口,sentinel3节点的服务IP和端口
比如sentinel.conf里配置为sentinel monitor my-sentinel-name 127.0.0.1 6379 2,那么这里就配置my-sentinel-name

    redisson.server.address=my-sentinel-name,127.0.0.1:26379,127.0.0.1:26389,127.0.0.1:26399
    redisson.server.type=sentinel
    redisson.lock.server.password=
    redisson.lock.server.database=1

redisson分布式锁配置--集群方式

cluster方式至少6个节点(3主3从,3主做sharding,3从用来保证主宕机后可以高可用)
地址格式为: 127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384

    redisson.server.address=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384
    redisson.server.type=cluster
    redisson.lock.server.password=
    redisson.lock.server.database=1

redisson分布式锁配置--主从

地址格式为主节点,子节点,子节点
比如:127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381
代表主节点:127.0.0.1:6379,从节点127.0.0.1:6380,127.0.0.1:6381

    redisson.server.address=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381
    redisson.server.type=masterslave
    redisson.lock.server.password=
    redisson.lock.server.database=1

参考资料

自己写分布式锁-基于redission

More Repositories

1

seckill-rocketmq

seckill-rocketmq-基于RocketMQ-电商高并发场景实战
Java
195
star
2

shield-ratelimter

基于Redis的分布式限流组件,注解支持
Java
141
star
3

snowalker-shardingjdbc-demo

shardingjdbc3.x 整合,实现自定义主键生成,自定义分库分表策略,可扩展,可读性更好
Java
91
star
4

order-charge-notify

模拟电商下单->订单处理->订单结果通知的完整流程,基于RocketMQ进行实现
Java
63
star
5

lite-tracer

lite-tracer轻量级链路追踪系统,google-dapper个人实现。仅供学习研究
Java
39
star
6

shield-dubbo-tracer

基于dubbo2.6.4的Dubbo TraceId的设置/获取/传递工具包
Java
29
star
7

sheild-conf-single

sheild-conf配置中心单机版本,提供一站式配置服务及管理
CSS
20
star
8

shieldTXC

A Message based Distributed Transaction Framework
Java
19
star
9

shield-job

基于队列的任务调度及分发中间件,based on RocketMQ
Java
15
star
10

shield-rocketmq-client-spring

rocketmq client based on Spring framework
Java
9
star
11

shield-conf

shield,基于Dubbo-RPC的分布式配置服务中心
Java
8
star
12

todo-board

极客时间代码之丑作业题
Java
4
star
13

springboot-global-exception

springboot global exception handling
Java
3
star
14

Jobhunters

Jobhunters is a website to make students find jobs and let company find nice workers
Java
2
star
15

rocketmq-lib-enterprise

阿里云消息队列MQ封装,商业版RocketMQ
Java
2
star
16

SunOnlineWebsiteDevelopmentVersion

SunOnline Website Development Version
Java
2
star
17

e-point

e-point教师辅助点名
Java
2
star
18

Eclipse-plugin-Desktop-genius

eclipse桌面精灵插件
Java
2
star
19

grpc-demo

基于gRPC的报价服务案例,添加服务注册发现(基于Nacos)、双向流等场景。
Java
2
star
20

StudentManagementSystem

A system that can manage students' grades and message themselves
Java
2
star
21

lifeSharing-blog

micro blog with dubbox
CSS
2
star
22

Notepad_Java_version

notepad using Java,seems like OS's appearance
Java
1
star
23

taxuewwl.github.io

惟精惟一,允执厥中
HTML
1
star
24

question-board-kit

个人问题统计查询应用
JavaScript
1
star
25

Jexplorer

A explorer written by Java
Java
1
star