导入redisson
maven
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.10.2</version>
</dependency>
gradle
compile group: 'org.redisson', name: 'redisson', version: '3.12.2'
application
spring:
application:
redis:
host: 127.0.0.1
port: 6379
database: 16
password: 123
lettuce:
pool:
#连接池最大连接数(使用负值表示没有限制)
max-active: 8
#连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
# 连接池中的最大空闲连接
max-idle: 8
# 连接池中的最小空闲连接
min-idle: 0
timeout: 10000
config
@Configuration
public class RedissonConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private Integer port;
@Value("${spring.redis.database}")
private Integer database;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.timeout}")
private Integer timeout;
@Value("${spring.redis.lettuce.pool.max-active}")
private Integer maxTotal;
@Value("${spring.redis.lettuce.pool.max-wait}")
private Integer maxWait;
@Value("${spring.redis.lettuce.pool.max-idle}")
private Integer maxIdle;
@Value("${spring.redis.lettuce.pool.min-idle}")
private Integer minIdle;
@Bean
RedissonClient redissonSingle() {
Config config = new Config();
SingleServerConfig serverConfig = config.useSingleServer()
.setAddress("redis://" + host + ":" + port)
.setTimeout(timeout)
.setDatabase(database)
.setConnectionPoolSize(maxTotal)
.setConnectionMinimumIdleSize(minIdle);
if (StringUtils.isNotBlank(password)) {
serverConfig.setPassword(password);
}
return Redisson.create(config);
}
}
service
public interface RedissonLocker {
RLock lock(String lockKey);
RLock lock(String lockKey, int timeout);
RLock lock(String lockKey, TimeUnit unit, int timeout);
void unlock(String lockKey);
void unlock(RLock lock);
boolean tryLock(String lockKey, int waitTime, int leaseTime);
boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime);
Impl
@Slf4j
@Component
public class RedissonLockerImple implements RedissonLocker {
@Autowired
private RedissonClient redissonClient;
private static final int expireTime = 5;
private static final TimeUnit timeType = TimeUnit.SECONDS;
@Override
public RLock lock(String lockKey) {
RLock lock = redissonClient.getLock(lockKey);
lock.lock(expireTime, timeType);
return lock;
}
@Override
public RLock lock(String lockKey, int timeout) {
RLock lock = redissonClient.getLock(lockKey);
lock.lock(timeout, timeType);
return lock;
}
@Override
public RLock lock(String lockKey, TimeUnit unit, int timeout) {
RLock lock = redissonClient.getLock(lockKey);
lock.lock(timeout, unit);
return lock;
}
@Override
public boolean tryLock(String lockKey, int waitTime, int leaseTime) {
RLock lock = redissonClient.getLock(lockKey);
try {
return lock.tryLock(waitTime, leaseTime, timeType);
} catch (Exception e) {
log.error("分布式锁尝试获取失败tryLock:", ExceptionUtils.getStackTrace(e));
return false;
}
}
@Override
public boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime) {
RLock lock = redissonClient.getLock(lockKey);
try {
return lock.tryLock(waitTime, leaseTime, unit);
} catch (Exception e) {
log.error("分布式锁尝试获取失败tryLock:{}", ExceptionUtils.getStackTrace(e));
return false;
}
}
@Override
public void unlock(String lockKey) {
RLock lock = redissonClient.getLock(lockKey);
lock.unlock();
}
@Override
public void unlock(RLock lock) {
lock.unlock();
}
public Boolean asyncReentrantLock(String lockKey, int waitTime, int leaseTime) {
RLock lock = redissonClient.getLock(lockKey);
Boolean res = false;
try {
Future<Boolean> result = lock.tryLockAsync(waitTime, leaseTime, timeType);
res = result.get();
} catch (Exception e) {
log.error("分布式锁异步执行失败asyncReentrantLock:{}", ExceptionUtils.getStackTrace(e));
}
return res;
}
public boolean fairLock(String lockKey, int waitTime, int leaseTime) {
RLock fairLock = redissonClient.getFairLock(lockKey);
try {
return fairLock.tryLock(waitTime, leaseTime, timeType);
} catch (Exception e) {
log.error("分布式锁公平锁失败asyncReentrantLock:{}", ExceptionUtils.getStackTrace(e));
return false;
}
}
public boolean fairAsyncLock(String lockKey, int waitTime, int leaseTime) {
RLock fairLock = redissonClient.getFairLock(lockKey);
Boolean res = false;
try {
Future<Boolean> result = fairLock.tryLockAsync(waitTime, leaseTime, timeType);
res = result.get();
} catch (Exception e) {
log.error("公平锁 的异步执行失败asyncReentrantLock:{}", ExceptionUtils.getStackTrace(e));
}
return res;
}
}