官方配置文档:https://github.com/redisson/redisson/tree/master/redisson-spring-boot-starter
引入依赖
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.17.7</version>
</dependency>
API 使用
官方使用文档:8. 分布式锁和同步器 · redisson/redisson Wiki · GitHub
可重入锁(Reentrant Lock)常用
可重入锁指的是可重复可递归调用的锁
@Autowired
private RedissonClient redissonClient;
@ResponseBody
@RequestMapping("/hello")
public String hello(Model model) {
RLock lock = redissonClient.getLock("my-lock");
lock.lock();
try{
System.out.println("加锁成功,执行业务..."+Thread.currentThread().getId());
Thread.sleep(3000);
}catch (Exception e){
} finally {
System.out.println("锁被释放了");
lock.unlock();
}
return "hello";
}
公平锁(Fair Lock)
保证了当多个Redisson客户端线程同时请求加锁时,优先分配给先发出请求的线程
@Autowired
private RedissonClient redissonClient;
@ResponseBody
@RequestMapping("/hello")
public String hello(Model model) {
RLock fairLock = redissonClient.getFairLock("anyLock");
fairLock.lock();
try{
System.out.println("公平锁加锁成功,执行业务..."+Thread.currentThread().getId());
Thread.sleep(3000);
}catch (Exception e){
} finally {
System.out.println("公平锁被释放了");
fairLock.unlock();
}
return "hello";
}
读写锁(ReadWriteLock)
保证每次读到都是最新,读锁和写锁是互斥,读锁之间没有互斥
读写锁需要保证锁key值一样才生效
@ResponseBody
@RequestMapping("/read")
public String read(Model model) {
RReadWriteLock rwlock = redissonClient.getReadWriteLock("anyRWLock");
rwlock.readLock().lock();
try{
System.out.println("读锁加锁成功,执行业务..."+Thread.currentThread().getId());
}catch (Exception e){
} finally {
System.out.println("读锁锁被释放了");
rwlock.readLock().unlock();
}
return "read";
}
@ResponseBody
@RequestMapping("/write")
public String write(Model model) {
RReadWriteLock rwlock = redissonClient.getReadWriteLock("anyRWLock");
rwlock.writeLock().lock();
try{
System.out.println("写锁加锁成功,执行业务..."+Thread.currentThread().getId());
Thread.sleep(10000);
}catch (Exception e){
} finally {
System.out.println("写锁被释放了");
rwlock.writeLock().unlock();
}
return "write";
}
信号量(Semaphore)
计数信号量时一种锁,它可以让用户限制一项资源最多能够同时被多少个进程访问。
计数信号量和其他锁的区别:客户端获取锁失败时,客户端会选择等待;获取信号量失败,通常直接退出,并向用户提示“资源繁忙”,由用户决定下一步如何处理。
@ResponseBody
@RequestMapping("/park")
public String park(Model model) throws InterruptedException {
RSemaphore semaphore = redissonClient.getSemaphore("semaphore");
boolean b = semaphore.tryAcquire();//非阻塞
//semaphore.acquire(); //阻塞
return "ok=>"+b;
}
@ResponseBody
@RequestMapping("/go")
public String go(Model model) throws InterruptedException {
RSemaphore semaphore = redissonClient.getSemaphore("semaphore");
semaphore.release();
return "ok";
}
闭锁(CountDownLatch)
等待其他条件满足,才释放锁
@ResponseBody
@RequestMapping("/lockDoor")
public String lockDoor() throws InterruptedException {
RCountDownLatch door = redissonClient.getCountDownLatch("door");
door.trySetCount(5);
door.await();//等待闭锁都完成
return "放假了...";
}
@ResponseBody
@RequestMapping("/gogo/{id}")
public String gogo(@PathVariable("id") Long id) throws InterruptedException {
RCountDownLatch door = redissonClient.getCountDownLatch("door");
door.countDown();//计数-1
return id + "班的人都走了...";
}
锁粒度命令约定
锁粒度命名越细越好
product-11-lock 11号商品
catalog-lock