1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
@Component public class EnhancedRedisDistributedLock { @Autowired private RedisTemplate<String, String> redisTemplate; @Autowired private WatchdogRenewalService watchdogRenewalService; private static final String LOCK_PREFIX = "enhanced:lock:"; private static final String RENEWAL_SCRIPT = "if redis.call('get', KEYS[1]) == ARGV[1] then " + " return redis.call('expire', KEYS[1], ARGV[2]) " + "else " + " return 0 " + "end";
public LockInfo tryLockWithWatchdog(String lockKey, String lockValue, long expireTime, long renewalInterval) { try { String key = LOCK_PREFIX + lockKey; Boolean success = redisTemplate.opsForValue() .setIfAbsent(key, lockValue, Duration.ofSeconds(expireTime)); if (success != null && success) { watchdogRenewalService.startWatchdogRenewal( lockKey, lockValue, renewalInterval, expireTime); LockInfo lockInfo = new LockInfo(lockKey, lockValue, expireTime, renewalInterval); log.info("获取锁成功(带看门狗): lockKey={}, lockValue={}, expireTime={}s", lockKey, lockValue, expireTime); return lockInfo; } else { log.debug("获取锁失败: lockKey={}, lockValue={}", lockKey, lockValue); return null; } } catch (Exception e) { log.error("获取锁异常: lockKey={}, lockValue={}", lockKey, lockValue, e); return null; } }
public boolean releaseLockWithWatchdog(LockInfo lockInfo) { if (lockInfo == null) { return false; } try { watchdogRenewalService.stopWatchdogRenewal( lockInfo.getLockKey(), lockInfo.getLockValue()); boolean success = releaseLock(lockInfo.getLockKey(), lockInfo.getLockValue()); if (success) { log.info("释放锁成功(停止看门狗): lockKey={}, lockValue={}", lockInfo.getLockKey(), lockInfo.getLockValue()); } return success; } catch (Exception e) { log.error("释放锁异常: lockKey={}, lockValue={}", lockInfo.getLockKey(), lockInfo.getLockValue(), e); return false; } }
public boolean renewLock(String lockKey, String lockValue, long expireTime) { try { String key = LOCK_PREFIX + lockKey; DefaultRedisScript<Long> script = new DefaultRedisScript<>(); script.setScriptText(RENEWAL_SCRIPT); script.setResultType(Long.class); Long result = redisTemplate.execute(script, Collections.singletonList(key), lockValue, String.valueOf(expireTime)); return result != null && result == 1; } catch (Exception e) { log.error("续约锁异常: lockKey={}, lockValue={}", lockKey, lockValue, e); return false; } }
public String getLockValue(String lockKey) { try { String key = LOCK_PREFIX + lockKey; return redisTemplate.opsForValue().get(key); } catch (Exception e) { log.error("获取锁值异常: lockKey={}", lockKey, e); return null; } }
private boolean releaseLock(String lockKey, String lockValue) { try { String key = LOCK_PREFIX + lockKey; String unlockScript = "if redis.call('get', KEYS[1]) == ARGV[1] then " + " return redis.call('del', KEYS[1]) " + "else " + " return 0 " + "end"; DefaultRedisScript<Long> script = new DefaultRedisScript<>(); script.setScriptText(unlockScript); script.setResultType(Long.class); Long result = redisTemplate.execute(script, Collections.singletonList(key), lockValue); return result != null && result == 1; } catch (Exception e) { log.error("释放锁异常: lockKey={}, lockValue={}", lockKey, lockValue, e); return false; } } }
@Data @AllArgsConstructor public class LockInfo { private String lockKey; private String lockValue; private long expireTime; private long renewalInterval; }
|