C# 实现的几种负载均衡算法
2021/5/14 20:30:58
本文主要是介绍C# 实现的几种负载均衡算法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
主要实现了 轮询、加权轮询、随机、加权随机、IPHash
参考大佬文章:
https://www.cnblogs.com/wxd0108/p/5465938.html
废话不说,码上见
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace System { /// <summary> /// 负载均衡算法 /// </summary> public class MyRoundRobin { // key=ip, val=权重 static Dictionary<String, Int32> serverWeigthMap = new Dictionary<string, int>(); static MyRoundRobin() { initData(); } // 初始化数据 private static void initData() { } /// <summary> /// 测试方法 /// </summary> public static void Test() { var pls = new Collections.Concurrent.ConcurrentBag<String>(); { var dic = new Dictionary<String, int>(); { dic.Add("192.168.1.12", 1); dic.Add("192.168.1.13", 1); dic.Add("192.168.1.14", 2); dic.Add("192.168.1.15", 2); dic.Add("192.168.1.16", 3); dic.Add("192.168.1.17", 3); dic.Add("192.168.1.18", 1); dic.Add("192.168.1.19", 2); } // 初始化数据 InitData(dic); Parallel.For(0, 200, item => { // 轮询 var str = MyRoundRobin.roundRobin(); // 加权轮询 //var str = MyRoundRobin.weightRoundRobin(); // 随机 //var str = MyRoundRobin.random(); // 加权随机 //var str = MyRoundRobin.weightRandom(); // ipHash //var str = MyRoundRobin.ipHash("192.168.0." + item); pls.Add(str); }); pls.GroupBy(d => d) .ToList() .ForEach(str => Console.WriteLine($"{str.Key} cou={str.Count()}")); } } /// <summary> /// 初始化或更新数据 /// </summary> public static void InitData(Dictionary<String, Int32> data) { foreach (var item in data) { serverWeigthMap.Add(item.Key, item.Value); } } private static Int32 pos = 0; private static object lockObj = new object(); /// <summary> /// 轮询 /// </summary> /// <returns></returns> public static String roundRobin() { //ip列表list var keySet = serverWeigthMap.Keys; String server = null; lock (lockObj) { // 重置索引 if (pos >= keySet.Count) { pos = 0; } server = keySet.ElementAt(pos); pos++; } return server; } private static Int32 pos2 = 0; private static object lockObj2 = new object(); /// <summary> /// 加权轮询 /// </summary> /// <returns></returns> public static String weightRoundRobin() { // ip列表list var keySet = serverWeigthMap.Keys; List<String> serverList = new List<String>(); foreach (var item in keySet) { Int32 weight = serverWeigthMap[item]; for (int i = 0; i < weight; i++) { serverList.Add(item); } } String server = null; lock (lockObj2) { // 重置索引 if (pos2 >= serverList.Count) { pos2 = 0; } server = serverList[pos2]; pos2++; } return server; } /// <summary> /// 加权随机 /// </summary> /// <returns></returns> public static String weightRandom() { // ip列表list var keySet = serverWeigthMap.Keys; List<String> serverList = new List<String>(); foreach (var item in keySet) { Int32 weight = serverWeigthMap[item]; for (int i = 0; i < weight; i++) { serverList.Add(item); } } Random random = new Random(); int randomPos = random.Next(serverList.Count); String server = serverList[randomPos]; return server; } /// <summary> /// 随机 /// </summary> /// <returns></returns> public static String random() { // ip列表list var keySet = serverWeigthMap.Keys; Random random = new Random(); int randomPos = random.Next(keySet.Count); String server = keySet.ElementAt(randomPos); return server; } /// <summary> /// IP Hash /// </summary> /// <param name="ip"></param> /// <returns></returns> public static String ipHash(String ip) { // ip列表list var keySet = serverWeigthMap.Keys; int hashCode = Math.Abs(ip.GetHashCode()); int serverPos = hashCode % keySet.Count; return keySet.ElementAt(serverPos); } } }
测试方法:
MyRoundRobin.Test();
over
这篇关于C# 实现的几种负载均衡算法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具