1、相关配置
@Component
public class RedissonDistributedLock {
@Autowired
private RedissonClient redissonClient;
public boolean tryLock(String lockKey, long expireTime, long waitTime) throws InterruptedException {
RLock lock = redissonClient.getLock(lockKey);
return lock.tryLock(waitTime, expireTime, TimeUnit.MILLISECONDS);
}
public void releaseLock(String lockKey) {
RLock lock = redissonClient.getLock(lockKey);
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}
2、相关代码