Redisson 使用分布式锁出现 WRONGTYPE Operation against a key holding the wrong kind of value 错误

异常信息

org.redisson.client.RedisException: WRONGTYPE Operation against a key holding the wrong kind of value.

结论

  • 分布式锁使用的key和别的键值对共用了同一个键

为什么这么说呢,写一段junit测试代码,这里RedissonClient配置就不展示了,有兴趣可以自行去了解一下

@RunWith(SpringRunner.class)
@SpringBootTest(classes = AccountServiceApplication.class)
class IScMemberProfitImplServiceTest {

	@Autowired
    private RedissonClient redissonClient;

	@Test
    void testRedisson() {
        //获取12345678对应的键
        RBucket<Object> bucket = redissonClient.getBucket("12345678");
        //获取键对应的值(此时为null)
        String o1 = (String)bucket.get();
        //设置同样的key
        RLock lock = redissonClient.getLock("12345678");
        //获取分布式锁,有效期2分钟
        lock.lock(2, TimeUnit.MINUTES);
        //测试重入锁
        //lock.lock(2, TimeUnit.MINUTES);
        //lock.lock(2, TimeUnit.MINUTES);

		//设置这个键对应的值为999,有效期为两分钟
        bucket.set("999",2, TimeUnit.MINUTES);
        
 		//释放分布式锁
        if(lock.isLocked()){
        	if(lock.isHeldByCurrentThread()){
            	lock.unlock();
            }
        }
}

运行完以上代码,线程在 if(lock.isHeldByCurrentThread()){ 处抛出如题异常,

回看以上代码,在分布式锁获取后,查看redis看到,redis中‘12345678’对应存的hash类型,内容如下
在这里插入图片描述
那运行到bucket.set("999",2, TimeUnit.MINUTES); 为什么不会报错呢? 分布式锁存储了hash类型,这不跟String类型冲突了吗

emmm,这要是在mysql确实执行不过去,但对于redis来说,set就意味着直接覆盖,因此,原来存储的不管是什么类型,都会直接覆盖


那为什么执行到if(lock.isHeldByCurrentThread()){ 这里又会报错呢?

额,接着上面所说,由于键 “12345678” 对应的值呗覆盖成了 “999” 字符串,线程执行到此行代码是,判断当前执行的线程是否与redis中存储的线程信息一致,但redis中的值结构已经不再是hash类型,因此会抛出异常

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Spring Boot项目中使用Redisson实现分布式锁,需要按照以下步骤进行: 1. 在项目中引入Redisson依赖,可以在pom.xml文件中添加以下代码: ``` <dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.13.3</version> </dependency> ``` 2. 配置Redisson连接,可以在application.yml文件中添加以下代码: ``` redisson: address: redis://127.0.0.1:6379 database: 0 connection-pool-size: 100 password: 123456 ``` 3. 创建RedissonClient对象,可以在Spring Boot项目的启动类中添加以下代码: ``` @Configuration public class RedissonConfig { @Value("${redisson.address}") private String address; @Value("${redisson.password}") private String password; @Value("${redisson.connection-pool-size}") private int connectionPoolSize; @Value("${redisson.database}") private int database; @Bean public RedissonClient redissonClient() { Config config = new Config(); config.useSingleServer().setAddress(address) .setPassword(password) .setConnectionPoolSize(connectionPoolSize) .setDatabase(database); return Redisson.create(config); } } ``` 4. 使用Redisson实现分布式锁,可以在需要加锁的代码中添加以下代码: ``` @Autowired private RedissonClient redissonClient; public void lockMethod() { RLock lock = redissonClient.getLock("lockKey"); try { lock.lock(); // 被锁定的代码 } finally { lock.unlock(); } } ``` 以上代码就是使用Redisson实现分布式锁的基本过程,实际项目中可能还需要根据实际情况进行修改和优化。 ### 回答2: Spring Boot是一款用于快速构建Spring应用程序的开发框架,而Redisson则是一个基于Redis的Java驻留内存数据网格(In-Memory Data Grid)和分布式锁框架。 在Spring Boot中使用Redisson来实现分布式锁,需要进行以下几个步骤: 1. 引入Redisson依赖:在pom.xml文件中添加Redisson的依赖。例如: ```xml <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>3.13.3</version> </dependency> ``` 2. 配置Redisson:在application.properties或application.yml文件中配置Redisson连接信息。例如: ```yaml spring: redis: host: 127.0.0.1 port: 6379 password: password ``` 3. 创建RedissonClient Bean:在应用程序的配置类中创建RedissonClient的Bean实例。例如: ```java @Configuration public class RedissonConfig { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private String port; @Bean public RedissonClient redisson() { Config config = new Config(); config.useSingleServer() .setAddress("redis://" + host + ":" + port); return Redisson.create(config); } } ``` 4. 使用分布式锁:在需要进行分布式锁控制的代码块中,通过RedissonClient来获取分布式锁对象,并使用分布式锁对象来实现具体的业务逻辑。例如: ```java @Service public class MyService { @Autowired private RedissonClient redisson; public void doSomething() { RLock lock = redisson.getLock("myLock"); try { lock.lock(); // 执行业务逻辑 } finally { lock.unlock(); } } } ``` 上述代码中,通过调用`redisson.getLock("myLock")`来获取名为"myLock"的分布式锁对象(RLock),然后通过`lock.lock()`来获取锁,执行业务逻辑,最后通过`lock.unlock()`来释放锁。 这样,Spring Boot就可以通过Redisson实现分布式锁的功能了。分布式锁的主要作用是在分布式系统中保证同一时刻只有一个线程能够访问共享资源,避免数据的冲突和不一致。通过使用Redisson,我们可以方便地在Spring Boot应用中实现分布式锁的控制,保证数据的一致性和可靠性。 ### 回答3: Spring Boot是一个快速开发框架,可以简化Java应用程序的开发过程。而Redisson是一个使用Java实现的Redis客户端,它提供了一种简单易用且高效的分布式锁解决方案。 要使用Redisson实现分布式锁,我们需要完成以下几个步骤: 1. 添加Redisson依赖:首先,在Spring Boot项目的pom.xml文件中添加Redisson的依赖。可以通过在<dependencies>标签内添加如下代码来引入Redisson: ```xml <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>3.11.3</version> </dependency> ``` 2. 添加Redis配置信息:在Spring Boot项目的配置文件(如application.properties)中添加Redis的相关配置信息,包括主机名、端口号、密码等。示例配置如下: ```properties spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password= ``` 3. 创建RedissonClient Bean:在Spring Boot的配置类中创建一个RedissonClient的Bean,并设置好相应的Redis配置信息。示例代码如下: ```java @Configuration public class RedissonConfig { @Value("${spring.redis.host}") private String redisHost; @Value("${spring.redis.port}") private String redisPort; @Bean public RedissonClient redissonClient() { Config config = new Config(); config.useSingleServer().setAddress("redis://" + redisHost + ":" + redisPort); return Redisson.create(config); } } ``` 4. 使用Redisson获取锁:在需要加锁的业务方法中,通过RedissonClient的getFairLock方法获取一个公平锁对象,然后使用lock方法获取锁。示例代码如下: ```java @Service public class MyService { @Autowired private RedissonClient redissonClient; public void doSomething() { RLock lock = redissonClient.getFairLock("myLock"); try { lock.lock(); // 执行需要加锁的业务逻辑 } finally { lock.unlock(); } } } ``` 以上就是使用Spring Boot和Redisson实现分布式锁的基本步骤。通过Redisson提供的锁对象,我们可以在需要时通过lock方法获取锁,然后执行需要加锁的业务逻辑,最后通过unlock方法释放锁。Redisson会自动处理锁的有效期和宕机等异常情况,保证高可用和数据一致性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值