Redisson中常用的分布式锁
Redisson中常用的分布式锁目录Redisson中常用的分布式锁是什么文档Redisson中提供的锁可重入锁ReentrantLock增强公平锁Fair Lock多锁MultiLock读写锁ReadWriteLock信号量Semaphore闭锁CountDownLatch是什么Redisson 是一个分布式协调Redis Client端,能帮助用户操作分布式环境中的Java的对象。Rediss
Redisson中常用的分布式锁
目录
是什么
Redisson 是一个分布式协调Redis Client端,能帮助用户操作分布式环境中的Java的对象。
Redisson、Jedis、Lettuce 是三个不同的操作 Redis 的客户端,Jedis、Lettuce 的 API 更侧重对 Reids 数据库的 CRUD(增删改查),而 Redisson API 侧重于分布式开发
文档
地址:https://github.com/redisson/redisson/wiki/Table-of-Content(需翻墙)
Redisson中提供的锁
参考资料:https://blog.csdn.net/Howinfun/article/details/121557405
可重入锁ReentrantLock增强
解决了锁的自动续期问题,保证了业务完成前不会自动释放锁。自动续期原理如下
- 锁不释放,一直续锁:加锁后起一条续锁线程,每隔10s续一次锁,除非走到unlock才会续锁线程才会终止,或者业务崩溃,续锁线程直接断掉即不再续锁,
- 如果制定了leasTime,则不会自动续期
公平锁Fair Lock(最常用)
公平锁加锁顺序按请求顺序执行,非公平锁加锁需争抢。
自动续期:每隔过期时间的1/3就会来确认一次锁是否释放,没有释放就续期
RLock lock = redisson.getFairLock("myLock");
// traditional lock method
lock.lock();
// or acquire lock and automatically unlock it after 10 seconds
lock.lock(10, TimeUnit.SECONDS);
// or wait for lock aquisition up to 100 seconds
// and automatically unlock it after 10 seconds
boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
if (res) {
try {
...
} finally {
lock.unlock();
}
}
多锁MultiLock
同时给多个资源加锁,只有所有资源加锁都成功,才算多锁加锁成功
RLock lock1 = redisson1.getLock("lock1");
RLock lock2 = redisson2.getLock("lock2");
RLock lock3 = redisson3.getLock("lock3");
RLock multiLock = anyRedisson.getMultiLock(lock1, lock2, lock3);
// traditional lock method
multiLock.lock();
// or acquire lock and automatically unlock it after 10 seconds
multiLock.lock(10, TimeUnit.SECONDS);
// or wait for lock aquisition up to 100 seconds
// and automatically unlock it after 10 seconds
boolean res = multiLock.tryLock(100, 10, TimeUnit.SECONDS);
if (res) {
try {
...
} finally {
multiLock.unlock();
}
}
读写锁ReadWriteLock
写锁是一个排它锁,读锁是一个共享锁
- 先读 + 后读:并发读,相当于无锁,所有读锁同时加锁成功
- 先写 + 后读:读阻塞,等待写锁释放
- 先读 + 后写:写阻塞,等待读锁释放
- 先写 + 后写:写阻塞,等待写锁释放
RReadWriteLock rwlock = redisson.getReadWriteLock("myLock");
RLock lock = rwlock.readLock();
// or
RLock lock = rwlock.writeLock();
// traditional lock method
lock.lock();
// or acquire lock and automatically unlock it after 10 seconds
lock.lock(10, TimeUnit.SECONDS);
// or wait for lock aquisition up to 100 seconds
// and automatically unlock it after 10 seconds
boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
if (res) {
try {
...
} finally {
lock.unlock();
}
}
信号量Semaphore
相当于是个锁池,可以实现限流、熔断能力
用acquire可以把多余请求阻塞限流
用tryacquire可以把多余请求直接熔断
RSemaphore semaphore = redisson.getSemaphore("mySemaphore");
// acquire single permit
semaphore.acquire();
// or acquire 10 permits
semaphore.acquire(10);
// or try to acquire permit
boolean res = semaphore.tryAcquire();
// or try to acquire permit or wait up to 15 seconds
boolean res = semaphore.tryAcquire(15, TimeUnit.SECONDS);
// or try to acquire 10 permit
boolean res = semaphore.tryAcquire(10);
// or try to acquire 10 permits or wait up to 15 seconds
boolean res = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);
if (res) {
try {
...
} finally {
semaphore.release();
}
}
闭锁CountDownLatch
多个任务(线程)均完成后才会释放锁。要动作的线程设置count,并进行await。等待解锁线程一个个countDown
不能通过锁一个方法里面串行多个待完成方法的方式来做,因为需要给各个方法解耦,所以用多线程执行
RCountDownLatch latch = redisson.getCountDownLatch("myCountDownLatch");
latch.trySetCount(1);
// await for count down
latch.await();
// in other thread or JVM
RCountDownLatch latch = redisson.getCountDownLatch("myCountDownLatch");
latch.countDown();
更多推荐
所有评论(0)