解决 springBoot 中使用redis一段时间不用再操作超时问题Command timed out

springboot 使用 redis 

开始没有问题,过一段时间不知道过多久,就会报redis超时。

各种排查都感觉没有问题。

网上果然有类似的问题--- 默认使用的时lettuce 连接池,然后这个包里有bug。

解决办法就是 不要引入这个jar

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

测试中,等待结果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
在Spring Boot使用Redis实现两个Redis操作加锁的过程如下: 1. 首先,需要在pom.xml文件添加Redis依赖,例如: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 在配置文件添加Redis配置,例如: ``` spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= ``` 3. 在Java代码使用RedisTemplate进行加锁操作,例如: ``` @Autowired private RedisTemplate<String, String> redisTemplate; public boolean tryLock(String key) { ValueOperations<String, String> ops = redisTemplate.opsForValue(); Boolean locked = ops.setIfAbsent(key, "locked"); if (locked != null && locked) { redisTemplate.expire(key, 10, TimeUnit.SECONDS); } return locked != null && locked; } public void releaseLock(String key) { redisTemplate.delete(key); } ``` 在上述代码,tryLock方法使用Redis的setIfAbsent命令进行加锁操作,如果返回值为true,则表示加锁成功;否则表示加锁失败。同时,设置了过期时间为10秒,避免锁永久存在。releaseLock方法使用Redis的delete命令进行解锁操作。 4. 在需要加锁的代码块进行加锁操作,例如: ``` if (tryLock("lock-key")) { try { // 执行加锁操作 } finally { releaseLock("lock-key"); } } else { // 加锁失败,处理异常情况 } ``` 在上述代码使用tryLock方法进行加锁操作,如果加锁成功,则执行加锁操作;否则处理加锁失败的情况。 通过以上步骤,就可以在Spring Boot使用Redis实现两个Redis操作加锁。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值