当前位置: 代码网 > it编程>编程语言>Java > SpringBoot 启动报错Unable to connect to Redis server: 127.0.0.1/127.0.0.1:6379问题的解决方案

SpringBoot 启动报错Unable to connect to Redis server: 127.0.0.1/127.0.0.1:6379问题的解决方案

2024年10月09日 Java 我要评论
问题描述使用开源项目 youlai-boot 中,使用默认线上配置没问题的,但将 redis 的修改为本地的环境出现无法连接的错误启动项目报错关键信息:org.redisson.client.redi

问题描述

使用开源项目 youlai-boot 中,使用默认线上配置没问题的,但将 redis 的修改为本地的环境出现无法连接的错误

启动项目报错关键信息:

org.redisson.client.redisconnectionexception: unable to connect to redis server: 127.0.0.1/127.0.0.1:6379

稍微完整的错误信息:

org.springframework.beans.factory.unsatisfieddependencyexception: error creating bean with name 'securityconfig' defined in file [d:\project\work\youlai-boot\target\classes\com\youlai\system\config\securityconfig.class]: unsatisfied dependency expressed through constructor parameter 2: error creating bean with name 'redistemplate' defined in class path resource [com/youlai/system/config/redisconfig.class]: unsatisfied dependency expressed through method 'redistemplate' parameter 0: error creating bean with name 'redissonconnectionfactory' defined in class path resource [org/redisson/spring/starter/redissonautoconfigurationv2.class]: unsatisfied dependency expressed through method 'redissonconnectionfactory' parameter 0: error creating bean with name 'redisson' defined in class path resource [org/redisson/spring/starter/redissonautoconfigurationv2.class]: failed to instantiate [org.redisson.api.redissonclient]: factory method 'redisson' threw exception with message: java.util.concurrent.executionexception: org.redisson.client.redisconnectionexception: unable to connect to redis server: 127.0.0.1/127.0.0.1:6379

错误截图:

image-20240529175344056

原因分析

再看下修改前后的对比图

线上 redis 服务配置了密码,而本地 redis 服务没有密码,因此我们理所当然地将 password 的值留空。然而,这正是问题的根本原因,因为空值仍会被解析为一个空字符串,而不是被忽略。

如果只是这样说是没有说服力的,接下来我将本着“源码面前无秘密”的原则,深入揭示问题的本质。

源码分析

假设本地的 redis 未配置密码,springboot 的 redis 连接配置如下:

spring:
  data:
    redis:
      database: 0
      host: 127.0.0.1
      port: 6379
      password:

masterslaveconnectionmanager#createclient 根据配置创建客户端,其中配置的 password 是空字符串而不是null。

baseconnectionhandler#channelactive 方法根据配置进行 redis 客户端连接初始化。由于配置中的 password 被解析成空字符串而非 null,因此尝试使用空字符串作为密码连接 redis,但由于 redis 服务未设置密码,连接失败。

image-20240529205521031

解决方案

由上可知,如果将配置的 password 值设置为空,则客户端在连接时会使用空字符串作为密码进行认证。由于服务端未设置密码,因此连接失败。

解决方法其实很简单,如果 redis 服务未设置密码,需要将 password 注释或删除,而不是设置为空字符串。

spring:
  data:
    redis:
      database: 0
      host: 127.0.0.1
      port: 6379
   	  # 如果 redis 服务未设置密码,需要将 password 删除或注释,而不是设置为空字符串
      # password:

再次调试可以看到 password 为 null ,直接跳过密码认证,直接返回已完成的 completablefuture 对象

以上就是springboot 启动报错unable to connect to redis server: 127.0.0.1/127.0.0.1:6379问题的解决方案的详细内容,更多关于springboot 启动报错unable connect redis的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com