org.springframework.data.redis.RedisConnectionFailureException 解决办法

题主在测试spingdata连接redis时,遇到了

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is org.springframework.data.redis.connection.PoolException: Could not get a resource from the pool; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379

这个错误

其中测试文件为

class SpringDataRedisApplicationTests {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    void contextLoads() {
    }

    @Test
    void writeTest(){
        redisTemplate.opsForValue().set("keyTest", "valueTest");
        String keyTest = (String)redisTemplate.opsForValue().get("keyTest");
        System.out.println("The name is " + keyTest);
    }

}

redis配置为

spring:
  data:
    redis:
      host: 192.168.188.100
      port: 6379
      password: 123456
      lettuce:
        pool:
          max-active: 8
          max-idle: 8
          min-idle: 0
          max-wait: 1000ms

查找了一会原因后,发现是redis配置的问题

spring.data.redis这个redis配置方式是springboot3.x后才开始引入的,而题主用的是springboot2.7,将redis配置改为spring.redis…后问题得到解决

spring:
   redis:
     host: 192.168.188.100
     port: 6379
     password: 123456
     lettuce:
       pool:
         max-active: 8
         max-idle: 8
         min-idle: 0
         max-wait: 1000ms

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://www.net2asp.com/94c2ba5e63.html