问题描述

在我用jedis连接虚拟机上的redis(3.2.1)的6380端口时,发现报了如下错误:
Exception in thread “main” redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool,然后用了之前的方法发现没用,就只能一步步排查了。

解决方法

  1. 首先查看对应的redis服务是否开启。
  2. 查看服务开启的对应端口是否为配置文件中所指定的。(注意:我是用配置文件开启的)。vim redis目录/redis.conf文件,将原来的6379改为6380,同时可以注意一下protected-mode yes是开启的。--Redis入坑2--jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 解决方法
    --Redis入坑2--jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 解决方法
    --Redis入坑2--jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 解决方法
  3. 使用客户端连接看看是否能连接上对应的端口。
    --Redis入坑2--jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 解决方法
  4. 以上说明本地redis连接没有问题,接着再查看防火墙有没有开放相关端口,因为我用的是iptables来配置,所以查看的结果是这样。
    --Redis入坑2--jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 解决方法
    发现已经开启对应端口供网络访问,systemctl restart iptables使之生效。
  5. 尝试使用jedis连接。
    --Redis入坑2--jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 解决方法
    发现报错。
  6. 在redis客户端中设置关闭保护模式,config set protected-mode "no",再进行尝试。
    --Redis入坑2--jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 解决方法
  7. 再进行jedis连接尝试。
    --Redis入坑2--jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 解决方法
    发现仍然出错,之后我将连接的端口改为6379默认端口连接,同时开启6379的redis服务,同时也开启了对应的6379端口供网络访问,并且也设置了config set protected-mode "no"
  8. 开始连接6379端口的redis。
    --Redis入坑2--jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 解决方法
    发现连接正常!
  9. 两个端口都开启了,并且redis服务都开启了,6380却仍然连接不上,说明应该是redis服务器出了问题,应该去6380的配置文件中查看。vim redis目录/redis.conf文件
    --Redis入坑2--jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 解决方法
    原来是redis默认只能localhost登录,所以需要开启远程登录。将其注释起来就可以供所有机子访问,当然你也可以设置指定ip才可以访问,即bind targetIP。这里我直接注释了起来。
    --Redis入坑2--jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 解决方法
  10. 注释之后再使用配置文件的方式打开redis服务。
    --Redis入坑2--jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 解决方法
    发现连接还不成功,不用紧张,在redis客户端设置config set protected-mode "no"即可。
    --Redis入坑2--jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 解决方法
  11. 再进行jedis连接测试。
    --Redis入坑2--jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 解决方法
    连接成功!

总结

就是先看服务器,端口是否开启,接下来设置protected-mode “no”,如果这个还不行再修改配置文件里的信息,修改完成后再在redis客户端再设置一下protected-mode “no”,这样应该就可以连接成功(在代码不出错的情况下),总之多排下错,我在这也卡了一晚上,庆幸还是找到错误的源头了,一起加油吧!

相关文章: