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部署機器的狀態(tài)信息,發(fā)現(xiàn)整體機器運行狀態(tài)良好
1.2 服務器設置防火墻,導致連接失敗
因為正常的代碼流程都可以跑通,所以防火墻設置沒有問題;
1.3 redis配置文件bind監(jiān)聽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,即可以監(jiān)聽每一個可用的網絡接口;相當于配置為:
我們的配置文件也配置正常,而且正常的代碼流程運行正常,也可以佐證這一點;
1.4 Jedis使用配置問題
目前Jedis的連接池配置如下:
private static JedisPool getPool() {
if (pool == null) {
JedisPoolConfig config = new JedisPoolConfig();
//控制一個pool可分配多少個jedis實例,通過pool.getResource()來獲??;
//如果賦值為-1,則表示不限制;如果pool已經分配了maxActive個jedis實例,則此時pool的狀態(tài)為exhausted(耗盡)。
config.setMaxActive(10);
//控制一個pool最多有多少個狀態(tài)為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的源代碼,發(fā)現(xiàn)如下:
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作為默認的超時時間,繼續(xù)跟蹤源碼:
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的連接就斷開,從而報錯,這在數據庫請求并發(fā)量比較大的時候是有可能發(fā)生的,遂做如下更改,在創(chuàng)建JedisPool的時候,傳入一個較大的超時時間:
pool = new JedisPool(config, ParamUtil.REDIS_ADDRESS[0], ParamUtil.REDIS_PORT, 1000 * 10);
2、總結
遇到問題還是多查,多看源碼,多看源碼中的配置,仔細一項一項地排查問題!
到此這篇關于Redis連接超時異常處理的文章就介紹到這了,更多相關Redis連接超時異常內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Redis分布式鎖實現(xiàn)方式及超時問題解決
- Redis開啟鍵空間通知實現(xiàn)超時通知的步驟詳解
- Redis總結筆記(二):C#連接Redis簡單例子
- Python與Redis的連接教程
- 詳解Redis開啟遠程登錄連接
- java 連接Redis的小例子