`
m635674608
  • 浏览: 5031541 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现2

    博客分类:
  • java
 
阅读更多

1. 一般轮询算法

服务器类

Java代码  收藏代码
  1. package com.sosop.roundRobin;  
  2.   
  3. public class Server {  
  4.     private String ip;  
  5.     private int weight;  
  6.       
  7.     public Server(String ip) {  
  8.         super();  
  9.         this.ip = ip;  
  10.     }  
  11.   
  12.     public Server(String ip, int weight) {  
  13.         this.ip     = ip;  
  14.         this.weight = weight;  
  15.     }  
  16.       
  17.     public String getIp() {  
  18.         return ip;  
  19.     }  
  20.     public void setIp(String ip) {  
  21.         this.ip = ip;  
  22.     }  
  23.     public int getWeight() {  
  24.         return weight;  
  25.     }  
  26.     public void setWeight(int weight) {  
  27.         this.weight = weight;  
  28.     }  
  29.   
  30.     @Override  
  31.     public String toString() {  
  32.         return "Server [ip=" + ip + ", weight=" + weight + "]";  
  33.     }     
  34. }  

 实现与测试

Java代码  收藏代码
  1. package com.sosop.roundRobin;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.concurrent.BrokenBarrierException;  
  6. import java.util.concurrent.CyclicBarrier;  
  7.   
  8. public class NormalRoundRobin {  
  9.       
  10.     private List<Server> servers;  
  11.       
  12.     private int currentIndex;  
  13.     private int totalServer;  
  14.       
  15.     public NormalRoundRobin() {  
  16.         servers = new ArrayList<>();  
  17.         servers.add(new Server("192.168.1.2"));  
  18.         servers.add(new Server("192.168.1.3"));  
  19.         servers.add(new Server("192.168.1.4"));  
  20.         servers.add(new Server("192.168.1.5"));  
  21.         servers.add(new Server("192.168.1.6"));  
  22.         servers.add(new Server("192.168.1.7"));  
  23.         servers.add(new Server("192.168.1.8"));  
  24.         totalServer = servers.size();  
  25.         currentIndex = totalServer - 1;  
  26.     }  
  27.   
  28.   
  29.     // 轮询  
  30.     public Server round() {  
  31.         currentIndex = (currentIndex + 1) % totalServer;  
  32.         return servers.get(currentIndex);  
  33.     }  
  34.       
  35.       
  36.     public static void main(String[] args) {  
  37.         final NormalRoundRobin r = new NormalRoundRobin();  
  38.         // 不带并发的轮询  
  39.         for (int i = 0; i < 14; i++) {  
  40.             System.out.println(r.round());  
  41.         }  
  42.           
  43.         System.out.println();  
  44.         System.out.println("==========================");  
  45.         System.out.println();  
  46.           
  47.         final CyclicBarrier b = new CyclicBarrier(14);  
  48.           
  49.         // 带并发的轮询  
  50.         for (int i = 0; i < 14; i++) {  
  51.             new Thread(new Runnable() {  
  52.                 @Override  
  53.                 public void run() {  
  54.                     try {  
  55.                         b.await();  
  56.                         System.out.println(Thread.currentThread().getName() + " " + r.round());  
  57.                     } catch (InterruptedException | BrokenBarrierException e) {  
  58.                         e.printStackTrace();  
  59.                     }  
  60.                 }  
  61.             }, "thread" + i).start();  
  62.         }  
  63.     }  
  64.       
  65. }  

结果:

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.加权轮询

Java代码  收藏代码
  1. package com.sosop.roundRobin;  
  2.   
  3. import java.math.BigInteger;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6. import java.util.concurrent.BrokenBarrierException;  
  7. import java.util.concurrent.CyclicBarrier;  
  8.   
  9. public class WeightRoundRobin {  
  10.     private List<Server> servers;  
  11.   
  12.     private int currentIndex;  
  13.     private int totalServer;  
  14.     private int currentWeight;  
  15.     private int maxWeight;  
  16.     private int gcdWeight;  
  17.   
  18.     public WeightRoundRobin() {  
  19.         servers = new ArrayList<>();  
  20.         servers.add(new Server("192.168.1.2"5));  
  21.         servers.add(new Server("192.168.1.3"10));  
  22.         servers.add(new Server("192.168.1.4"15));  
  23.         servers.add(new Server("192.168.1.5"100));  
  24.         servers.add(new Server("192.168.1.6"5));  
  25.         servers.add(new Server("192.168.1.7"20));  
  26.         servers.add(new Server("192.168.1.8"30));  
  27.         totalServer = servers.size();  
  28.         currentIndex = totalServer - 1;  
  29.         maxWeight = maxWeight();  
  30.         gcdWeight = serverGcd();  
  31.     }  
  32.   
  33.     public Server round() {  
  34.         while (true) {  
  35.             currentIndex = (currentIndex + 1) % totalServer;  
  36.             if (currentIndex == 0) {  
  37.                 currentWeight = currentWeight - gcdWeight;  
  38.                 if (currentWeight <= 0) {  
  39.                     currentWeight = maxWeight;  
  40.                     if(currentWeight == 0) {  
  41.                         return null;  
  42.                     }  
  43.                 }  
  44.             }  
  45.               
  46.             if(servers.get(currentIndex).getWeight() >= currentWeight) {  
  47.                 return servers.get(currentIndex);  
  48.             }  
  49.         }  
  50.     }  
  51.   
  52.     /** 
  53.      * 返回所有服务器的权重的最大公约数 
  54.      *  
  55.      * @return 
  56.      */  
  57.     private int serverGcd() {  
  58.         int comDivisor = 0;  
  59.         for (int i = 0; i < totalServer - 1; i++) {  
  60.             if (comDivisor == 0) {  
  61.                 comDivisor = gcd(servers.get(i).getWeight(), servers.get(i + 1).getWeight());  
  62.             } else {  
  63.                 comDivisor = gcd(comDivisor, servers.get(i + 1).getWeight());  
  64.             }  
  65.         }  
  66.         return comDivisor;  
  67.     }  
  68.   
  69.     /** 
  70.      * 获得服务器中的最大权重 
  71.      *  
  72.      * @return 
  73.      */  
  74.     private int maxWeight() {  
  75.         int max = servers.get(0).getWeight();  
  76.         int tmp;  
  77.         for (int i = 1; i < totalServer; i++) {  
  78.             tmp = servers.get(i).getWeight();  
  79.             if (max < tmp) {  
  80.                 max = tmp;  
  81.             }  
  82.         }  
  83.         return max;  
  84.     }  
  85.   
  86.     /** 
  87.      * 求两个数的最大公约数 4和6最大公约数是2 
  88.      *  
  89.      * @param num1 
  90.      * @param num2 
  91.      * @return 
  92.      */  
  93.     private int gcd(int num1, int num2) {  
  94.         BigInteger i1 = new BigInteger(String.valueOf(num1));  
  95.         BigInteger i2 = new BigInteger(String.valueOf(num2));  
  96.         return i1.gcd(i2).intValue();  
  97.     }  
  98.   
  99.     public static void main(String[] args) {  
  100.         final WeightRoundRobin wr = new WeightRoundRobin();  
  101.         // 非并发情况  
  102.         for (int i = 0; i < 100; i++) {  
  103.             System.out.println(wr.round());  
  104.         }  
  105.           
  106.         System.out.println();  
  107.         System.out.println("==========");  
  108.         System.out.println();  
  109.           
  110.         final CyclicBarrier b = new CyclicBarrier(30);  
  111.         // 并发情况  
  112.         for (int i = 0; i < 30; i++) {  
  113.             new Thread(new Runnable() {  
  114.                 @Override  
  115.                 public void run() {  
  116.                     try {  
  117.                         b.await();  
  118.                         System.out.println(Thread.currentThread().getName() + " " + wr.round());  
  119.                     } catch (InterruptedException | BrokenBarrierException e) {  
  120.                         e.printStackTrace();  
  121.                     }  
  122.                 }  
  123.             }, "thread" + i).start();  
  124.         }  
  125.     }  
  126. }  

 

结果:

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]

分享到:
评论

相关推荐

    加权轮询调度算法DWRR

    权重轮询调度算法(WeightedRound-RobinScheduling)-matlab实现

    c# 实现轮询算法实例代码

    下面我们将深入探讨C#中轮询算法的实现以及在给定代码中的应用。 首先,我们要理解轮询算法的基本概念。轮询算法简单来说就是按照固定的顺序遍历一组元素,每次遍历到一个元素时执行相应的操作。在本例中,算法的...

    负载均衡应用服务器控制用户的算法

    权重轮询调度算法的实现涉及一个指示变量i,用于跟踪上次选择的服务器,以及一个当前权值变量cw,用于记录当前应分配的请求。当新的请求到来时,算法会尝试分配给具有足够权值的服务器,直到找到合适的服务器或所有...

    Nginx平滑的基于权重的轮询算法实现

    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环境下基于权重的任务调度算法.pdf

    然而,Storm默认的轮询调度算法在面对复杂的拓扑结构和多样化的任务需求时,可能存在负载均衡不佳和通信开销过大的问题。为此,文章提出了一种基于权重的任务调度算法(TSAW-Storm)来优化这一状况。 TSAW-Storm...

    WFQ调度算法仿真源码

    通过分析和理解"wfq_patches_1.0b1.tar.gz"中的源码,可以深入学习WFQ的工作原理,以及如何在NS2中实现和调整这种调度算法,这对于网络研究和优化工作具有重要意义。同时,这个源码也可以作为其他网络调度算法实现的...

    流量管理中队列调度算法研究.pdf

    DWRR算法是一种权重轮询算法,旨在实现带宽的公平分配。它通过为每个队列分配一定的“权重”,在轮询过程中按照权重比例分配带宽。每个队列会有一个“信用”值,每次服务完一个数据包后,相应的信用值会减少。当信用...

    深信服AD负载均衡原理.pdf

    2. 加权轮询调度算法:该算法根据链路的权重来选择下一个可用的IP地址。 3. 首个可用调度算法:该算法根据监视器检测到的第一个可用的IP地址来选择下一个可用的IP地址。 4. 哈希调度算法:该算法根据Local DNS的IP...

    ns-代码-WiMAX_MAC层带宽调度算法研究

    ### WiMAX_MAC层带宽调度算法研究 #### 一、WiMAX无线城域网技术概述 WiMAX(Worldwide Interoperability for Microwave Access)技术基于IEEE 802.16标准,是一种新兴的宽带无线接入技术。它以其高传输带宽、多种...

    SPF/RR进程调度算法

    **RR算法**,即轮询调度算法,是一种简单的进程调度策略。在多任务环境下,RR将CPU时间片划分为固定长度,然后依次分配给每个就绪进程,每个进程在分配的时间片内执行,时间片结束后,进程被挂起,下一个进程获得CPU...

    转:典型的几个负载均衡算法---SLB

    负载均衡器如BIG-IP Controller在实现这些算法时,通常还包括监控服务器健康状态、故障检测和恢复策略等功能。通过对流量进行智能调度,负载均衡技术极大地提升了系统的稳定性和效率,对于大规模网络服务尤其重要。

    基于交换芯片的3级调度算法的研究与实现.pdf

    根据提供的文件内容,本文将详细阐述有关基于交换芯片的3级调度算法的研究与实现的相关知识点。 首先,从文献的标题《基于交换芯片的3级调度算法的研究与实现》来看,本文讨论的是在交换芯片上实现的三级调度算法。...

    改进加权补偿优先双轮询调度算法在医院预约分诊排队管理中的应用_刘鹏.pdf

    ### 改进加权补偿优先双轮询调度算法在医院预约分诊排队管理中的应用 #### 概述 本文旨在探讨一种改进的加权补偿优先双轮询调度算法(Weighted Compensation Priority Double Round Robin Scheduling Algorithm, ...

    深信服AD负载均衡技术原理.pdf

    深信服AD负载均衡技术是网络优化的一种重要手段,它主要通过智能DNS调度算法、智能路由调度算法和虚拟服务节点调度算法来实现网络资源的有效分配,确保服务的高可用性和性能。 1. 智能DNS调度算法: - 轮询:每个...

    LVS工作模式-始终调度算法

    ### LVS工作模式与十种调度算法 #### LVS简介 LVS(Linux Virtual Server)是一种基于Linux内核的负载均衡技术,它通过构建虚拟服务器集群来实现对大量并发访问的处理,有效提升系统的可用性和扩展性。LVS支持三种...

    一种LVS负载均衡调度算法WLC的改进.pdf

    本文针对被采用最多、效果较好的WLC调度算法,提出一种优化改进方法,使负载均衡系统更好实现任务均衡。 一、LVS负载均衡调度算法 LVS(Linux Virtual Server)系统是一个开源项目,它采用基于IP层负载均衡的调度...

    无线网络中WRR调度算法的改进.pdf

    传统的轮询调度算法如Weighted Round Robin (WRR)虽然能够在一定程度上提供按权重分配的带宽服务,但在处理变长数据流时往往出现不公平现象。本文介绍了一种改进的WRR调度算法——AFWRR(Average Fair Weighted ...

    车联网数据流的多维服务质量改进异构计算最早完成时间调度算法.pdf

    结果表明,这种基于多维QoS改进的HEFT调度算法,无论是在综合性能还是在满足不同用户需求方面,均优于传统的HEFT调度算法和轮询调度算法。这一发现为解决车联网数据流分布式处理的调度问题提供了新的思路和方法。 ...

Global site tag (gtag.js) - Google Analytics