負載均衡算法(四)IP Hash負載均衡算法


    /// <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];
        }

    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM