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

项目开发的过程中,应业务需求将Redis作为缓存服务器整合进了Spring Boot的项目中,在初测试的时候,遇到连接超时的情况,错误不想说,治标不治本

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/org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis xxx

 

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