Redis连接超时异常的处理方法
0、问题描述
使用jedis连接redis进行数据查询操作,正常的代码运行没有问题,但是时不时会报出如下错误:
exception in thread "main" redis.clients.jedis.exceptions.jedisconnectionexception: java.net.sockettimeoutexception: read timed out
at redis.clients.util.redisinputstream.ensurefill(redisinputstream.java:202)
at redis.clients.util.redisinputstream.read(redisinputstream.java:181)
at redis.clients.jedis.protocol.processbulkreply(protocol.java:181)
at redis.clients.jedis.protocol.process(protocol.java:155)
at redis.clients.jedis.protocol.processmultibulkreply(protocol.java:206)
at redis.clients.jedis.protocol.process(protocol.java:157)
at redis.clients.jedis.protocol.processmultibulkreply(protocol.java:206)
at redis.clients.jedis.protocol.process(protocol.java:157)
at redis.clients.jedis.protocol.read(protocol.java:215)
at redis.clients.jedis.connection.readprotocolwithcheckingbroken(connection.java:340)
at redis.clients.jedis.connection.getrawobjectmultibulkreply(connection.java:285)
at redis.clients.jedis.connection.getobjectmultibulkreply(connection.java:291)
at redis.clients.jedis.binaryjedis.hscan(binaryjedis.java:3390)
at com.ict.mcg.filter.duplicatecluefilterv2.hscan(duplicatecluefilterv2.java:867)
at com.ict.mcg.filter.duplicatecluefilterv2.collectrecentcluekeywords(duplicatecluefilterv2.java:487)
at com.ict.mcg.main.getcluesmain.run_online(getcluesmain.java:208)
at com.ict.mcg.main.getcluesmain.main(getcluesmain.java:1685)
caused by: java.net.sockettimeoutexception: read timed out
at java.net.socketinputstream.socketread0(native method)
at java.net.socketinputstream.socketread(socketinputstream.java:116)
at java.net.socketinputstream.read(socketinputstream.java:171)
at java.net.socketinputstream.read(socketinputstream.java:141)
at java.net.socketinputstream.read(socketinputstream.java:127)
at redis.clients.util.redisinputstream.ensurefill(redisinputstream.java:196)
... 16 more
究其原因,可以定位为java.net.sockettimeoutexception: read timed out,即网络连接异常;
1、 可能的原因
1.1 服务器资源包括内存、磁盘、cpu等利用率高
经过查看redis部署机器的状态信息,发现整体机器运行状态良好
1.2 服务器设置防火墙,导致连接失败
因为正常的代码流程都可以跑通,所以防火墙设置没有问题;
1.3 redis配置文件bind监听host配置不当
redis的配置文件中bind对应host的配置如下:
# by default redis listens for connections from all the network interfaces # available on the server. it is possible to listen to just one or multiple # 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
默认的bind绑定的host为0.0.0.0,即可以监听每一个可用的网络接口;相当于配置为:
bind 0.0.0.0
我们的配置文件也配置正常,而且正常的代码流程运行正常,也可以佐证这一点;
1.4 jedis使用配置问题
目前jedis的连接池配置如下:
private static jedispool getpool() { if (pool == null) { jedispoolconfig config = new jedispoolconfig(); //控制一个pool可分配多少个jedis实例,通过pool.getresource()来获取; //如果赋值为-1,则表示不限制;如果pool已经分配了maxactive个jedis实例,则此时pool的状态为exhausted(耗尽)。 config.setmaxactive(10); //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。 config.setmaxidle(2); //表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出jedisconnectionexception; config.setmaxwait(1000 * 200000); //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; config.settestonborrow(true); config.settestonreturn(true); //目前redis只有一个服务器 pool = new jedispool(config, "localhost", 6379); } return pool; } private static jedis getjedis() { jedis jedis = null; int count = 0; do { try { pool = getpool(); jedis = pool.getresource(); } catch(exception e) { // system.out.println(e.getmessage()); e.printstacktrace(); pool.returnbrokenresource(jedis); } count++; } while (jedis==null && count < 3); return jedis; }
构建jedispool的逻辑中,只是设置了config.setmaxwait(1000 * 200000);,这个是引入新的jedis实例的最大等待时间,并没有进行其他相关的连接超时的配置;于是查看jedispool的源代码,发现如下:
public jedispool(final config poolconfig, final string host) { this(poolconfig, host, protocol.default_port, protocol.default_timeout, null, protocol.default_database); } public jedispool(string host, int port) { this(new config(), host, port, protocol.default_timeout, null, protocol.default_database); } public jedispool(final string host) { this(host, protocol.default_port); } public jedispool(final config poolconfig, final string host, int port, int timeout, final string password) { this(poolconfig, host, port, timeout, password, protocol.default_database); } public jedispool(final config poolconfig, final string host, final int port) { this(poolconfig, host, port, protocol.default_timeout, null, protocol.default_database); } public jedispool(final config poolconfig, final string host, final int port, final int timeout) { this(poolconfig, host, port, timeout, null, protocol.default_database); } public jedispool(final config poolconfig, final string host, int port, int timeout, final string password, final int database) { super(poolconfig, new jedisfactory(host, port, timeout, password, database)); }
由上述代码可以看到,jedispool有多个重载的构造函数,并且构造函数中需要传入一个timeout参数作为连接的超时时间,如果没有传,则采用protocol.default_timeout作为默认的超时时间,继续跟踪源码:
public final class protocol { public static final int default_port = 6379; public static final int default_timeout = 2000; public static final int default_database = 0; public static final string charset = "utf-8"; public static final byte dollar_byte = '$'; public static final byte asterisk_byte = '*'; public static final byte plus_byte = '+'; public static final byte minus_byte = '-'; public static final byte colon_byte = ':'; private protocol() { // this prevent the class from instantiation }
可以得出结论,默认jedispool中连接的默认超时时间为2秒,而我们调用的jedispool构造函数,恰恰采用的是这个配置,只要两秒钟没有连接成功,redis的连接就断开,从而报错,这在数据库请求并发量比较大的时候是有可能发生的,遂做如下更改,在创建jedispool的时候,传入一个较大的超时时间:
pool = new jedispool(config, paramutil.redis_address[0], paramutil.redis_port, 1000 * 10);
2、总结
遇到问题还是多查,多看源码,多看源码中的配置,仔细一项一项地排查问题!
到此这篇关于redis连接超时异常处理的文章就介绍到这了,更多相关redis连接超时异常内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!