• 哈希一致性算法实现思路


    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

    学习永不止境,技术成就梦想。
  • 相关阅读:
    harbor install & docker-compose
    ngx安装 (转)
    docker对镜像自动重启的设置
    docker安装脚本
    sql中的递归拼接
    【HC89S003F4开发板】 4端口消抖
    【HC89S003F4开发板】 6crc校验
    【HC89S003F4开发板】 3串口调试
    【HC89S003F4开发板】 1环境搭建
    用mkdocs在gitee码云上建立一个简单的文档博客
  • 原文地址:https://www.cnblogs.com/zd1994/p/6780335.html
Copyright © 2020-2023  润新知