负载均衡的基本算法

负载均衡的基本算法,主要有以下几种(参考F5产品):

  • 随机:负载均衡方法随机的把负载分配到各个可用的服务器上,通过随机数生成算法选取一个服务器,然后把连接发送给它。虽然许多均衡产品都支持该算法,但是它的有效性一直受到质疑,除非把服务器的可运行时间看的很重。
  • 轮询:轮询算法按顺序把每个新的连接请求分配给下一个服务器,最终把所有请求平分给所有的服务器。轮询算法在大多数情况下都工作的不错,但是如果负载均衡的设备在处理速度、连接速度和内存等方面不是完全均等,那么效果会更好。
  • 加权轮询:该算法中,每个机器接受的连接数量是按权重比例分配的。这是对普通轮询算法的改进,比如你可以设定:第三台机器的处理能力是第一台机器的两倍,那么负载均衡器会把两倍的连接数量分配给第3台机器。
  • 动态轮询:类似于加权轮询,但是,权重值基于对各个服务器的持续监控,并且不断更新。这是一个动态负载均衡算法,基于服务器的实时性能分析分配连接,比如每个节点的当前连接数或者节点的最快响应时间等。
  • 最快算法:最快算法基于所有服务器中的最快响应时间分配连接。该算法在服务器跨不同网络的环境中特别有用。
  • 最少连接:系统把新连接分配给当前连接数目最少的服务器。该算法在各个服务器运算能力基本相似的环境中非常有效。
  • 观察算法:该算法同时利用最小连接算法和最快算法来实施负载均衡。服务器根据当前的连接数和响应时间得到一个分数,分数较高代表性能较好,会得到更多的连接。
  • 预判算法:该算法使用观察算法来计算分数,但是预判算法会分析分数的变化趋势来判断某台服务器的性能正在改善还是降低。具有改善趋势的服务器会得到更多的连接。该算法适用于大多数环境。

性能调优社区dynatrace在其博客中分享了客户案例,电商网站在假日客流峰值期间数次崩溃,经过SQL优化和调整负载均衡算法解决了相关问题.首先要分析执行最慢的数据库语句,并做性能优化,比如增加索引等。同时也优化了连接池大小来满足高峰时刻的需求。然后,企业把负载均衡器的算法从Round-Robin改为了Least-Busy。

相关文章:

电商网站的宕机案例分析

ASP.NET Session State Partitioning

ASP.NET Session State Partitioning using State Server Load Balancing

解析nginx负载均衡

Round Robin Scheduling

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.CompilerServices;

using System.Text;



namespace LoadBalancer

{

    public class LoadBalance

    {

        /// <summary>

        /// 锁对象

        /// </summary>

        private static readonly object locker = new object();



        /// <summary>

        /// 服务器权重列表

        /// </summary>

        private static List<int> weightList = new List<int>();



        /// <summary>

        /// 当前索引

        /// </summary>

        private static int currentIndex;



        /// <summary>

        /// 当前权重

        /// </summary>

        private static int currentWeight;



        private static int maxWeight;



        /// <summary>

        /// 最大公约数

        /// </summary>

        private static int gcd;



        static LoadBalance()

        {

            currentIndex = -1;

            currentWeight = 0;



            //获取服务器权重列表,从配置文件

            weightList = GetWeightList();

            maxWeight = GetMaxWeight(weightList);

            gcd = GetMaxGCD(weightList);

        }



        private static List<int> GetWeightList()

        {

            List<int> list = new List<int>();

            list.Add(3);

            list.Add(1);

            list.Add(1);

            list.Add(4);

            list.Add(1);

            list.Add(7);



            return list;

        }



        [MethodImpl(MethodImplOptions.Synchronized)]

        public static int Start()

        {

            lock (locker)

            {

                int? iWeight = RoundRobin();

                if (iWeight != null)

                {

                    return (int)iWeight;

                }

                return weightList[0];

            }

        }





        /// <summary>

        /// 获取最大公约数

        /// </summary>

        /// <param name="list">要查找的int集合</param>

        /// <returns>返回集合中所有数的最大公约数</returns>

        private static int GetMaxGCD(List<int> list)

        {

            list.Sort(new WeightCompare());



            int iMinWeight = weightList[0];



            int gcd = 1;



            for (int i = 1; i < iMinWeight; i++)

            {

                bool isFound = true;

                foreach (int iWeight in list)

                {

                    if (iWeight % i != 0)

                    {

                        isFound = false;

                        break;

                    }

                }

                if (isFound) gcd = i;

            }

            return gcd;

        }





        /// <summary>

        /// 获取服务器权重集合中的最大权重

        /// </summary>

        /// <param name="list"></param>

        /// <returns></returns>

        private static int GetMaxWeight(List<int> list)

        {

            int iMaxWeight = 0;

            foreach (int i in list)

            {

                if (iMaxWeight < i) iMaxWeight = i;

            }

            return iMaxWeight;

        }



        private static int? RoundRobin()

        {

            while (true)

            {

                currentIndex = (currentIndex + 1) % weightList.Count;

                if (0 == currentIndex)

                {

                    currentWeight = currentWeight - gcd;

                    if (0 >= currentWeight)

                    {

                        currentWeight = maxWeight;

                        if (currentWeight == 0) return null;

                    }

                }



                if (weightList[currentIndex] >= currentWeight)

                {

                    return weightList[currentIndex];

                }

            }

        }



    }



    public class WeightCompare : IComparer<int>

    {

        public int Compare(int x, int y)

        {

            return x - y;

        }

    }



}

你可能感兴趣的:(负载均衡)