java实现轮询和加权轮询

半兽人 发表于: 2017-11-01   最后更新时间: 2017-11-01  
  •   13 订阅,761 游览

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]






发表于: 9月前   最后更新时间: 9月前   游览量:761
上一条: Java Semaphore信号量
下一条: java读取Excel的方法,解析xls、xlsx两种格式

评论…


  • 评论…
    • in this conversation
      提问