使用Jedis连接池模式容易遇到无法获取连接池的错误如下。
1 | redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool |
可以根据以下几种方法进行分类排查。
1 | auth [$Password] |
说明:[$Password]为密码。
1 | netstat -an | grep 6379 | grep EST | wc -l |
对于JedisPool连接池的操作,每次getResource之后需要调用returnResource或者close进行归还,可以查看代码是否是正确使用,代码示例如下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | JedisPoolConfig config = new JedisPoolConfig(); //最大空闲连接数,应用自己评估,不要超过ApsaraDB for Redis每个实例最大的连接数 config.setMaxIdle( 200 ); //最大连接数,应用自己评估,不要超过ApsaraDB for Redis每个实例最大的连接数 config.setMaxTotal( 300 ); config.setTestOnBorrow( false ); config.setTestOnReturn( false ); String host = "*.aliyuncs.com" ; String password = "密码" ; JedisPool pool = new JedisPool(config, host, 6379 , 3000 , password); Jedis jedis = null ; try { jedis = pool.getResource(); /// ... do stuff here ... for example jedis.set( "foo" , "bar" ); String foobar = jedis.get( "foo" ); jedis.zadd( "sose" , 0 , "car" ); jedis.zadd( "sose" , 0 , "bike" ); Set<String> sose = jedis.zrange( "sose" , 0 , - 1 ); } finally { if (jedis != null ) { jedis.close(); } } /// ... when closing your application: pool.destroy(); |
通过dmesg检查客户端是否有异常。如果发生nf_conntract丢包可以修改设置,参考sysctl -w net.netfilter.nf_conntrack_max=120000
命令。
1 | nf_conntrack: table full, dropping packet |
ss -s
命令查看time wait链接是否过多,结果如下。1 | sysctl -w net.ipv4.tcp_max_tw_buckets= 180000 <br>sysctl -w net.ipv4.tcp_tw_recycle= 1 |
在/etc/hosts
文件直接绑定host地址,绑定完成之后查看问题是否还存在,如果还存在则不是DNS解析问题。
1 | 192 .XXX.XXX. 1 *.redis.rds.aliyuncs.com |
1 | sudo tcpdump -i eth0 tcp and port 6379 -n -nn -s 74 -w redis.cap |