ShardedJedis部分源码 redis分布式直连同步方式
来源:互联网 发布:水暖毯和电热毯 知乎 编辑:程序博客网 时间:2024/04/20 20:53
ShardedJedis redis分布式直连同步
==============================================================
Sharded类中4个属性,2个重要方法:
4个属性
public static final int DEFAULT_WEIGHT = 1;//权重private final Hashing algo;//哈希private TreeMap<Long, S> nodes;//虚拟节点private final Map<ShardInfo<R>, R> resources = new LinkedHashMap<ShardInfo<R>, R>();//真实节点两个重要方法
1.new JedisShardInfo()时调用,算是初始化
private void initialize(List<S> shards) { nodes = new TreeMap<Long, S>(); 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());//shardInfo.createResource() = new Jedis() } }160为虚拟节点基数,shardInfo.getWeight()权重越大,虚拟节点数越多,越容易存取到该节点
nodes和resources结合使用,找到虚拟节点对应的真实节点
2.get()/set()方法时调用,获取存取数据到的具体Jedis
public R getShard(String key) { return resources.get(getShardInfo(key)); } public S getShardInfo(byte[] key) { SortedMap<Long, S> tail = nodes.tailMap(algo.hash(key)); if (tail.isEmpty()) { return nodes.get(nodes.firstKey()); } return tail.get(tail.firstKey()); }TreeMap、nodes.tailMap()、SortedMap
实现一致性哈希旋转概念(找到最近的节点)
ShardedJedis.java
-----------------------------------------------------
public String set(String key, String value) { Jedis j = getShard(key); return j.set(key, value); }public String get(String key) { Jedis j = getShard(key); return j.get(key); }......-----------------------------------------------------
BinaryShardedJedis.java
-----------------------------------------------------
public String set(byte[] key, byte[] value) { Jedis j = getShard(key); return j.set(key, value); }public byte[] get(byte[] key) { Jedis j = getShard(key); return j.get(key); }......-----------------------------------------------------
TreeMap、treeMap.tailMap()、SortedMap示例:
public static void main(String[] args) {// creating mapsTreeMap<Integer, String> treemap = new TreeMap<Integer, String>();// populating tree maptreemap.put(2, "two");treemap.put(1, "one");treemap.put(3, "three");treemap.put(6, "six");treemap.put(5, "five");SortedMap<Integer, String> treemapincl = treemap.tailMap(3);//该方法调用返回此映射,其键大于或等于fromKey(这里是3)的值System.out.println("Tail map values: " + treemapincl);// 执行结果: Tail map values: {3=three, 5=five, 6=six}}
0 0
- ShardedJedis部分源码 redis分布式直连同步方式
- 分布式缓存Redis之ShardedJedis
- Spring+Redis+ShardedJedis
- Jedis源码解析(ShardedJedis)
- 连同
- redis网络部分源码分析
- jedis源码中ShardedJedis实现sharding
- jedis源码中ShardedJedis实现sharding
- Jedis下的ShardedJedis(分布式)使用方法(一)
- Jedis下的ShardedJedis(分布式)使用方法(二)
- Jedis下的ShardedJedis(分布式)使用方法(一)
- Jedis下的ShardedJedis(分布式)使用方法(二)
- Jedis下的ShardedJedis(分布式)使用方法(一)
- Jedis下的ShardedJedis(分布式)使用方法(二)
- scrapy-redis 分布式爬取源码分析
- 基于Redis的分布式锁实现方式
- Redis分布式锁的正确实现方式
- 注解的方式实现redis分布式锁
- Spring JMS
- iOS 定位原理和使用建议
- Spring OXM
- 【DoTween】Unity插件DOTween教程(零)基础篇
- tomcat无法启动
- ShardedJedis部分源码 redis分布式直连同步方式
- 系统集成项目管理工程师—案例分析重点记忆内容
- iOS容易造成循环引用的三种场景,(循环引用),内存泄漏
- HTML&CSS基础学习
- ln软硬链接
- linux shell脚本案例六:结构化程序分支语句
- Git .gitignore配置
- oracl数据迁移到mysql中
- iOS与H5交互 以及问题记录