欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > 07_SpringBoot2集成Redis连接失败

07_SpringBoot2集成Redis连接失败

2025/10/21 5:33:05 来源:https://blog.csdn.net/wgy17734894660/article/details/147892357  浏览:    关键词:07_SpringBoot2集成Redis连接失败

🌟 07_SpringBoot2 集成 Redis 连接失败

❓ 场景描述

在 Spring Boot 2 项目中集成 Redis 时,将配置写成了如下形式:

spring:data:redis:host: localhostport: 6379password: 123456

结果启动项目时 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:6379at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.translateException(LettuceConnectionFactory.java:1689)at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1597)at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1383)at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getConnection(LettuceConnectionFactory.java:1366)

经分析,所有配置均正确,但是仍然连接不上。


💥 问题分析:使用了错误的配置前缀

🔍 配置格式写错了!

  • 上述配置是 Spring Boot 3.x 的格式,而我用的是 Spring Boot 2.x
  • 在 Spring Boot 2 中,正确的 Redis 配置前缀应为 spring.redis,而不是 spring.data.redis
  • 所以虽然配置写了,但 根本不会被 Spring Boot 2 识别,Redis 自动配置不会生效!

✅ 正确写法:Spring Boot 2 Redis 配置方式

spring:redis:host: localhostport: 6379password: 123456

只要写成如上格式,Spring Boot 2 就能自动配置 Redis 连接。


🔄 配置格式对比表:Spring Boot 2 vs Spring Boot 3

Spring Boot 版本Redis 配置前缀示例字段
2.xspring.redisspring.redis.host
3.xspring.data.redisspring.data.redis.host

🧠 小结 Tips

  • 配置不生效 ≠ 配置内容错,也可能是配置路径不对
  • Spring Boot 的配置属性映射强依赖于版本,不同版本前缀有差异

📌 建议

  • 在使用第三方教程时,注意确认其对应的 Spring Boot 版本;
  • 若使用的是 Spring Boot 2,请严格使用 spring.redis 格式;
  • 若项目后续升级到 Spring Boot 3,再切换为 spring.data.redis 格式即可

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com