当前位置: 代码迷 >> 综合 >> 三种常用的负载均衡算法C#(随机、轮询、权重)
  详细解决方案

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

热度:67   发布时间:2024-02-11 22:47:53.0

入参实体:

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

 

  相关解决方案