三种常用的负载均衡算法C#(随机、轮询、权重)

入参实体:

    internal class ServiceCenterModel
    {
        public string Url { get; set; }
        public string[] ServiceTags { get; set; }
    }

1、随机

 /// 
    /// 随机
    /// 
    internal class RandomAlgorithm
    {
        private readonly static Random random = new Random();
        public static string Get(List serviceList,string serviceName)
        {
            if (serviceList == null)
                return null;
            if(serviceList.Count == 1)
                return serviceList[0].Url;

            int index = random.Next(serviceList.Count);

            string url = serviceList[index].Url;

            Console.WriteLine(serviceName + "+++" + url);
            return url;
        }
    }

2、轮询


    /// 
    /// 轮询
    /// 
    internal class PollingAlgorithm
    {
        private static Dictionary _serviceDic=new Dictionary(); 
        private static SpinLock _spinLock = new SpinLock();
        public static string Get(List serviceList, string serviceName)
        {
            if (serviceList == null || string.IsNullOrEmpty(serviceName))
                return null;
            if (serviceList.Count == 1)
                return serviceList[0].Url;

            bool locked = false;
            _spinLock.Enter(ref locked);//获取锁

            int index = -1;
            if (!_serviceDic.ContainsKey(serviceName))
                _serviceDic.TryAdd(serviceName, index);
            else
                _serviceDic.TryGetValue(serviceName,out index);

            string url = string.Empty;
            ++index;
            if (index > serviceList.Count - 1) //当前索引 > 最新服务最大索引
            {
                index = 0;
                url = serviceList[0].Url;
            }
            else {
                url = serviceList[index].Url;
            }
            _serviceDic[serviceName] = index;

            Console.WriteLine(serviceName+":" + url);
            if (locked) //释放锁
                _spinLock.Exit();
            return url;
        }
    }

3、权重

    /// 
    /// 权重
    /// 
    internal class WeightAlgorithm
    {
        private static ConcurrentDictionary _serviceDic = new ConcurrentDictionary();
        private static SpinLock _spinLock = new SpinLock();
        public static string Get(List serviceList, string serviceName)
        {
            if (serviceList == null)
                return null;
            if (serviceList.Count == 1)
                return serviceList[0].Url;

            bool locked = false;
            _spinLock.Enter(ref locked);//获取锁

            WeightAlgorithmItem weightAlgorithmItem = null;
            if (!_serviceDic.ContainsKey(serviceName))
            {
                weightAlgorithmItem = new WeightAlgorithmItem()
                {
                    Index = -1,
                    Urls = new List()
                };
                BuildWeightAlgorithmItem(weightAlgorithmItem, serviceList);
                _serviceDic.TryAdd(serviceName, weightAlgorithmItem);
            }
            else
            {
                _serviceDic.TryGetValue(serviceName,out weightAlgorithmItem);
                weightAlgorithmItem.Urls.Clear();
                BuildWeightAlgorithmItem(weightAlgorithmItem, serviceList);
            }

            string url = string.Empty;
            ++weightAlgorithmItem.Index;
            if (weightAlgorithmItem.Index > weightAlgorithmItem.Urls.Count - 1) //当前索引 > 最新服务最大索引
            {
                weightAlgorithmItem.Index = 0;
                url = serviceList[0].Url;
            }
            else
            {
                url = weightAlgorithmItem.Urls[weightAlgorithmItem.Index];
            }
            _serviceDic[serviceName] = weightAlgorithmItem;

            Console.WriteLine(serviceName + "-----" + url);
            if (locked) //释放锁
                _spinLock.Exit();
            return url;
        }

        private static void BuildWeightAlgorithmItem(WeightAlgorithmItem weightAlgorithmItem, List serviceList)
        {
            serviceList.ForEach(service => //有几个权重就加几个实例
            {
                int weight = 1;
                if (service.ServiceTags != null && service.ServiceTags.Length > 0)
                {
                    int.TryParse(service.ServiceTags[0], out weight);//获取权重值
                }
                for (int i = 0; i < weight; i++)
                {
                    weightAlgorithmItem.Urls.Add(service.Url);
                }
            });
        }
    }

    internal class WeightAlgorithmItem
    {
        public List Urls { get; set; }
        public int Index { get; set; }
    }

 

你可能感兴趣的:(.net,Core,.net,轮询算法,随机算法,权重算法,C#)