All Projects → TaXueWWL → Redis Distributed Lock

TaXueWWL / Redis Distributed Lock

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

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Redis Distributed Lock

Seconds Kill
基于 Springboot + Redis + Kafka 的秒杀系统,乐观锁 + 缓存 + 限流 + 异步,TPS 从 500 优化到 3000
Stars: ✭ 180 (-17.43%)
Mutual labels:  springboot
Springboot mybatisplus
SpringBoot集成MyBatisPlus
Stars: ✭ 199 (-8.72%)
Mutual labels:  springboot
Xiao Xiao Su
基于Spring Boot+Spring Security+JWT+Vue前后端分离的旺旺小小酥  ( 一口一口又香又脆❤~ )
Stars: ✭ 207 (-5.05%)
Mutual labels:  springboot
Roses
Roses基于Spring Boot 2和Spring Cloud Finchley.RELEASE,致力做更简洁的分布式和服务化解决方案,Roses拥有高效率的开发体验,提供可靠消息最终一致性分布式事务解决方案,提供基于调用链的服务治理,提供可靠的服务异常定位方案(Log + Trace)等等,一个分布式框架不仅需要构建高效稳定的底层开发框架,更需要解决分布式带来的种种挑战!
Stars: ✭ 181 (-16.97%)
Mutual labels:  springboot
Encrypt Body Spring Boot Starter
(停止维护,替代品搜索:https://github.com/search?l=Java&q=encrypt&type=Repositories )SpringBoot控制器统一的响应体加密与请求体解密的注解处理方式,支持MD5/SHA/AES/DES/RSA
Stars: ✭ 198 (-9.17%)
Mutual labels:  springboot
My Blog Layui
layui 版本的 My-Blog : A simple & beautiful blogging system implemented with spring-boot & layui & thymeleaf & mybatis My Blog 是由 SpringBoot + Layui + Mybatis + Thymeleaf 等技术实现的 Java 博客系统,页面美观、功能齐全、部署简单及完善的代码,一定会给使用者无与伦比的体验
Stars: ✭ 204 (-6.42%)
Mutual labels:  springboot
Seckill
基于Spring Boot的高性能秒杀系统
Stars: ✭ 171 (-21.56%)
Mutual labels:  springboot
Spring Cloud Study
SpringCloud源码实战微服务框架(202003更新到SpringCloud的Greenwich版本,即基于SpringBoot2.1.7版本,以及新增SpringCloud Alibaba从入门到精通教程),涉及内容:配置中心,注册中心,服务提供者,服务消费者,熔断器,配置服务,API Gateway,API网关限流ratelimit,服务链路追踪zipkin,hystrix监控,监控聚合中心turbine等
Stars: ✭ 215 (-1.38%)
Mutual labels:  springboot
Demo Project
存放学习过程中的demo项目,别光fork,顺便点下⭐哦
Stars: ✭ 198 (-9.17%)
Mutual labels:  springboot
Javacollection
Java开源项目之「自学编程之路」:学习指南+面试指南+资源分享+技术文章
Stars: ✭ 2,957 (+1256.42%)
Mutual labels:  springboot
Hospital
医院预约挂号微信小程序(Spring Boot、Vue、Uni-app)
Stars: ✭ 182 (-16.51%)
Mutual labels:  springboot
Spring Boot Learning
springboot系列教程,基于1.5.x版本
Stars: ✭ 197 (-9.63%)
Mutual labels:  springboot
Gopush
分布式消息推送服务,可以用于客服、推送、聊天等诸多系统的 核心组件服务!
Stars: ✭ 204 (-6.42%)
Mutual labels:  springboot
Boot Actuator
基于SpringBoot2.0 实现的jvm远程监工图形化工具,可以同时监控多个web应用,支持远程监控
Stars: ✭ 180 (-17.43%)
Mutual labels:  springboot
Cloudopt Next
A simple and modern Java and Kotlin vert.x web framework
Stars: ✭ 211 (-3.21%)
Mutual labels:  springboot
Spring Boot Plus
🔥 Spring-Boot-Plus is a easy-to-use, high-speed, high-efficient,feature-rich, open source spring boot scaffolding. 🚀
Stars: ✭ 2,198 (+908.26%)
Mutual labels:  springboot
Learningsummary
涵盖大部分Java进阶需要掌握的知识,包括【微服务】【中间件】【缓存】【数据库优化】【搜索引擎】【分布式】等等,欢迎Star~
Stars: ✭ 201 (-7.8%)
Mutual labels:  springboot
Bcmall
以教学为目的的电商系统。包含ToB复杂业务、互联网高并发业务、缓存应用;DDD、微服务指导。模型驱动、数据驱动。了解大型服务进化路线,编码技巧、学习Linux,性能调优。Docker/k8s助力、监控、日志收集、中间件学习。前端技术、后端实践等。主要技术:SpringBoot+JPA+Mybatis-plus+Antd+Vue3。
Stars: ✭ 188 (-13.76%)
Mutual labels:  springboot
Springboot Examples
spring boot 实践系列
Stars: ✭ 216 (-0.92%)
Mutual labels:  springboot
Jeecg Cloud
基于Spring Boot 2.1.3、 Spring Cloud Greenwich.SR3 & Alibaba、 Shiro、JWT的 JAVA快速开发平台(Jeecg Boot的微服务版本,功能和底层代码,两边保持同步)
Stars: ✭ 205 (-5.96%)
Mutual labels:  springboot

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].