哈希一致性算法实现思路

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HashDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //list.key虚拟节点 list.value实际节点
            Dictionary<string, string> list = new Dictionary<string, string>();
            list.Add("192.168.1.1", "192.168.1.1");
            list.Add("192.168.1.2", "192.168.1.2");
            list.Add("192.168.1.3", "192.168.1.3");
            list.Add("192.168.1.4", "192.168.1.4");
            list.Add("192.168.1.5", "192.168.1.5");
            list.Add("192.168.1.6", "192.168.1.1");
            list.Add("192.168.1.7", "192.168.1.2");
            list.Add("192.168.1.8", "192.168.1.3");
            list.Add("192.168.1.9", "192.168.1.4");
            list.Add("192.168.1.10", "192.168.1.5");

            //节点的hashcode和节点的实际地址
            SortedDictionary<int, string> dic = new SortedDictionary<int, string>();
            foreach (var item in list)
            {
                dic.Add(item.Key.GetHashCode(), item.Value);
            }

            //请求的缓存
            List<string> checheKey = new List<string>() { "enterprise456", "project123", "3", "4", "5", "6", "7", "8", "900000" };
            //ip分配的节点值(测试看)
            Dictionary<string, string> result = new Dictionary<string, string>();
            //请求的缓存随机分配
            foreach (var item2 in checheKey)
            {
                var key1 = item2;
                foreach (var item in dic)
                {
                    if (item.Key >= key1.GetHashCode())
                    {
                        string value;
                        if (result.TryGetValue(item.Value, out value))
                        {
                            result[item.Value] = result[item.Value] + "," + key1;
                        }
                        else
                        {
                            result.Add(item.Value, key1);
                        }
                        break;
                    }
                }
            }
           
            foreach (var item in result)
            {
                Console.WriteLine("key:{0} value:{1}", item.Key, item.Value);
            }

            Console.ReadLine();
        }

    }
}

源码下载http://pan.baidu.com/s/1nvfpFsd