Round-Robin轮询调度法及其实现原理 加权轮询算法 Round Robin算法的简单C#实现 权重轮询调度算法(Weighted Round-Robin Scheduling)-C#实现 三种常用的负载均衡算法C#(随机、轮询、权重)

    应用场景,是要根据权重给多个队列均匀的提供数据,对输入的数据进行拆分处理。
    最核心的部分,其实是取最大公约数。

    1. using System;
    2. using System.Collections.Generic;
    3. using RoundRobin;
    4. public class MyClass
    5. {
    6. public static void RunSnippet()
    7. {
    8. List<Node> servers = new List<Node>();
    9. servers.Add(new Node("serv","urlA",1));
    10. servers.Add(new Node("serv","urlB",1));
    11. servers.Add(new Node("serv","urlC",4));
    12. Dictionary<string,List<int>> dictLog = new Dictionary<string,List<int>>();
    13. RoundRobinBalancer balancer = new RoundRobinBalancer(servers);
    14. for(int i = 1;i<=100;i++){
    15. Node node = balancer.DispatchTo();
    16. if(!dictLog.ContainsKey(node.Url))
    17. dictLog[node.Url] = new List<int>();
    18. dictLog[node.Url].Add(i);
    19. }
    20. foreach(string key in dictLog.Keys){
    21. WL("Url:{0} ==> {1}",key,dictLog[key].Count);
    22. }
    23. }
    24. #region Helper methods
    25. public static void Main()
    26. {
    27. try
    28. {
    29. RunSnippet();
    30. }
    31. catch (Exception e)
    32. {
    33. string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
    34. Console.WriteLine(error);
    35. }
    36. finally
    37. {
    38. Console.Write("Press any key to continue...");
    39. Console.ReadKey();
    40. }
    41. }
    42. private static void WL(object text, params object[] args)
    43. {
    44. Console.WriteLine(text.ToString(), args);
    45. }
    46. private static void RL()
    47. {
    48. Console.ReadLine();
    49. }
    50. private static void Break()
    51. {
    52. System.Diagnostics.Debugger.Break();
    53. }
    54. #endregion
    55. }
    56. namespace RoundRobin{
    57. public class Node{
    58. public Node(string serviceName,string url,int weight){
    59. this.ServiceName = serviceName;
    60. this.Url = url;
    61. this.Weight = weight;
    62. }
    63. public int Weight{get;private set;}
    64. public string Url{get;private set;}
    65. public string ServiceName{get;private set;}
    66. }
    67. public class RoundRobinBalancer{
    68. private readonly List<Node> nodes;
    69. private int i = -1;
    70. private int cw = 0;
    71. public RoundRobinBalancer(List<Node> nodes){
    72. this.nodes = nodes;
    73. }
    74. public Node DispatchTo(){
    75. while(true){
    76. i = (i+1)%nodes.Count;
    77. if(i==0){
    78. cw = cw - MaxCommonDivisor(nodes);
    79. if(cw <= 0){
    80. cw = MaxWeight(nodes);
    81. if(cw == 0)
    82. return null;
    83. }
    84. }
    85. if((nodes[i]).Weight >= cw)
    86. return nodes[i];
    87. }
    88. }
    89. private static int MaxCommonDivisor(List<Node> nodes){
    90. List<int> nums = new List<int>();
    91. foreach(Node node in nodes)
    92. {
    93. nums.Add(node.Weight);
    94. }
    95. return max_common_divisor(nums);
    96. }
    97. private static int MaxWeight(List<Node> nodes){
    98. int ret = -1;
    99. foreach(Node node in nodes){
    100. if(node.Weight>ret)
    101. ret = node.Weight;
    102. }
    103. return ret;
    104. }
    105. public static int gcd(int n, int m)
    106. {
    107. //swap
    108. if (n<m)
    109. {
    110. n=m+n;
    111. m=n-m;
    112. n=n-m;
    113. }
    114. if (m==0) return n;
    115. return gcd(m,n%m);
    116. }
    117. public static int max_common_divisor(List<int> several)
    118. {
    119. int a=several[0];
    120. int b=several[1];
    121. int c=gcd(a,b);
    122. int i;
    123. for (i=2; i<several.Count; i++)
    124. {
    125. c=gcd(c,several[i]);
    126. }
    127. return c;
    128. }
    129. }
    130. }