List shards = Arrays.asList(
new JedisShardInfo("localhost",6379),
new JedisShardInfo("localhost",6380));
ShardedJedis sharding = new ShardedJedis(shards);
long start = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
String result = sharding.set("sn" + i, "n" + i);
}
long end = System.currentTimeMillis();
ShardedJedis继承自Sharded
public class Sharded> {
public static final int DEFAULT_WEIGHT = 1;
private TreeMap nodes;
private final Map, R> resources = new LinkedHashMap, R>();
//....
}
假如我们的redis集群就像上面的代码,有两个实例,也就是说有两个JedisShardInfo
我们看下图:
ShardedJedis为每个redis实例做出了160个虚拟节点(也可是320或者480,这个权重可以设定,默认是160) private void initialize(List shards) {
nodes = new TreeMap();
for (int i = 0; i != shards.size(); ++i) {
final S shardInfo = shards.get(i);
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);
}
resources.put(shardInfo, shardInfo.createResource());
}
}
大家知道那320个节点的标识符的来源是什么了吧?反正结果就是一个long型的数字么