Round Robin算法的简单C#实现
整理笔记的时候,发现自己在项目中还用C#实现过一次Round Robin
算法算法,当时的应用场景,是要根据权重给多个队列均匀的提供数据,对输入的数据进行拆分处理。算法很简单,最核心的部分,其实是取最大公约数。
using System;
using System.Collections.Generic;
using RoundRobin;public class MyClass
{public static void RunSnippet(){List<Node> servers = new List<Node>();servers.Add(new Node("serv","urlA",1));servers.Add(new Node("serv","urlB",1));servers.Add(new Node("serv","urlC",4));Dictionary<string,List<int>> dictLog = new Dictionary<string,List<int>>();RoundRobinBalancer balancer = new RoundRobinBalancer(servers);for(int i = 1;i<=100;i++){Node node = balancer.DispatchTo();if(!dictLog.ContainsKey(node.Url))dictLog[node.Url] = new List<int>();dictLog[node.Url].Add(i);}foreach(string key in dictLog.Keys){WL("Url:{0} ==> {1}",key,dictLog[key].Count);}}#region Helper methodspublic static void Main(){try{RunSnippet();}catch (Exception e){string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());Console.WriteLine(error);}finally{Console.Write("Press any key to continue...");Console.ReadKey();}}private static void WL(object text, params object[] args){Console.WriteLine(text.ToString(), args);}private static void RL(){Console.ReadLine();}private static void Break(){System.Diagnostics.Debugger.Break();}#endregion
}namespace RoundRobin{public class Node{public Node(string serviceName,string url,int weight){this.ServiceName = serviceName;this.Url = url;this.Weight = weight;}public int Weight{get;private set;}public string Url{get;private set;}public string ServiceName{get;private set;}}public class RoundRobinBalancer{private readonly List<Node> nodes;private int i = -1;private int cw = 0;public RoundRobinBalancer(List<Node> nodes){this.nodes = nodes;}public Node DispatchTo(){while(true){i = (i+1)%nodes.Count;if(i==0){cw = cw - MaxCommonDivisor(nodes);if(cw <= 0){cw = MaxWeight(nodes);if(cw == 0)return null;}}if((nodes[i]).Weight >= cw)return nodes[i];}}private static int MaxCommonDivisor(List<Node> nodes){List<int> nums = new List<int>();foreach(Node node in nodes){nums.Add(node.Weight);}return max_common_divisor(nums);}private static int MaxWeight(List<Node> nodes){int ret = -1;foreach(Node node in nodes){if(node.Weight>ret)ret = node.Weight;}return ret;}public static int gcd(int n, int m){//swapif (n<m){n=m+n;m=n-m;n=n-m;}if (m==0) return n;return gcd(m,n%m);}public static int max_common_divisor(List<int> several){int a=several[0];int b=several[1];int c=gcd(a,b);int i;for (i=2; i<several.Count; i++){c=gcd(c,several[i]);}return c;}}
}
Round Robin算法的简单C#实现相关推荐
- Round Robin 算法
什么是Round Robin? 先来看和他相近的名词,轮询调度算法(Round-Robin Scheduling) 轮询调度算法的原理是每一次把来自用户的请求轮流分配给内部中的服务器,从1开始,直到N ...
- 仲裁器设计(二)-- Round Robin Arbiter 轮询调度算法
作者:李虹江 原文:https://mp.weixin.qq.com/s/r-nckE5nGz9mc5KqjPXKYg 本文授权转自IC加油站微信号,未经作者授权,严禁二次转载. 上一篇老李讲了固定优 ...
- round robin权重轮循算法实现
为什么80%的码农都做不了架构师?>>> 先上代码,采用php脚本语言 <?php/* * Copyright (C) FatHong*//* 数据初始化,weight: ...
- 负载均衡算法--轮询法(Round Robin)
在分布式系统中,为了实现系统的高性能.高并发.高可用,在构架中都会进行负载均衡设计,它是分布式系统的核心和中枢,负载均衡的好坏直接影响着整个系统的性能.负载均衡分为软件均衡和硬件均衡两类,比如apac ...
- 负载均衡算法--加权轮询法(Weight Round Robin)
接上一篇博文:负载均衡算法–轮询法(Round Robin),本文讲解加权轮询算法. 加权轮询算法:不同的后端服务器可能机器的配置和当前系统的负载并不相同,因此它们的抗压能力也不相同.给配置高.负载低 ...
- Nginx负载均衡的4种方式 :轮询-Round Robin 、Ip地址-ip_hash、最少连接-least_conn、加权-weight=n
负载均衡的概念: Load Balance负载均衡是用于解决一台机器(一个进程)无法解决所有请求而产生的一种算法. 我们知道单台服务器的性能是有上限的,当流量很大时,就需要使用多台服务器来共同提供服务 ...
- 教学优化算法的简单介绍
目录 摘要 背景 算法 学生初始化 教学阶段 学习阶段 流程总结 优缺点 优点 缺点 一些改进 总结 参考文献 摘要 教学优化算法(Teaching-learning-based optimizati ...
- 【数字IC/FPGA】仲裁器进阶--Round Robin Arbiter
Round Robin Arbiter 固定优先级的缺点是:每个模块的优先级自始至终是固定不变的,这在某种程度上来说是不公平的,Round Robin就是考虑到公平性的一种仲裁算法.其基本思路是,当一 ...
- Round robin
本篇文章先讲述轮询调度算法 (Round-Robin)及其在此基础上改进型的权重轮询算法 (Weighted Round-Robin). 轮询调度算法(Round-Robin Scheduling) ...
最新文章
- kotlin重写构造方法编译报错:Primary constructor call expected
- 北京工业大学计算机科学与技术学科评估,本次学科评估进步最大学校之一——北京工业大学...
- vue html引入资源dev下404,webpack vue 项目打包生成的文件,资源文件报404问题的修复方法(总结篇)...
- .Net Core 环境安装
- ios开发瀑布流框架的封装
- Java集合框架:ArrayList
- 云服务器开启ftp_用云服务器怎么挂机器人
- php create()方法,ThinkPHP中create()方法自动验证实例
- 疑似又一vivoX80新机通过3C质量认证:全系标配80W快充
- 图像的平滑与锐化代码matlab_【图像处理】轻松搞懂图像锐化
- Sublime text3 更改侧边栏颜色
- 如何编写兼容各主流邮箱的HTML邮件
- 怎么通过创新再造58
- 【预测模型】BP神经网络的预测
- mysql 删除恢复_MySQL之delete 忘加where条件误删除恢复
- 窗口置顶工具v2.1.0
- 防止excel单元格有效性验证因被粘贴而失效
- skinsdog 狗网官网CSGO饰品皮肤开箱网站可直接取回
- 2021 An Updated Comparison of Four Low Earth Orbit Satellite Constellation Systems to Provide Global
- 21年双非二战南京大学软件学院专硕经验贴
热门文章
- bzoj 1064 noi2008 假面舞会题解
- js实现文件下载并重命名
- short 在JAVA_short在java中是什么类型的
- 拓臻生物任命Senthil Sundaram为首席执行官、Mark Vignola, Ph.D.为首席财务官、Erin Quirk, M.D.为总裁
- [置顶] 我奋斗了18年才和你坐在一起喝咖啡
- angular4子路由辅助路由
- 一生的读书计划——影响中国历史进程的中国名人
- flex布局文本不换行
- 使用Android Studio 写骰子游戏
- opencv与C++实现最大类间方差法(OTSU)进行图像二值化