目录Redis+lua同步锁Jedis配置Jedis工具类→获取jedisredis 锁工具类加锁示例(jedis+lua)Redis+Lua同步锁 Jedis配置 @Configuration @Getter
@Configuration
@Getter
@Setter
@Slf4j
@ConfigurationProperties(prefix = "jedis")
public class JedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.passWord}")
private String password;
private int timeout;
private int maxTotal;
private int maxIdle;
private int minIdle;
@Bean
public JedisPool jedisPool() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMinIdle(minIdle);
jedisPoolConfig.setMaxTotal(maxTotal);
JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
log.info("JedisPool连接成功:" + host + "\t" + port);
return jedisPool;
}
}
@Component
public class JedisUtil {
@Resource
private JedisPool jedisPool;
public Jedis getJedis() {
return jedisPool.getResource();
}
public void close(Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
}
public class RedisLockUtil {
private static final Long RELEASE_SUCCESS = 1L;
private static final String PREFIX = "api_LOCK_";
public static boolean unLock(Jedis jedis, String lockKey, String valve) {
String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
Object result = jedis.eval(script, Collections.singletonList(PREFIX + lockKey), Collections.singletonList(PREFIX + valve));
if (RELEASE_SUCCESS.equals(result)) {
return true;
}
return false;
}
public static boolean lock(Jedis jedis, String lockKey, String valve, int timeout) {
String script = "if redis.call('setnx',KEYS[1],ARGV[1]) == 1 then" +
" redis.call('expire',KEYS[1],ARGV[2]) return 1 else return 0 end";
Object result = jedis.eval(script, Collections.singletonList(PREFIX + lockKey), Lists.newArrayList(PREFIX + valve, String.valueOf(timeout)));
//判断是否成功
if (RELEASE_SUCCESS.equals(result)) {
return true;
}
return false;
}
}
@Slf4j
@Component
public class InterfaceEventListener {
@Resource
private JedisUtil jedisUtil;
@Value("${jedis.lock.cycle-number}")
private int cycleNumber;
@Value("${jedis.lock.expire-time}")
private int expireTime;
@Value("${jedis.lock.sleep-time}")
private int sleepTime;
@Value("${spring.redis.database}")
private int database;
public void onApplicationEvent(InterfaceEvent event) {
Jedis jedis = jedisUtil.getJedis();
jedis.select(database);
boolean unLock = false;
boolean lock;
int currentNumber = 0;
try {
do {
lock = RedisLockUtil.lock(jedis, "lockKey", "valve", expireTime);
if (lock) {
try {
//todo 加锁的代码
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
} finally {
unLock = RedisLockUtil.unLock(jedis, detectCode, detectCode);
}
}
currentNumber++;
Thread.sleep(sleepTime);
}
while (!unLock && currentNumber < cycleNumber);
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
} finally {
jedisUtil.close(jedis);
}
}
}
以上就是Redis Lua同步锁实现源码解析的详细内容,更多关于Redis Lua同步锁的资料请关注我们其它相关文章!
--结束END--
本文标题: Redis Lua同步锁实现源码解析
本文链接: https://lsjlt.com/news/213049.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0