参考链接: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