解决Redis cluster的jedis驱动在高并发下的拥塞问题

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);
  }

 

测试后比较处理比较平稳,未出现某一个访问耗时非常长得的情况。

 

 

 

 

本文出自 “天涯时空” 博客,请务必保留此出处http://leshjmail.blog.51cto.com/629172/1784716

文章来自:http://leshjmail.blog.51cto.com/629172/1784716
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3