当前位置:网站首页>分布式锁开发
分布式锁开发
2022-07-30 05:56:00 【weixin_45031570】
Redisson分布式锁
搭建
maven
<!-- https://mvnrepository.com/artifact/org.redisson/redisson -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.12.0</version>
</dependency>
单实例模式搭建
github地址
https://github.com/redisson/redisson/wiki/2.-Configuration
配置类,加入ioc容器
// connects to 127.0.0.1:6379 by default
RedissonClient redisson = Redisson.create();
Config config = new Config();
config.useSingleServer().setAddress("redis://myredisserver:6379");
RedissonClient redisson = Redisson.create(config);
分布式锁文档地址
https://github.com/redisson/redisson/wiki/8.-%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81%E5%92%8C%E5%90%8C%E6%AD%A5%E5%99%A8
可重入锁(Reentrant Lock)
无需处理掉电或者程序问题导致锁失效问题
RLock lock = redisson.getLock("anyLock");
// 最常见的使用方法
lock.lock();//阻塞式等待,默认30s释放,无需处理掉电或者程序问题导致锁失效问题
// 加锁以后10秒钟自动解锁
// 无需调用unlock方法手动解锁
lock.lock(10, TimeUnit.SECONDS);
// 尝试加锁,最多等待100秒,上锁以后10秒自动解锁
boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
if (res) {
try {
...
} finally {
lock.unlock();
}
}
公平锁(Fair Lock)
FIFO算法执行锁,依次排队等待锁
联锁(MultiLock)
红锁(RedLock)
读写锁(ReadWriteLock)
读业务用读锁,写业务用写锁,2者一起用。
读读业务是共享锁
其余业务都是排他锁(阻塞等待)
信号量(Semaphore)
可用业务比如服务限流
可过期性信号量(PermitExpirableSemaphore)
闭锁(CountDownLatch)
springCache开发
边栏推荐
猜你喜欢
Pioneer in Distributed Systems - Leslie Lambert
入选“十大硬核科技”,详解可信密态计算(TECC)技术点
From catching up to surpassing, domestic software shows its talents
《心智社会》—马文·明斯基
The calculation and source code of the straight line intersecting the space plane
首届人工智能安全大赛正式启动
AI元学习引入神经科学,医疗效果有望精准提升
The terminal connection tools, rolling Xshell
Rodrigues: vector representation of rotation matrices
Go uses the mencached cache
随机推荐
学生成绩管理系统(C语言)
go : go-redis 基础操作
When does MySQL use table locks and when does it use row locks?
云服务器零基础部署网站(保姆级教程)
The Geometric Meaning of Vector Cross Product and the Calculation of Modulus
从 Google 离职,前Go 语言负责人跳槽小公司
Rodrigues: vector representation of rotation matrices
识别“数据陷阱”,发现数据的可疑之处
go : 使用gorm创建数据库记录
MySQL off-topic [ORM thought analysis]
Process and Scheduled Task Management
The calculation proof of the intersection of the space line and the plane and its source code
手机端滚动至页面指定位置
新人误删数据,组长巧用MySQL主从复制延迟挽回损失
MySQL基础篇【命名规范】
golang : Zap日志整合
Pioneer in Distributed Systems - Leslie Lambert
Universal js time date format conversion
专访蚂蚁:这群技术排头兵,如何做好底层开发这件事?| 卓越技术团队访谈录
MYSQL 主从恢复锁表后, 处理SQL 线程锁解决.