1. 一般轮询算法
服务器类
- package com.sosop.roundRobin;
- public class Server {
- private String ip;
- private int weight;
- public Server(String ip) {
- super();
- this.ip = ip;
- }
- public Server(String ip, int weight) {
- this.ip = ip;
- this.weight = weight;
- }
- public String getIp() {
- return ip;
- }
- public void setIp(String ip) {
- this.ip = ip;
- }
- public int getWeight() {
- return weight;
- }
- public void setWeight(int weight) {
- this.weight = weight;
- }
- @Override
- public String toString() {
- return "Server [ip=" + ip + ", weight=" + weight + "]";
- }
- }
实现与测试
- package com.sosop.roundRobin;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.BrokenBarrierException;
- import java.util.concurrent.CyclicBarrier;
- public class NormalRoundRobin {
- private List<Server> servers;
- private int currentIndex;
- private int totalServer;
- public NormalRoundRobin() {
- servers = new ArrayList<>();
- servers.add(new Server("192.168.1.2"));
- servers.add(new Server("192.168.1.3"));
- servers.add(new Server("192.168.1.4"));
- servers.add(new Server("192.168.1.5"));
- servers.add(new Server("192.168.1.6"));
- servers.add(new Server("192.168.1.7"));
- servers.add(new Server("192.168.1.8"));
- totalServer = servers.size();
- currentIndex = totalServer - 1;
- }
- // 轮询
- public Server round() {
- currentIndex = (currentIndex + 1) % totalServer;
- return servers.get(currentIndex);
- }
- public static void main(String[] args) {
- final NormalRoundRobin r = new NormalRoundRobin();
- // 不带并发的轮询
- for (int i = 0; i < 14; i++) {
- System.out.println(r.round());
- }
- System.out.println();
- System.out.println("==========================");
- System.out.println();
- final CyclicBarrier b = new CyclicBarrier(14);
- // 带并发的轮询
- for (int i = 0; i < 14; i++) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- b.await();
- System.out.println(Thread.currentThread().getName() + " " + r.round());
- } catch (InterruptedException | BrokenBarrierException e) {
- e.printStackTrace();
- }
- }
- }, "thread" + i).start();
- }
- }
- }
结果:
Server [ip=192.168.1.2, weight=0]
Server [ip=192.168.1.3, weight=0]
Server [ip=192.168.1.4, weight=0]
Server [ip=192.168.1.5, weight=0]
Server [ip=192.168.1.6, weight=0]
Server [ip=192.168.1.7, weight=0]
Server [ip=192.168.1.8, weight=0]
Server [ip=192.168.1.2, weight=0]
Server [ip=192.168.1.3, weight=0]
Server [ip=192.168.1.4, weight=0]
Server [ip=192.168.1.5, weight=0]
Server [ip=192.168.1.6, weight=0]
Server [ip=192.168.1.7, weight=0]
Server [ip=192.168.1.8, weight=0]
==========================
thread13 Server [ip=192.168.1.2, weight=0]
thread4 Server [ip=192.168.1.6, weight=0]
thread3 Server [ip=192.168.1.5, weight=0]
thread1 Server [ip=192.168.1.3, weight=0]
thread12 Server [ip=192.168.1.7, weight=0]
thread0 Server [ip=192.168.1.2, weight=0]
thread2 Server [ip=192.168.1.4, weight=0]
thread10 Server [ip=192.168.1.6, weight=0]
thread11 Server [ip=192.168.1.5, weight=0]
thread8 Server [ip=192.168.1.3, weight=0]
thread9 Server [ip=192.168.1.4, weight=0]
thread7 Server [ip=192.168.1.2, weight=0]
thread6 Server [ip=192.168.1.8, weight=0]
thread5 Server [ip=192.168.1.7, weight=0]
2.加权轮询
- package com.sosop.roundRobin;
- import java.math.BigInteger;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.BrokenBarrierException;
- import java.util.concurrent.CyclicBarrier;
- public class WeightRoundRobin {
- private List<Server> servers;
- private int currentIndex;
- private int totalServer;
- private int currentWeight;
- private int maxWeight;
- private int gcdWeight;
- public WeightRoundRobin() {
- servers = new ArrayList<>();
- servers.add(new Server("192.168.1.2", 5));
- servers.add(new Server("192.168.1.3", 10));
- servers.add(new Server("192.168.1.4", 15));
- servers.add(new Server("192.168.1.5", 100));
- servers.add(new Server("192.168.1.6", 5));
- servers.add(new Server("192.168.1.7", 20));
- servers.add(new Server("192.168.1.8", 30));
- totalServer = servers.size();
- currentIndex = totalServer - 1;
- maxWeight = maxWeight();
- gcdWeight = serverGcd();
- }
- public Server round() {
- while (true) {
- currentIndex = (currentIndex + 1) % totalServer;
- if (currentIndex == 0) {
- currentWeight = currentWeight - gcdWeight;
- if (currentWeight <= 0) {
- currentWeight = maxWeight;
- if(currentWeight == 0) {
- return null;
- }
- }
- }
- if(servers.get(currentIndex).getWeight() >= currentWeight) {
- return servers.get(currentIndex);
- }
- }
- }
- /**
- * 返回所有服务器的权重的最大公约数
- *
- * @return
- */
- private int serverGcd() {
- int comDivisor = 0;
- for (int i = 0; i < totalServer - 1; i++) {
- if (comDivisor == 0) {
- comDivisor = gcd(servers.get(i).getWeight(), servers.get(i + 1).getWeight());
- } else {
- comDivisor = gcd(comDivisor, servers.get(i + 1).getWeight());
- }
- }
- return comDivisor;
- }
- /**
- * 获得服务器中的最大权重
- *
- * @return
- */
- private int maxWeight() {
- int max = servers.get(0).getWeight();
- int tmp;
- for (int i = 1; i < totalServer; i++) {
- tmp = servers.get(i).getWeight();
- if (max < tmp) {
- max = tmp;
- }
- }
- return max;
- }
- /**
- * 求两个数的最大公约数 4和6最大公约数是2
- *
- * @param num1
- * @param num2
- * @return
- */
- private int gcd(int num1, int num2) {
- BigInteger i1 = new BigInteger(String.valueOf(num1));
- BigInteger i2 = new BigInteger(String.valueOf(num2));
- return i1.gcd(i2).intValue();
- }
- public static void main(String[] args) {
- final WeightRoundRobin wr = new WeightRoundRobin();
- // 非并发情况
- for (int i = 0; i < 100; i++) {
- System.out.println(wr.round());
- }
- System.out.println();
- System.out.println("==========");
- System.out.println();
- final CyclicBarrier b = new CyclicBarrier(30);
- // 并发情况
- for (int i = 0; i < 30; i++) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- b.await();
- System.out.println(Thread.currentThread().getName() + " " + wr.round());
- } catch (InterruptedException | BrokenBarrierException e) {
- e.printStackTrace();
- }
- }
- }, "thread" + i).start();
- }
- }
- }
结果:
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.4, weight=15]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.3, weight=10]
Server [ip=192.168.1.4, weight=15]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.2, weight=5]
Server [ip=192.168.1.3, weight=10]
Server [ip=192.168.1.4, weight=15]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.6, weight=5]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.4, weight=15]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.3, weight=10]
Server [ip=192.168.1.4, weight=15]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.2, weight=5]
Server [ip=192.168.1.3, weight=10]
Server [ip=192.168.1.4, weight=15]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.6, weight=5]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.4, weight=15]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.3, weight=10]
==========
thread0 Server [ip=192.168.1.5, weight=100]
thread3 Server [ip=192.168.1.2, weight=5]
thread7 Server [ip=192.168.1.6, weight=5]
thread10 Server [ip=192.168.1.5, weight=100]
thread12 Server [ip=192.168.1.5, weight=100]
thread15 Server [ip=192.168.1.5, weight=100]
thread18 Server [ip=192.168.1.5, weight=100]
thread2 Server [ip=192.168.1.8, weight=30]
thread29 Server [ip=192.168.1.5, weight=100]
thread1 Server [ip=192.168.1.7, weight=20]
thread27 Server [ip=192.168.1.8, weight=30]
thread26 Server [ip=192.168.1.5, weight=100]
thread25 Server [ip=192.168.1.8, weight=30]
thread24 Server [ip=192.168.1.5, weight=100]
thread23 Server [ip=192.168.1.5, weight=100]
thread22 Server [ip=192.168.1.5, weight=100]
thread21 Server [ip=192.168.1.5, weight=100]
thread20 Server [ip=192.168.1.5, weight=100]
thread19 Server [ip=192.168.1.5, weight=100]
thread17 Server [ip=192.168.1.5, weight=100]
thread16 Server [ip=192.168.1.5, weight=100]
thread14 Server [ip=192.168.1.5, weight=100]
thread13 Server [ip=192.168.1.5, weight=100]
thread11 Server [ip=192.168.1.5, weight=100]
thread9 Server [ip=192.168.1.8, weight=30]
thread8 Server [ip=192.168.1.7, weight=20]
thread6 Server [ip=192.168.1.5, weight=100]
thread5 Server [ip=192.168.1.4, weight=15]
thread4 Server [ip=192.168.1.3, weight=10]
thread28 Server [ip=192.168.1.5, weight=100]
相关推荐
权重轮询调度算法(WeightedRound-RobinScheduling)-matlab实现
下面我们将深入探讨C#中轮询算法的实现以及在给定代码中的应用。 首先,我们要理解轮询算法的基本概念。轮询算法简单来说就是按照固定的顺序遍历一组元素,每次遍历到一个元素时执行相应的操作。在本例中,算法的...
权重轮询调度算法的实现涉及一个指示变量i,用于跟踪上次选择的服务器,以及一个当前权值变量cw,用于记录当前应分配的请求。当新的请求到来时,算法会尝试分配给具有足够权值的服务器,直到找到合适的服务器或所有...
Nginx平滑的基于权重轮询算法描述为: Algorithm is as follows: on each peer selection we increase current_weight of each eligible peer by its weight, select peer with greatest current_weight and reduce ...
然而,Storm默认的轮询调度算法在面对复杂的拓扑结构和多样化的任务需求时,可能存在负载均衡不佳和通信开销过大的问题。为此,文章提出了一种基于权重的任务调度算法(TSAW-Storm)来优化这一状况。 TSAW-Storm...
通过分析和理解"wfq_patches_1.0b1.tar.gz"中的源码,可以深入学习WFQ的工作原理,以及如何在NS2中实现和调整这种调度算法,这对于网络研究和优化工作具有重要意义。同时,这个源码也可以作为其他网络调度算法实现的...
DWRR算法是一种权重轮询算法,旨在实现带宽的公平分配。它通过为每个队列分配一定的“权重”,在轮询过程中按照权重比例分配带宽。每个队列会有一个“信用”值,每次服务完一个数据包后,相应的信用值会减少。当信用...
2. 加权轮询调度算法:该算法根据链路的权重来选择下一个可用的IP地址。 3. 首个可用调度算法:该算法根据监视器检测到的第一个可用的IP地址来选择下一个可用的IP地址。 4. 哈希调度算法:该算法根据Local DNS的IP...
### WiMAX_MAC层带宽调度算法研究 #### 一、WiMAX无线城域网技术概述 WiMAX(Worldwide Interoperability for Microwave Access)技术基于IEEE 802.16标准,是一种新兴的宽带无线接入技术。它以其高传输带宽、多种...
**RR算法**,即轮询调度算法,是一种简单的进程调度策略。在多任务环境下,RR将CPU时间片划分为固定长度,然后依次分配给每个就绪进程,每个进程在分配的时间片内执行,时间片结束后,进程被挂起,下一个进程获得CPU...
负载均衡器如BIG-IP Controller在实现这些算法时,通常还包括监控服务器健康状态、故障检测和恢复策略等功能。通过对流量进行智能调度,负载均衡技术极大地提升了系统的稳定性和效率,对于大规模网络服务尤其重要。
根据提供的文件内容,本文将详细阐述有关基于交换芯片的3级调度算法的研究与实现的相关知识点。 首先,从文献的标题《基于交换芯片的3级调度算法的研究与实现》来看,本文讨论的是在交换芯片上实现的三级调度算法。...
### 改进加权补偿优先双轮询调度算法在医院预约分诊排队管理中的应用 #### 概述 本文旨在探讨一种改进的加权补偿优先双轮询调度算法(Weighted Compensation Priority Double Round Robin Scheduling Algorithm, ...
深信服AD负载均衡技术是网络优化的一种重要手段,它主要通过智能DNS调度算法、智能路由调度算法和虚拟服务节点调度算法来实现网络资源的有效分配,确保服务的高可用性和性能。 1. 智能DNS调度算法: - 轮询:每个...
### LVS工作模式与十种调度算法 #### LVS简介 LVS(Linux Virtual Server)是一种基于Linux内核的负载均衡技术,它通过构建虚拟服务器集群来实现对大量并发访问的处理,有效提升系统的可用性和扩展性。LVS支持三种...
本文针对被采用最多、效果较好的WLC调度算法,提出一种优化改进方法,使负载均衡系统更好实现任务均衡。 一、LVS负载均衡调度算法 LVS(Linux Virtual Server)系统是一个开源项目,它采用基于IP层负载均衡的调度...
传统的轮询调度算法如Weighted Round Robin (WRR)虽然能够在一定程度上提供按权重分配的带宽服务,但在处理变长数据流时往往出现不公平现象。本文介绍了一种改进的WRR调度算法——AFWRR(Average Fair Weighted ...
结果表明,这种基于多维QoS改进的HEFT调度算法,无论是在综合性能还是在满足不同用户需求方面,均优于传统的HEFT调度算法和轮询调度算法。这一发现为解决车联网数据流分布式处理的调度问题提供了新的思路和方法。 ...