建议使用 Redission
@Resource
private RedissonClient redissonClient;
RLock lock = redissonClient.getLock(lockKey);
try {
if (!lock.tryLock()) {
log.warn(".....{}", lockKey);
return false;
}
...
} catch(Exception e) {
...
} finally {
for (int i = 0; i < unlockRetryTimes; i++) {
try {
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
break;
} catch (Exception e) {
log.error("【触发器执行】分布式锁释放失败:key={}", lockKey, e);
}
}
}
参考文档:
https://mp.weixin.qq.com/s/OChnLc8FkUWUVdJuBFjnVQ
https://mp.weixin.qq.com/s/5-Hc1GFEXTYFjSqGYTH3MQ