首页 > 编程语言 > 详细

负载均衡算法(四)IP Hash负载均衡算法

时间:2016-08-02 16:38:49      阅读:349      评论:0      收藏:0      [点我收藏+]
    /// <summary>
    /// IP Hash负载均衡算法
    /// </summary>
    public static class IpHash
    {
        static Dictionary<string, int> dic = new Dictionary<string, int>
        {
            { "192.168.1.12", 1},
            {"192.168.1.13", 1 },
            { "192.168.1.14", 3},
            { "192.168.1.15", 1},
            {"192.168.1.16", 1},
            {"192.168.1.17", 1 },
            { "192.168.1.18", 1},
            { "192.168.1.19", 1}
        };

        public static string ipHash(string remoteIp)
        {
            List<string> keys = dic.Keys.ToList();

            int hashCode = Math.Abs(remoteIp.GetHashCode());
            int serverListSize = keys.Count;
            int serverPos = hashCode % serverListSize;

            return keys[serverPos];
        }

    }

 

负载均衡算法(四)IP Hash负载均衡算法

原文:http://www.cnblogs.com/liyangLife/p/5729394.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!