随笔 - 100  文章 - 50  trackbacks - 0
<2015年10月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

常用链接

留言簿(3)

随笔分类

随笔档案

文章分类

文章档案

收藏夹

我收藏的一些文章!

搜索

  •  

最新评论

阅读排行榜

评论排行榜

由于redis是单点,但是项目中不可避免的会使用多台Redis缓存服务器,那么怎么把缓存的Key均匀的映射到多台Redis服务器上,且随着缓存服务器的增加或减少时做到最小化的减少缓存Key的命中率呢?这样就需要我们自己实现分布式。

  Memcached对大家应该不陌生,通过把Key映射到Memcached Server上,实现快速读取。我们可以动态对其节点增加,并未影响之前已经映射到内存的Key与memcached Server之间的关系,这就是因为使用了一致性哈希。
因为Memcached的哈希策略是在其客户端实现的,因此不同的客户端实现也有区别,以Spymemcache、Xmemcache为例,都是使用了KETAMA作为其实现。

  因此,我们也可以使用一致性hash算法来解决Redis分布式这个问题。在介绍一致性hash算法之前,先介绍一下我之前想的一个方法,怎么把Key均匀的映射到多台Redis Server上。

  由于LZ水平有限且对Redis研究的不深,文中有写的不对的地方请指正。

方案一

该方案是前几天想的一个方法,主要思路是通过对缓存Key中的字母和数字的ascii码值求sum,该sum值对Redis Server总数取余得到的数字即为该Key映射到的Redis Server,该方法有一个很大的缺陷就是当Redis Server增加或减少时,基本上所有的Key都映射不到对应的的Redis Server了。代码如下:

复制代码
    /// <summary>         /// 根据缓存的Key映射对应的Server         /// </summary>         /// <param name="Key"></param>         /// <returns></returns>         public static RedisClient GetRedisClientByKey(string Key)         {             List<RedisClientInfo> RedisClientList = new List<RedisClientInfo>();             RedisClientList.Add(new RedisClientInfo() { Num = 0, IPPort = "127.0.0.1:6379" });             RedisClientList.Add(new RedisClientInfo() { Num = 1, IPPort = "127.0.0.1:9001" });              char[] charKey = Key.ToCharArray();             //记录Key中的所有字母与数字的ascii码和             int KeyNum = 0;             //记录余数             int Num = 0;             foreach (var c in charKey)             {                 if ((c >= 'a' && 'z' >= c) || (c >= 'A' && 'Z' >= c))                 {                     System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();                     KeyNum = KeyNum + (int)asciiEncoding.GetBytes(c.ToString())[0];                 }                 if (c >= '1' && '9' >= c)                 {                     KeyNum += Convert.ToInt32(c.ToString());                 }             }             Num = KeyNum % RedisClientList.Count;             return new RedisClient(RedisClientList.Where(it => it.Num == Num).First().IPPort);         }         //Redis客户端信息         public class RedisClientInfo         {             //Redis Server编号             public int Num { get; set; }             //Redis Server IP地址和端口号             public string IPPort { get; set; }         }
复制代码

方案二

1、分布式实现

通过key做一致性哈希,实现key对应redis结点的分布。

一致性哈希的实现:

  1. hash值计算:通过支持MD5与MurmurHash两种计算方式,默认是采用MurmurHash,高效的hash计算。
  2. 一致性的实现:通过java的TreeMap来模拟环状结构,实现均匀分布

什么也不多说了,直接上代码吧,LZ也是只知道点皮毛,代码中还有一些看不懂的地方,留着以后慢慢琢磨

复制代码
public class KetamaNodeLocator     {         //原文中的JAVA类TreeMap实现了Comparator方法,这里我图省事,直接用了net下的SortedList,其中Comparer接口方法)         private SortedList<long, string> ketamaNodes = new SortedList<long, string>();         private HashAlgorithm hashAlg;         private int numReps = 160;         //此处参数与JAVA版中有区别,因为使用的静态方法,所以不再传递HashAlgorithm alg参数         public KetamaNodeLocator(List<string> nodes/*,int nodeCopies*/)         {             ketamaNodes = new SortedList<long, string>();             //numReps = nodeCopies;             //对所有节点,生成nCopies个虚拟结点             foreach (string node in nodes)             {                 //每四个虚拟结点为一组                 for (int i = 0; i < numReps / 4; i++)                 {                     //getKeyForNode方法为这组虚拟结点得到惟一名称                      byte[] digest = HashAlgorithm.computeMd5(node + i);                     /** Md5是一个16字节长度的数组,将16字节的数组每四个字节一组,分别对应一个虚拟结点,这就是为什么上面把虚拟结点四个划分一组的原因*/                     for (int h = 0; h < 4; h++)                     {                         long m = HashAlgorithm.hash(digest, h);                         ketamaNodes[m] = node;                     }                 }             }         }         public string GetPrimary(string k)         {             byte[] digest = HashAlgorithm.computeMd5(k);             string rv = GetNodeForKey(HashAlgorithm.hash(digest, 0));             return rv;         }         string GetNodeForKey(long hash)         {             string rv;             long key = hash;             //如果找到这个节点,直接取节点,返回                if (!ketamaNodes.ContainsKey(key))             {                 //得到大于当前key的那个子Map,然后从中取出第一个key,就是大于且离它最近的那个key 说明详见: http://www.javaeye.com/topic/684087                 var tailMap = from coll in ketamaNodes                               where coll.Key > hash                               select new { coll.Key };                 if (tailMap == null || tailMap.Count() == 0)                     key = ketamaNodes.FirstOrDefault().Key;                 else                     key = tailMap.FirstOrDefault().Key;             }             rv = ketamaNodes[key];             return rv;         }     }     public class HashAlgorithm     {         public static long hash(byte[] digest, int nTime)         {             long rv = ((long)(digest[3 + nTime * 4] & 0xFF) << 24)                     | ((long)(digest[2 + nTime * 4] & 0xFF) << 16)                     | ((long)(digest[1 + nTime * 4] & 0xFF) << 8)                     | ((long)digest[0 + nTime * 4] & 0xFF);             return rv & 0xffffffffL; /* Truncate to 32-bits */         }         /**          * Get the md5 of the given key.          */         public static byte[] computeMd5(string k)         {             MD5 md5 = new MD5CryptoServiceProvider();              byte[] keyBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(k));             md5.Clear();             //md5.update(keyBytes);             //return md5.digest();             return keyBytes;         }     }
复制代码

2、分布式测试

1、假设有两个server:0001和0002,循环调用10次看看Key值能不能均匀的映射到server上,代码如下:

复制代码
    static void Main(string[] args)         {             //假设的server             List<string> nodes = new List<string>() { "0001","0002" };             KetamaNodeLocator k = new KetamaNodeLocator(nodes);             string str = "";             for (int i = 0; i < 10; i++)             {                 string Key="user_" + i;                 str += string.Format("Key:{0}分配到的Server为:{1}\n\n", Key, k.GetPrimary(Key));             }                          Console.WriteLine(str);                         Console.ReadLine();                       }
复制代码

程序运行两次的结果如下,发现Key基本上均匀的分配到Server节点上了。

2、我们在添加一个0003的server节点,代码如下:

复制代码
  static void Main(string[] args)         {             //假设的server             List<string> nodes = new List<string>() { "0001","0002" ,"0003"};             KetamaNodeLocator k = new KetamaNodeLocator(nodes);             string str = "";             for (int i = 0; i < 10; i++)             {                 string Key="user_" + i;                 str += string.Format("Key:{0}分配到的Server为:{1}\n\n", Key, k.GetPrimary(Key));             }                          Console.WriteLine(str);                         Console.ReadLine();                       }
复制代码

程序运行两次的结果如下:

对比第一次的运行结果发现只有user_5,user_7,user_9的缓存丢失,其他的缓存还可以命中。

3、我们去掉server 0002,运行两次的结果如下:

对比第二次和本次运行结果发现 user_0,user_1,user_6 缓存丢失。

结论

通过一致性hash算法可以很好的解决Redis分布式的问题,且当Redis server增加或减少的时候,之前存储的缓存命中率还是比较高的。

http://www.cnblogs.com/lc-chenlong/p/4194150.html
http://www.cnblogs.com/lc-chenlong/p/4195033.html
http://www.cnblogs.com/lc-chenlong/p/3218157.html

本文参考

1、http://blog.csdn.net/chen77716/article/details/5949166

2、http://www.cr173.com/html/6474_2.html


转:http://www.cnblogs.com/lc-chenlong/p/4195814.html?utm_source=tuicool&utm_medium=referral



posted on 2015-10-20 15:51 fly 阅读(301) 评论(0)  编辑  收藏 所属分类: J2EE-负载均衡

只有注册用户登录后才能发表评论。


网站导航: