org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis xxx
Posted 最小的帆也能远航
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis xxx的主要内容,如果未能解决你的问题,请参考以下文章
添加 HAL 浏览器时出错:找不到依赖项 'org.springframework.data:spring-data-rest-hal-browser:'
嵌套异常是 java.lang.NoClassDefFoundError: org/springframework/data/mapping/IdentifierAccessor
MongoDB $addFields 使用 org.springframework.data.mongodb.core.MongoTemplate
org.springframework.data.annotation.Transient 和 javax.persistence.Transient 的区别
@Transient 注解、@org.springframework.data.annotation.Transient 注解、transient 关键字和密码存储
org.springframework.data.redis.serializer.SerializationException: Cannot serialize; nested exception