在Redis集群中,会有很多个分片,如果此时利用Jedis来操作此Redis集群,那么他会把数据路由到不到的分片上。而且如果动态的往集群中增加分片,也不会影响Jedis的功能。究竟是怎么做到的呢?

由于最近公司要集中迁移redis集群,也就是把旧集群的数据迁移到Redis Cluster中,就需要我们自己来整理数据。刚好我这里有个库存热点数据,我们叫做A吧,这个A在Redis集群中,每个分片上都有数据。比如,Redis集群有4个片,而我总库存量为1000,那么会在4个片上放上Key A,每个A中库存量为250.

新的Redis cluster,为了防止热点库存问题,也就申请了4个cluster,每个cluster相当于之前的一个分片。数据在写入的时候,每个cluster会写一个key A,库存量为250.

现在问题来了,有一些Key,比如说B,在原来的Redis集群中,是利用Jedis路由来导向某一片写入的,假设这里写入的是第3片。如果迁移到新的4个Redis cluster中,势必需要写到第三个Redis Cluster中。因为数据迁移,肯定是第一片redis数据迁移到第一个Cluster,第二片redis数据迁移到第二个Cluster,以此类推。

所以这里需要我们来实现和Jedis一样的路由算法,按照Jedis提供的类,我们实现如下:

首先,定义好ShardNode,继承自Jedis的ShardInfo:

public class ShardNode implements ShardInfo {

    public ShardNode(String index, Cluster cluster) {
this.index = index;
this.cluster = cluster;
} private String index; private Cluster cluster; @Override
public int getWeight() {
return 1;
} @Override
public String getName() {
return null;
} public Cluster getCluster() {
return cluster;
} public void setCluster(Cluster cluster) {
this.cluster = cluster;
} public String getIndex() {
return index;
} public void setIndex(String index) {
this.index = index;
}
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

注意,getName方法里面一定要return null, 这样就会根据配置的分片的数量先后顺序来运算哈希key。具体源码如下(翻看KetamaHashing类的源码):

    protected void initialize(List<T> shards) {
Charset charset = Charset.forName("utf-8");
String key = null; for(int i = 0; i < shards.size(); ++i) {
T shardInfo = (ShardInfo)shards.get(i);
if (shardInfo == null) {
throw new IllegalArgumentException("shard element #" + i + " is null.");
} AtomicReference<T> wrapper = new AtomicReference(shardInfo);
this.originals.add(wrapper); for(int n = 0; n < 160 * shardInfo.getWeight(); ++n) {
if (shardInfo.getName() == null) {
key = "SHARD-" + i + "-NODE-" + n;
} else {
key = shardInfo.getName() + "*" + shardInfo.getWeight() + n;
} this.nodes.put(this.algo.hash(key.getBytes(charset)), wrapper);
}
} }
 

注意我标黄颜色部分,正式因为shardInfo.getName为null,所以我们的路由算法才能够按照配置的分片顺序进行路由。
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

然后进行实现即可:

 public Cluster getCluster(String key) {
KetamaHashing ketamaHashing = new KetamaHashing(shardNodes, new MurmurHash());
ShardNode shard = (ShardNode) ketamaHashing.getShardInfo(key.getBytes());
return shard.getCluster();
}

这样我们就可以通过key来获取新的Redis Cluster实例了。通过测试用例结果,我们也可以看出Jedis路由算法和我们所写的路由算法是一致的。

 @Test
public void testJedisHash() { Map<String, String> map = new HashMap<>();
map.put("192.168.155.84:6379", "0");
map.put("192.168.155.84:6390", "1");
map.put("192.168.155.85:6379", "2");
map.put("192.168.155.85:6390", "3"); String key;
List<String> list = new ArrayList<>(); for (int i = 0; i < 1000; i++) { key = UUID.randomUUID().toString(); ShardNode cluster = storeJimdbs.getShardNode(key); ShardInfo redisShard = jrc.getShardInfo(key);
String shardKey = redisShard.toString().split("/")[0]; if (cluster.getIndex().equals(map.get(shardKey))) {
list.add("OK");
} else {
System.out.println("NNNNNNNotMatch!!!!!!!");
}
} System.out.println(list.size());
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

最后运算出来的结果是1000,也就是说1000个随机key,利用Jedis路由算法操作,利用我们写的路由算法操作,其实是打到相同的分片序号上的。

也就是0号分片对应0号cluster。

Jedis路由key的算法剥离的更多相关文章

  1. 基于SNMP的路由拓扑发现算法收集

    一.三层(网络层)发现 算法来源:王娟娟.基于SNMP的网络拓扑发现算法研究.武汉科技大学硕士学位论文,2008 数据结构: 待检路由设备网关链表:存放指定深度内待检路由设备的网关信息,处理后删除. ...

  2. key寻址算法

    分布式寻址算法 hash 算法(大量缓存重建) 一致性 hash 算法(自动缓存迁移)+ 虚拟节点(自动负载均衡) redis cluster 的 hash slot 算法 hash 算法 来了一个 ...

  3. 用Jedis操作redis示例一,Key,value HashMap

    简单的key,value形式 public class RedisDemo { public static void main(String[] args) { Jedis jedis = new J ...

  4. jedis连接池详解(Redis)

    转自:http://tianxingzhe.blog.51cto.com/3390077/1684306 原子性(atomicity): 一个事务是一个不可分割的最小工作单位,事务中包括的诸操作要么都 ...

  5. 超大批量删除redis中无用key+配置

    目前线上一个单实例redis中无用的key太多,决定删除一部分. 1.删除指定用户的key,使用redis的pipeline 根据一定条件把需要删除的用户统计出来,放到一个表里面,表为 del_use ...

  6. _00013 一致性哈希算法 Consistent Hashing 新的讨论,并出现相应的解决

    笔者博文:妳那伊抹微笑 博客地址:http://blog.csdn.net/u012185296 个性签名:世界上最遥远的距离不是天涯,也不是海角,而是我站在妳的面前.妳却感觉不到我的存在 技术方向: ...

  7. Jedis使用总结【pipeline】【分布式的id生成器】【分布式锁【watch】【multi】】【redis分布式】(转)

    前段时间细节的了解了Jedis的使用,Jedis是redis的java版本的客户端实现.本文做个总结,主要分享如下内容: [pipeline][分布式的id生成器][分布式锁[watch][multi ...

  8. RabbitMQ之路由(Routing)【译】

    在上一节中,我们创建了一个简单的日志系统,可以广播消息到很多接收者. 这一节,我们将在上一节的基础上加一个功能--订阅部分消息.例如,我们只将严重错误信息写入到日志文件保存在磁盘上,同时我们能将所有的 ...

  9. 一致性hash与CRUSH算法总结

    相同之处:都解决了数据缓存系统中数据如何存储与路由. 不同之处:区别在于虚拟节点和物理节点的映射办法不同 由于一般的哈希函数返回一个int(32bit)型的hashCode.因此,可以将该哈希函数能够 ...

随机推荐

  1. Dubbo的异常处理

    记一次Dubbo的异常处理过程. 现象:业务团队报送,服务端定义一个BuinessException,继承与RunTimeException,服务端执行时抛出该异常,但是客户端捕捉不到该异常. 记录: ...

  2. CAN自收自发问题小结

    2011-12-02 21:59:23 流程图: CAN自收自发问题小结 1,地址如何确定?  答:51的片外扩展地址,R/W脚的电平会根据代码自动更改,不需要设置.    参考 单片机的外部存储器的 ...

  3. How to using Piwis Tester II code Porsche rear end electronics

    V18.100 Piwis Tester II Diagnostic Tool For Porsche With CF30 Laptop High Quality Top 7 Reasons to G ...

  4. 51nod 1009 数字1的数量(数位dp模板)

    给定一个十进制正整数N,写下从1开始,到N的所有正数,计算出其中出现所有1的个数. 例如:n = 12,包含了5个1.1,10,12共包含3个1,11包含2个1,总共5个1.   数位dp的模板题   ...

  5. Java7/8 中的 HashMap 和 ConcurrentHashMap 全解析

    Java7/8 中的 HashMap 和 ConcurrentHashMap 全解析 今天发一篇”水文”,可能很多读者都会表示不理解,不过我想把它作为并发序列文章中不可缺少的一块来介绍.本来以为花不了 ...

  6. 单例模式-懒汉式的一次多线程Debug

    单例模式要要点就是一个类只会存在一个实例,要想达到这种效果,最重要的就是将构造方法设置为私有,然后通过static的方法来获取对象. 上述设计并不线程安全,因为在lazySingleton = new ...

  7. ECS之Git服务器搭建

    最简教程 ### . 安装Git 安装Git服务,命令如下: ```Shell $ yum install curl-devel expat-devel gettext-devel openssl-d ...

  8. ERROR: Got error reading packet from server: A slave with the same server_uuid/server_id as this slave has connected to the master

    centos7.5 做binlog-server,拉取主库binlog报错 问题: [root@db03-53 binlog]# mysqlbinlog -R --host=10.0.0.55 --u ...

  9. 浅谈RESTful

    浅谈RESTful 什么是RESTful? REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次出现在2000年Ro ...

  10. 转载redis持久化的几种方式

    redis持久化的几种方式 1.前言 Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服 ...