redis cluster发布后我们项目中使用了cluster,使用驱动是jedis,但是在压力测试过程中发现有一定数量的redis访问非常缓慢高达几十秒数分钟,经过分析jedis驱动JedisClusterInfoCache中加锁造成
private Map<String, JedisPool> nodes = new HashMap<String, JedisPool>();
private Map<Integer, JedisPool> slots = new HashMap<Integer, JedisPool>();
public JedisPool getNode(String nodeKey) {
r.lock();
try {
return nodes.get(nodeKey);
} finally {
r.unlock();
}
}
public JedisPool getSlotPool(int slot) {
r.lock();
try {
return slots.get(slot);
} finally {
r.unlock();
}
}
将map替换成ConcurrentHashMap
private Map<String, JedisPool> nodes = new ConcurrentHashMap<String, JedisPool>();
private Map<Integer, JedisPool> slots = new ConcurrentHashMap<Integer, JedisPool>();
去除锁
public JedisPool getNode(String nodeKey) {
return nodes.get(nodeKey);
}
public JedisPool getSlotPool(int slot) {
return slots.get(slot);
}
测试后比较处理比较平稳,未出现某一个访问耗时非常长得的情况。
另外2.8版本的驱动刷新也偶尔会出问题,需要在刷新中加入判断,如果在更新则跳过更新动作;否则会导致卡顿,很难恢复。
以上问题在2.9版本的驱动中已经解决;可以更新2.9版本驱动来解决问题;无需修改源代码
转载于:https://blog.51cto.com/leshjmail/1784716