我是靠谱客的博主 鲤鱼大船,最近开发中收集的这篇文章主要介绍三种常用的负载均衡算法C#(随机、轮询、权重),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

入参实体:

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

1、随机

 /// <summary>
    /// 随机
    /// </summary>
    internal class RandomAlgorithm
    {
        private readonly static Random random = new Random();
        public static string Get(List<ServiceCenterModel> 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、轮询


    /// <summary>
    /// 轮询
    /// </summary>
    internal class PollingAlgorithm
    {
        private static Dictionary<string,int> _serviceDic=new Dictionary<string, int>(); 
        private static SpinLock _spinLock = new SpinLock();
        public static string Get(List<ServiceCenterModel> 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、权重

    /// <summary>
    /// 权重
    /// </summary>
    internal class WeightAlgorithm
    {
        private static ConcurrentDictionary<string, WeightAlgorithmItem> _serviceDic = new ConcurrentDictionary<string, WeightAlgorithmItem>();
        private static SpinLock _spinLock = new SpinLock();
        public static string Get(List<ServiceCenterModel> 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<string>()
                };
                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<ServiceCenterModel> 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<string> Urls { get; set; }
        public int Index { get; set; }
    }

 

最后

以上就是鲤鱼大船为你收集整理的三种常用的负载均衡算法C#(随机、轮询、权重)的全部内容,希望文章能够帮你解决三种常用的负载均衡算法C#(随机、轮询、权重)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部