0


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/<unresolved>: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版本,对应的配置文件的格式,有可能会发生改变,需要及时更正

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


本文转载自: https://blog.csdn.net/qq_63189148/article/details/135222238
版权归原作者 雪飞,雨落 所有, 如有侵权,请联系我们删除。

“springBoot连接远程Redis连接失败(已解决)”的评论:

还没有评论