20-Redis6-分布式锁

本文探讨了Redis作为分布式锁的使用,从最初的setnx命令到解决锁无法释放的问题,通过设置过期时间来避免死锁。同时,针对多个应用共享同一锁可能导致的错误释放,提出了使用UUID验证和Lua脚本的解决方案,确保锁的安全性。
摘要由CSDN通过智能技术生成

参考链接:https://www.bilibili.com/video/BV1Rv41177Af?p=43&spm_id_from=pageDriver


 


1、刚开始 可以使用sexnx   这个命令上锁。。。使用完毕删除该key释放锁

2、使用setnx出现问题,使用sexnx一直不释放锁。怎么办?可以给锁加一个过期时间。

3、使用setnx 之后,准备设置过期时间。但是不能设置过期时间。怎么办?同时上锁的时候,同时设置过期时间      set key1 vvvv1 nx ex 12  #设置key1 值是vvvv1 过期时间是12秒

4、锁的释放问题

5、锁的UUID一样之后,还能删锁问题

6、锁的特点

互斥性:相互互斥

不发生死锁

各自释放各自的锁

加锁解锁有原子性

 

 

 


先测试1:加锁操作值释放锁。没有获得锁就等待

application.properties

spring.redis.cluster.nodes=192.168.181.138:7001, 192.168.181.138:7002,192.168.181.138:7003,192.168.181.138:7004,192.168.181.138:7005,192.168.181.138:7006
spring.redis.cluster.max-redirects=3
spring.redis.lettuce.pool.max-active=5
spring.redis.lettuce.pool.max-wait=-1
spring.redis.lettuce.shutdown-timeout=6000

config

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Serializable>
    redisTemplate(LettuceConnectionFactory connectionFactory) {
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setConnectionFacto
以下是使用Spring Redis实现Redis分布式锁的例子: 首先,在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> ``` 然后,创建一个Redis分布式锁的接口: ```java public interface RedisLock { /** * 获取锁 * * @param key 锁的key * @param expireTime 锁的过期时间 * @return 是否获取成功 */ boolean lock(String key, long expireTime); /** * 释放锁 * * @param key 锁的key */ void unlock(String key); } ``` 接着,实现Redis分布式锁的接口: ```java @Component public class RedisLockImpl implements RedisLock { private final RedisTemplate<String, String> redisTemplate; private final ThreadLocal<Map<String, Integer>> lockers = new ThreadLocal<>(); @Autowired public RedisLockImpl(RedisTemplate<String, String> redisTemplate) { this.redisTemplate = redisTemplate; } @Override public boolean lock(String key, long expireTime) { Map<String, Integer> localLockers = getLocalLockers(); Integer count = localLockers.get(key); if (count != null) { localLockers.put(key, count + 1); return true; } Boolean result = redisTemplate.opsForValue().setIfAbsent(key, ""); if (!result) { return false; } redisTemplate.expire(key, expireTime, TimeUnit.MILLISECONDS); localLockers.put(key, 1); return true; } @Override public void unlock(String key) { Map<String, Integer> localLockers = getLocalLockers(); Integer count = localLockers.get(key); if (count == null) { return; } if (count > 1) { localLockers.put(key, count - 1); } else { localLockers.remove(key); redisTemplate.delete(key); } } private Map<String, Integer> getLocalLockers() { Map<String, Integer> localLockers = lockers.get(); if (localLockers != null) { return localLockers; } lockers.set(new HashMap<>()); return lockers.get(); } } ``` 最后,在需要使用分布式锁的地方,注入RedisLock接口,使用lock()方法获取锁,使用unlock()方法释放锁: ```java @Service public class UserService { private final RedisLock redisLock; @Autowired public UserService(RedisLock redisLock) { this.redisLock = redisLock; } public void updateUser(String userId) { String lockKey = "user_" + userId; boolean locked = redisLock.lock(lockKey, 5000); if (!locked) { throw new RuntimeException("获取锁失败"); } try { // 更新用户信息 } finally { redisLock.unlock(lockKey); } } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值