当前位置: 首页 > news >正文

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 格式即可

相关文章:

  • Git日志信息
  • nginx-整合modsecurity做waf
  • 【高并发架构设计】-1:高并发通用设计思想
  • 外出充电不发愁,倍思便携式移动电源成出行新宠
  • 【日撸 Java 三百行】Day 12(顺序表(二))
  • Linux笔记---信号(上)
  • I/O多路复用(select/poll/epoll)
  • 一.Gitee基本操作
  • K8S中构建双架构镜像-从零到成功
  • 反弹shell再入门
  • 计算机网络:CPU与时钟的关系
  • 动手学深度学习12.3.自动并行-笔记练习(PyTorch)
  • 【TVM 教程】microTVM PyTorch 教程
  • @Component 注解:Spring 组件扫描与管理的基石
  • istringstream的简化源码详解
  • django之视图
  • 构建DEEPPOLAR ——Architecture for DEEPPOLAR (256,37)
  • 一文读懂如何使用MCP创建服务器
  • LWIP传输层协议笔记
  • C++ asio网络编程(5)简单异步echo服务器
  • 马上评丨未成年人“擦边”短视频岂能成流量密码
  • 印称一名高级官员在巴基斯坦发动的袭击中死亡
  • 被取消总统候选人资格,金文洙:将采取政治法律措施讨回公道
  • 巴基斯坦称成功拦截印度导弹,空军所有资产安全
  • 协会:坚决支持司法机关依法打击涉象棋行业的违法行为
  • 上海:企业招用高校毕业生可享受1500元/人一次性扩岗补助