当前位置: 首页 > 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 格式即可
http://www.dtcms.com/a/186301.html

相关文章:

  • 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服务器
  • 【】东方财务的Choice数据量化接口,在linux上安装python 版本,需要联系客户经理审核通过后就可以使用了。使用接口更加稳定和全面。
  • 智能指针入门:深入理解 C++ 的 shared_ptr
  • 【Mysql基础】一、基础入门和常见SQL语句
  • Matlab自学笔记五十四:符号数学工具箱和符号运算、符号求解、绘图
  • LLaMA Factory 深度调参
  • 右值和移动
  • 国产化Excel处理控件Spire.XLS系列教程:如何通过 C# 删除 Excel 工作表中的筛选器
  • 开疆智能Profinet转Canopen网关连接sick RFID读写器配置案例
  • 212. 单词搜索 II【 力扣(LeetCode) 】
  • RDB和AOF的区别