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

项目开发的过程中,应业务需求将Redis作为缓存服务器整合进了Spring Boot的项目中,在初测试的时候,遇到连接超时的情况,错误信息如下:

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 192.168.43.129:6379
    at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:132)
    at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:95)
    at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:82)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:211)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:184)
    at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:95)
    at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:198)
    at com.leyou.test.RedisTest.testRedis(RedisTest.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
 
 
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to 192.168.43.129:6379
    at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:56)
    at io.lettuce.core.AbstractRedisClient.getConnection(AbstractRedisClient.java:233)
    at io.lettuce.core.RedisClient.connectStandalone(RedisClient.java:253)
    at io.lettuce.core.RedisClient.connect(RedisClient.java:202)
    at org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.getConnection(StandaloneConnectionProvider.java:56)
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:959)
    ... 41 more
Caused by: io.netty.channel.ConnectTimeoutException: connection timed out: /192.168.43.129:6379

网上很多,你应该已经查了一溜够了!全是要 protected-mode 修改为 no  并且 将 bind属性注释掉 (老版本只注释掉bind),不想说,治标不治本

config 配置文件!

################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1
# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
#    "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode yes
......

如果链接不上,我看很多都让把 protected-mode 修改为 no  并且 将 bind属性注释掉 (老版本只注释掉bind),但是,这样,不就谁都可以链接redis数据库了?那就不安全了啊

所以protected-mode属性根本不用改,bind属性不要注释,只需要在bind 后添加你想要链接此数据库的地址就好了 如:

bind 192.168.1.100 10.0.0.1 就代表192.168.1.100和10.0.0.1是可以链接到数据库的,人家写的很清楚了!而且这也是安全并被推荐使用的!

像这种情况,出现的原因可能有两种:

  • 其一,在redis的配置文件中,没有配置局域网的访问权限
  • 其二,被访问方的服务器的6379端口不允许访问

如果是云服务器,记得开放对应的6379端口号,我的是第二种情况

针对第一种情况,具体解决方案如下

打开配置文件redis.conf,搜索 “ bind ”,你会看到这一句:

这个bind配置的是,允许以这些地址来访问我们的reids,配置之前是只有127.0.0.1的,那只需要写上我们服务器的地址就好了,,注意,写的是服务器自己的地址,那么在这个配置之后,与其同局域网的电脑也就可以访问这个redis了

记得在改完配置文件后,重启redis,使得配置生效。

 

针对第二种情况,具体解决方案

首先运行: firewall-cmd --query-port=6379/tcp 以检测我们的端口是不是被允许访问的

这里很明显:

 

6379端口不允许外界访问。

所以我们需要打开端口:

firewall-cmd --zone=public --add-port=6379/tcp --permanent

然后重启防火墙即可!

开放端口:

firewall-cmd --zone=public --add-port=6379/tcp --permanent

关闭端口:

firewall-cmd --permanent --remove-port=6379/tcp

查询端口是否开放:

firewall-cmd --query-port=6379/tcp

重启:

firewall-cmd --reload

 

  • 6
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis 是一个Spring Data Redis库中的异常,表示无法连接到Redis数据库。 通常情况下,这个异常的原因可以归结为以下几个方面: 1. Redis服务器未启动或未正确配置。你可以检查Redis服务器的状态以及配置文件中的主机和端口信息是否正确。确保Redis服务器正在运行,并且你的应用程序可以访问到它。 2. 网络连接问题。可能是由于网络连接中断、防火墙配置或者Redis服务器配置中的限制导致的。你可以检查网络连接是否正常,并确保防火墙允许你的应用程序与Redis服务器进行通信。 3. 密码错误。如果Redis服务器设置了密码,你需要在应用程序中正确地配置密码。检查你的应用程序配置文件中的密码是否正确。 综上所述,如果你遇到了org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis的异常,请检查Redis服务器的状态和配置、网络连接以及密码是否正确配置。如果问题仍然存在,你可以尝试重启Redis服务器或联系Redis技术支持寻求帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [springboot连接redis出现org.springframework.data.redisRedisConnectionFailureException异常](https://blog.csdn.net/m0_67393828/article/details/123684060)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [org.springframework.data.redis.RedisConnectionFailureException异常解决](https://blog.csdn.net/Sexycool/article/details/122352805)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值