【发布时间】:2015-03-07 07:43:18
【问题描述】:
所以,我有 2 个使用 jedis 的应用程序。它们都连接到同一台服务器,并且一个监听 Publish 以检查是否写入了某些内容。好吧,经过大约 10 个小时的一致使用和加载、设置/获取/发布/订阅等,绝地返回 Broken Pipe。我不知道为什么,因为我在绝地武士中超时为 0。有什么想法吗?
【问题讨论】:
-
有人知道问题是什么吗?
所以,我有 2 个使用 jedis 的应用程序。它们都连接到同一台服务器,并且一个监听 Publish 以检查是否写入了某些内容。好吧,经过大约 10 个小时的一致使用和加载、设置/获取/发布/订阅等,绝地返回 Broken Pipe。我不知道为什么,因为我在绝地武士中超时为 0。有什么想法吗?
【问题讨论】:
据我所知,Jedis 保持与 Redis 的开放连接,并且不检查这些连接的状态。如果连接在空闲时间被中断(网络被重置或暂时断开,或连接超时),Jedis 连接池基本上会失效,但 Jedis 客户端不会报告,直到您尝试向下发送命令。 这是github上的讨论:https://github.com/xetorthio/jedis/issues/185
我解决这个问题的方法是在发送任何数据之前发送一个“ping”:
private Jedis getResource() {
Jedis jedis;
try{
jedis = pool.getResource();
jedis.ping();
} catch (JedisException e) {
pool.destroy();
pool = getPool(this.url);
jedis = pool.getResource();
}
return jedis;
}
public String get(EphemeralKey key, EphemeralLocation location) throws Exception {
String encodedKey = encodeKey(key, location);
try (Jedis jedis = getResource()) {
return jedis.get(encodedKey);
} catch (JedisException e) {
throw wrapJedisException(e);
}
return null;
}
【讨论】: