前两章我们了解了《【实践篇】Redis最强Java客户端(一)之Redisson入门介绍》和《【实践篇】Redis最强Java客户端(二)之Redisson基础概念》本章第三章主要介绍Redisson的七种分布式锁,分别是简单锁、公平锁、可重入锁、红锁、读写锁、信号量和闭锁。下面是每种锁的基本概念、使用方法以及性能和并发控制方面的考虑。
getLock()
方法来获取锁,并使用lock()
和unlock()
方法来加锁和解锁。RLock lock = redisson.getLock("myLock");
lock.lock();
try {
// 执行需要加锁的业务逻辑
} finally {
lock.unlock();
}
getFairLock()
方法来获取公平锁。RLock fairLock = redisson.getFairLock("myFairLock");
fairLock.lock();
try {
// 执行需要加锁的业务逻辑
} finally {
fairLock.unlock();
}
getReentrantLock()
方法来获取可重入锁。RReentrantLock reentrantLock = redisson.getReentrantLock("myReentrantLock");
reentrantLock.lock();
try {
// 执行需要加锁的业务逻辑
} finally {
reentrantLock.unlock();
}
getRedLock()
方法来获取红锁。RLock lock1 = redisson.getLock("lock1");
RLock lock2 = redisson.getLock("lock2");
RLock lock3 = redisson.getLock("lock3");
RedissonRedLock redLock = new RedissonRedLock(lock1, lock2, lock3);
redLock.lock();
try {
// 执行需要加锁的业务逻辑
} finally {
redLock.unlock();
}
getReadWriteLock()
方法来获取读写锁。RReadWriteLock readWriteLock = redisson.getReadWriteLock("myReadWriteLock");
RLock readLock = readWriteLock.readLock();
RLock writeLock = readWriteLock.writeLock();
readLock.lock();
try {
// 执行需要读取的业务逻辑
} finally {
readLock.unlock();
}
writeLock.lock();
try {
// 执行需要写入的业务逻辑
} finally {
writeLock.unlock();
}
getSemaphore()
方法来获取信号量。RSemaphore semaphore = redisson.getSemaphore("mySemaphore");
semaphore.acquire();
try {
// 执行需要并发控制的业务逻辑
} finally {
semaphore.release();
}
getCountDownLatch()
方法来获取闭锁。RCountDownLatch latch = redisson.getCountDownLatch("myLatch");
latch.trySetCount(1);
// 等待其他线程完成任务
latch.await();
// 执行需要等待的业务逻辑
以上是Redisson的七种分布式锁的基本概念、使用方法以及性能和并发控制方面的考虑。这些分布式锁可以在分布式环境中提供可靠的并发控制和资源访问解决方案。
<dependency>
<groupId>org.redissongroupId>
<artifactId>redisson-spring-boot-starterartifactId>
<version>3.15.5version>
dependency>
# Redisson连接信息
spring.redis.host=127.0.0.1
spring.redis.port=6379
# 其他Redisson配置(可选)
spring.redisson.config=classpath:redisson.yaml
singleServerConfig:
address: "redis://127.0.0.1:6379"
package com.example.redissondemo.controller;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class LockController {
@Autowired
RedissonClient redissonClient;
@GetMapping("/lock")
public void testLock() throws InterruptedException {
// 1. 可重入锁(Reentrant Lock)
RLock reentrantLock = redissonClient.getLock("reentrantLock");
reentrantLock.lock();
try {
// critical section / business logic
} finally {
reentrantLock.unlock();
}
// 2. 公平锁(Fair Lock)
RLock fairLock = redissonClient.getFairLock("fairLock");
fairLock.lock();
try {
// critical section / business logic
} finally {
fairLock.unlock();
}
// 3. 读写锁(Read-Write Lock)
RReadWriteLock rwLock = redissonClient.getReadWriteLock("rwLock");
// 写锁
rwLock.writeLock().lock();
try {
// write data
} finally {
rwLock.writeLock().unlock();
}
// 读锁
rwLock.readLock().lock();
try {
// read data
} finally {
rwLock.readLock().unlock();
}
// 4. 联锁(MultiLock)
RLock lock1 = redissonClient.getLock("lock1");
RLock lock2 = redissonClient.getLock("lock2");
RLock lock3 = redissonClient.getLock("lock3");
RLock multiLock = redissonClient.getMultiLock(lock1, lock2, lock3);
multiLock.lock();
try {
// combined business operations on lock1, lock2 and lock3
} finally {
multiLock.unlock();
}
// 5. 红锁(RedLock)
RLock redLock = redissonClient.getRedLock(lock1, lock2, lock3);
redLock.lock();
try {
// combined business operations on lock1, lock2 and lock3
} finally {
redLock.unlock();
}
// 6. 自动过期锁(Auto-expiring Lock)
RLock autoExpireLock = redissonClient.getLock("autoExpireLock");
autoExpireLock.lock();
try {
// critical section / business logic
} finally {
autoExpireLock.forceUnlock();
}
// 7. 闭锁(CountDownLatch Lock)
RCountDownLatch latch = redissonClient.getCountDownLatch("latchLock");
latch.trySetCount(1);
latch.await();
// to release the latch
latch.countDown();
}
}
如果想自定义配置可以这样写,不依赖于Spring boot 注入bean 手动构造一个client 提交给Spring 容器
@java
public class RedissonConfig {
@Value("${redisson.address}")
private String addressUrl;
@Value("${redisson.password}")
private String password;
@Bean
public RedissonClient getRedisson(){
Config config = new Config();
config.useSingleServer().setAddress(addressUrl).setPassword(password);
return Redisson.create(config);
}
}
Redisson官方网站:https://redisson.org/
Redisson GitHub仓库:https://github.com/redisson/redisson
redisson 参考文档 https://redisson.org/documentation.html
https://github.com/wangshuai67/icepip-springboot-action-examples
https://github.com/wangshuai67/Redis-Tutorial-2023