所以,我有 2 个使用 jedis 的应用程序。它们都连接到同一台服务器,并且其中一个监听 Publish 以检查是否写入了某些内容。好吧,经过大约 10 小时的一致使用和加载、设置/获取/发布/订阅等,jedis 返回了 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;
}
关于java - 绝地武士:破管,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27852900/