springBoot连接远程Redis连接失败(已解决)

问题:

首先,我是先用jedis进行的redis连接,成功连接,没有任何问题,说明redis配置,以及访问地址、端口、密码都是正确的。

我的yml文件配置如下:

复制代码
spring:
  redis:
    host: 远程ip地址
    port: 6379
    password: 密码

但是当我使用springboot里面的redisTemplate进行连接的时候,却发生了报错,报错信息如下

复制代码
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis
.....

Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to localhost/<unresolved>:6379
......
Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: localhost/127.0.0.1:6379
.....
Caused by: java.net.ConnectException: Connection refused: no further information
.....

然后我就很奇怪,同样的访问,为什么使用jedis能够成功,但是使用redisTemplate就会失败

解决过程:

目光看向报错信息,Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to localhost/:6379,这段表示在连接本地的redis的时候失败了,???,我本地没有安装redis,当然连接不上了,但是为什么我配置的远程ip,会连接到本地!

由于在springBoot中每个配置项都会有一个默认的自动配置类与之对应,我这里配置了但是没效果,显然配置失败,项目使用默认的localhost进行连接的,那为啥会配置失败呢

在我查了半天教程之后,总算在一篇教程里面发现了类似的情况,如下:

升级springboot3.x踩坑记录-CSDN博客

上面这篇文件就是,作者在从spirngBoot2升到3之后redis连接失败了,查看了源码之后发现redis的前缀发生了改变!!!

而我使用的是spirngBoot3,上面的yml配置方法是springBoot2的配置方法,所以产生了配置失效!

解决方法:

根据源码可知,springboot3中redis的前缀从"spring.redis"变成了"spring.data.redis"

因此我们的配置文件,需要再中间加一个data!

复制代码
spring:
  data:
    redis:
      host: 39.104.26.173
      port: 6379
      password: wen200389

这样就能够成功连接了!

总结:

一定要注意,不同的springboot版本,对应的配置文件的格式,有可能会发生改变,需要及时更正

还有就是,遇到问题,多看源码!!许多问题真的能够通过看源码解决

相关推荐
无籽西瓜a7 小时前
【西瓜带你学设计模式 | 第七期 - 适配器模式】适配器模式 —— 类适配器与对象适配器实现、优缺点与适用场景
java·后端·设计模式·软件工程·适配器模式
前端付豪7 小时前
实现消息级操作栏
前端·人工智能·后端
Flittly7 小时前
【SpringAIAlibaba新手村系列】(6)PromptTemplate 提示词模板与变量替换
java·spring boot·agent
M--Y7 小时前
初识Redis
数据库·redis·缓存
yaaakaaang7 小时前
3.springboot,用eclipse轻松创建~
java·spring boot·eclipse
计算机学姐7 小时前
基于SpringBoot的新能源充电桩管理系统
java·vue.js·spring boot·后端·mysql·spring·java-ee
qq5680180767 小时前
Springboot中使用Elasticsearch(部署+使用+讲解 最完整)
spring boot·elasticsearch·jenkins
瑶山7 小时前
SpringBoot + MongoDB 5分钟快速集成:从0到1实操指南
java·数据库·spring boot·后端·mongodb
Nontee227 小时前
Redis 分布式锁
后端
AI_大白7 小时前
实时行情系统的第一道槛:如何应对数据源的“限流”与“断流”
后端