ShardedJedisPool xml配置:
xml配置对应的构造方法:
public class ShardedJedisPool extends Pool{ public ShardedJedisPool(final GenericObjectPoolConfig poolConfig, List shards) { this(poolConfig, shards, Hashing.MURMUR_HASH); }}
ShardedJedisPool使用示例:
ShardedJedisPool pool = ctx.getBean(ShardedJedisPool.class);ShardedJedis jedis = pool.getResource();String zhang = jedis.get("zhang");jedis.close();
ShardedJedisPool.getResource 的调用栈:
代码分析:
//类 redis.clients.util.Sharded>private final Map , R> resources = new LinkedHashMap , R>();private void initialize(List shards) { nodes = new TreeMap(); //shards是xml中配置的redis.clients.jedis.JedisShardInfo的list for (int i = 0; i != shards.size(); ++i) { final S shardInfo = shards.get(i); //没有为JedisShardInfo设置name,所以执行if分支,weight默认为1 //取 SHARD-0-NODE-0 ~ SHARD-0-NODE-159 哈希值 //取 SHARD-1-NODE-0 ~ SHARD-1-NODE-159 哈希值 //取 SHARD-2-NODE-0 ~ SHARD-2-NODE-159 哈希值 //把(哈希值->JedisShardInfo)键值对放入nodes中 if (shardInfo.getName() == null) for (int n = 0; n < 160 * shardInfo.getWeight(); n++) { nodes.put(this.algo.hash("SHARD-" + i + "-NODE-" + n), shardInfo); } else for (int n = 0; n < 160 * shardInfo.getWeight(); n++) { nodes.put(this.algo.hash(shardInfo.getName() + "*" + shardInfo.getWeight() + n), shardInfo); } //添加到map中,键为JedisShardInfo,值为Jedis resources.put(shardInfo, shardInfo.createResource()); }}
JedisShardInfo.createResource:
//省略其他代码public class JedisShardInfo extends ShardInfo{ @Override public Jedis createResource() { return new Jedis(this); }}
存取一个键时,根据键获取一个JedisShardInfo,以get为例:
public S getShardInfo(byte[] key) { //nodes是TreeMap,由红黑树实现,是按键值排好序的map,默认为升序 //假定 algo.hash(key) 的值为10086,该行代码是取出键大于10086的所有键值对 SortedMaptail = nodes.tailMap(algo.hash(key)); //如果不存在,则取第一个键值对的JedisShardInfo if (tail.isEmpty()) { return nodes.get(nodes.firstKey()); } //如果存在这样的键值对,则返回第一个键值对中的JedisShardInfo return tail.get(tail.firstKey()); }
Sharded类中有2个map,TreeMap<Long, S> nodes和Map<ShardInfo<R>, R> resources,
运行时具体是:TreeMap<Long, JedisShardInfo>和Map<JedisShardInfo, Jedis>。
分析以上代码可知,首先计算出 "SHARD-i-NODE-n" 的哈希值,预先生成一颗红黑树,即填充nodes。
当存取键值对时,计算键的哈希值,然后从红黑树上摘下比该值大的第一个节点上的JedisShardInfo,随后从resources取出Jedis。
假定有红黑树如下: