Jedis客户端分片机制
所属分类 redis
浏览量 1397
启动2个Redis 实例
nohup redis-server --port 6379 &
nohup redis-server --port 6380 &
默认端口 6379
一致性hash算法 虚拟节点数 160
hash算法 Hashing.MURMUR_HASH
redis-server -h
Usage: ./redis-server [/path/to/redis.conf] [options]
./redis-server - (read config from stdin)
./redis-server -v or --version
./redis-server -h or --help
./redis-server --test-memory
Examples:
./redis-server (run the server with default conf)
./redis-server /etc/redis/6379.conf
./redis-server --port 7777
./redis-server --port 7777 --replicaof 127.0.0.1 8888
./redis-server /etc/myredis.conf --loglevel verbose
Sentinel mode:
./redis-server /etc/sentinel.conf --sentinel
JedisPoolConfig config =new JedisPoolConfig();
config.setTestOnBorrow(true);
List shardInfoList =new ArrayList();
JedisShardInfo infoA = new JedisShardInfo("127.0.0.1", 6379);
JedisShardInfo infoB = new JedisShardInfo("127.0.0.1", 6380);
shardInfoList.add(infoA);
shardInfoList.add(infoB);
ShardedJedisPool pool = new ShardedJedisPool(config,shardInfoList);
jedis = pool.getResource();
final int NUM = 10;
for(int i=0;i tail = nodes.tailMap(algo.hash(key));
if (tail.isEmpty()) {
return nodes.get(nodes.firstKey());
}
return tail.get(tail.firstKey());
}
TreeMap nodes
Hashing algo
redis.clients.jedis.util.MurmurHash
TreeMap 的 tailMap() firstKey() 方法使用
public SortedMap tailMap(K fromKey)
返回 key 大于或等于 fromKey的部分视图
public SortedMap headMap(K toKey)
返回 key 小于 toKey 的部分视图
public SortedMap subMap(K fromKey,K toKey)
返回 key 从fromKey(包括)到toKey(不包括)的部分视图
key对应的分片信息
key=client_shard_test_key_0,shardInfo=127.0.0.1:6380*1
key=client_shard_test_key_1,shardInfo=127.0.0.1:6380*1
key=client_shard_test_key_2,shardInfo=127.0.0.1:6380*1
key=client_shard_test_key_3,shardInfo=127.0.0.1:6379*1
key=client_shard_test_key_4,shardInfo=127.0.0.1:6380*1
key=client_shard_test_key_5,shardInfo=127.0.0.1:6380*1
key=client_shard_test_key_6,shardInfo=127.0.0.1:6380*1
key=client_shard_test_key_7,shardInfo=127.0.0.1:6379*1
key=client_shard_test_key_8,shardInfo=127.0.0.1:6379*1
key=client_shard_test_key_9,shardInfo=127.0.0.1:6380*1
key数量太少 , 看起来不是很均匀
分片信息初始化 构建hash环
public Sharded(List shards, Pattern tagPattern) {
this(shards, Hashing.MURMUR_HASH, tagPattern); // MD5 is really not good
// as we works with
// 64-bits not 128
}
public Sharded(List shards, Hashing algo, Pattern tagPattern) {
this.algo = algo;
this.tagPattern = tagPattern;
initialize(shards);
}
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() + "*" + n), shardInfo);
}
resources.put(shardInfo, shardInfo.createResource());
}
}
上一篇
下一篇
B+树 与 B树 索引
B树与红黑树,为什么数据库使用B树索引
联合索引与最左匹配原则
一致性hash与treemap
MapReduce和Tez比较
redis 主从 哨兵 集群机制