#redis redisson锁(信号量)使用
安装依赖
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.16.8</version>
</dependency>
springboot配置
spring:
redis:
redisson:
config:
singleServerConfig:
# 连接空闲超时,单位:毫秒
idleConnectionTimeout: 100000
# 连接超时,单位:毫秒
connectTimeout: 10000
# 命令等待超时,单位:毫秒
timeout: 3000
# 命令失败重试次数
retryAttempts: 3
# 命令重试发送时间间隔,单位:毫秒
retryInterval: 1500
# 密码
password: null
# 单个连接最大订阅数量
subscriptionsPerConnection: 5
# 客户端名称
clientName: null
# 节点地址
address: redis://192.168.1.198:6379
# 发布和订阅连接的最小空闲连接数
subscriptionConnectionMinimumIdleSize: 1
# 发布和订阅连接池大小
subscriptionConnectionPoolSize: 50
# 最小空闲连接数
connectionMinimumIdleSize: 32
# 连接池大小
connectionPoolSize: 64
# redis数据库编号
database: 0
# DNS监测时间间隔,单位:毫秒
dnsMonitoringInterval: 5000
# 线程池数量
threads: 0
# Netty线程池数量
nettyThreads: 0
# 编码
codec:
class: "org.redisson.codec.JsonJacksonCodec"
# 传输模式
transportMode: "NIO"
# 配置看门狗的默认超时时间为30s,这里改为10s
lockWatchdogTimeout: 10000
定义信号量名称
public interface SemaphoreConstant {
/**
* 名称
*/
String COLLABORATE = "collaborate";
}
设置可过期信号量初始值
package com.jz.pjnj.config;
import com.jz.pjnj.constant.SemaphoreConstant;
import org.redisson.api.RPermitExpirableSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RedissonClientConfig {
@Autowired
private RedissonClient redissonClient;
@Bean
public void semaphoreClient(){
RPermitExpirableSemaphore totalSemaphore = redissonClient.getPermitExpirableSemaphore(SemaphoreConstant.COLLABORATE);
totalSemaphore.trySetPermits(5);
}
}
信号量模式 加锁
/**
* 信号量模式 加锁
* @return
*/
public String semaphoreLock(){
try{
RPermitExpirableSemaphore accountSemaphore = redissonClient
.getPermitExpirableSemaphore(SemaphoreConstant.COLLABORATE);
String accountPermitId = accountSemaphore.tryAcquire(0, 40, TimeUnit.SECONDS);
System.out.println(accountPermitId);
if(null == accountPermitId){
return "false";
}else{
return accountPermitId;
}
}catch (InterruptedException e){
return "false";
}
}
信号量模式 解锁
/**
* 信号量模式 解锁
* @return
*/
public Boolean semaphoreUnLock(String permitKey){
RPermitExpirableSemaphore semaphore = redissonClient
.getPermitExpirableSemaphore(SemaphoreConstant.COLLABORATE);
semaphore.release(permitKey);
return true;
}
测试加解锁
public void test(){
String locked = redisLockUtils.semaphoreLock();
if (!locked.equals("false")) {
return R.ok("加锁成功");
}
return R.ok("加锁失败");
}
完整工具类
@Component
public class RedisLockUtils {
@Autowired
private RedissonClient redissonClient;
/**
* 获取分布式锁
*
* @param lockKey 锁的键
* @param expireTime 锁的过期时间
* @param waitTimeout 等待获取锁的超时时间
* @return 是否成功获取锁
*/
public boolean lock(String lockKey, long expireTime, long waitTimeout) {
RLock lock = redissonClient.getLock(lockKey);
try {
return lock.tryLock(waitTimeout, expireTime, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
return false;
}
}
/**
* 释放分布式锁
*
* @param lockKey 锁的键
*/
public void unlock(String lockKey) {
RLock lock = redissonClient.getLock(lockKey);
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
/**
* 信号量模式 加锁
* @return
*/
public String semaphoreLock(){
try{
RPermitExpirableSemaphore accountSemaphore = redissonClient
.getPermitExpirableSemaphore(SemaphoreConstant.COLLABORATE);
String accountPermitId = accountSemaphore.tryAcquire(0, 40, TimeUnit.SECONDS);
System.out.println(accountPermitId);
if(null == accountPermitId){
return "false";
}else{
return accountPermitId;
}
}catch (InterruptedException e){
return "false";
}
}
/**
* 信号量模式 解锁
* @return
*/
public Boolean semaphoreUnLock(String permitKey){
RPermitExpirableSemaphore semaphore = redissonClient
.getPermitExpirableSemaphore(SemaphoreConstant.COLLABORATE);
semaphore.release(permitKey);
return true;
}
}