一、通过Redis分布式锁双重防死锁
1、Redis分布式锁命令:
setnx: 只有不存在的时候才set
getset: 先get再set
expire: 设置key的有效期
del: 删除
2、Redis分布式锁优化版流程图

代码实现:
1 | (cron="0 */1 * * * ?") |
二、Redisson框架实现分布式锁
1、Redisson介绍
- Redisson是架设在Redis基础上的一个Java驻内存数据网络(In-Memort-Data-Grid)
- Redisson在基于NIO的Netty框架上,充分的利用了Redis键值数据库提供的一系列优势
- 在Java实用工具包中常用接口的基础上,为使用者提供了一系列具有分布式特性的常用工具类
- 使得原本为协调单机多线程并发程序的工具包获得了协调分布式多机多线程并发系统的能力,大大降低了设计和研发大规模分布式系统的难度。
- 同时结合各富特色的分布式服务,更进一步简化了分布式环境中程序相互之间的协作
- Redisson官网:https://redisson.org Redisson Github: https://github.com/redisson/ Redisson Wiki:https://github.com/redisson/redisson/wiki
2、引入maven依赖
1 | <dependency> |
3、Redisson工具类封装:
1 | package com.mmall.common; |
3、业务代码中的使用
1 | // @Scheduled(cron="0 */1 * * * ?") |
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !