大型分布式网站架构(一)负载均衡算法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lwl2014100338/article/details/81876053
原地址哈希法
package com.lwl.zookeeper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author liuweilong
 * @ClassName: Hash
 * @Description: 源地址哈希法
 * @Date 2018-08-20 17:40
 * @Version 1.0
 **/
public class Hash {
    public static void main(String[] args) {

    }
    public  static  String testConsumerHash(String remoteip){
        //重建Map
        Map<String,Integer> serviceMap=new HashMap<String,Integer>();
        serviceMap.putAll(serviceMap);
        //取得ip地址List
        Set<String> keySet= serviceMap.keySet();
        System.out.println("keyset"+keySet);
        ArrayList<String> keyList= new ArrayList<String>();
        keyList.addAll(keySet);
        int hashCode= remoteip.hashCode();
        int serviceListSize= keyList.size();
        //取模运算
        int servicePos= hashCode % serviceListSize;

        return keyList.get(servicePos);
    }

}

随机法
package com.lwl.zookeeper;

import java.util.*;

/**
 * @author liuweilong
 * @ClassName: Random
 * @Description: 随机法
 * @Date 2018-08-20 17:29
 * @Version 1.0
 **/
public class Random2 {
    public static void main(String[] args) {

    }
    public  String testRandom(){
        //重建Map
        Map<String,Integer> serviceMap=new HashMap<String,Integer>();
        serviceMap.putAll(serviceMap);
        //取得ip地址List
        Set<String> keySet= serviceMap.keySet();
        System.out.println("keyset"+keySet);
        ArrayList<String> keyList= new ArrayList<String>();
        keyList.addAll(keySet);

        //产生随机数
        Random random= new Random();
        int randomPos=random.nextInt(keyList.size());
        String server= keyList.get(randomPos);
        return  server;
    }

}

轮询法
package com.lwl.zookeeper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author liuweilong
 * @ClassName: RoundRobin
 * @Description: zookeeper负载均衡算法之轮询法
 * @Date 2018-08-20 17:09
 * @Version 1.0
 **/
public class RoundRobin {

    public static void main(String[] args) {
        HashMap serviceWeightMap= new HashMap<String,Integer>();
        serviceWeightMap.put("192.168.1.100",1);
        serviceWeightMap.put("192.168.1.101",1);
        serviceWeightMap.put("192.168.1.102",4);
        serviceWeightMap.put("192.168.1.103",1);
        serviceWeightMap.put("192.168.1.105",3);
        serviceWeightMap.put("192.168.1.107",2);

    }
    public static  String testRoundRobin(){
        //重建Map
        Map<String,Integer> serviceMap=new HashMap<String,Integer>();
        serviceMap.putAll(serviceMap);
        //取得ip地址List
        Set<String> keySet= serviceMap.keySet();
        System.out.println("keyset"+keySet);
        ArrayList<String> keyList= new ArrayList<String>();
        keyList.addAll(keySet);
        String server=null;
        Integer pos=null;
        synchronized (pos){
            if (pos>=keySet.size()){
                pos=0;
            }
            server=keyList.get(pos);
            pos++;
        }

         return  server;

    }
}

加权随机
package com.lwl.zookeeper;

import java.util.*;

/**
 * @author liuweilong
 * @ClassName: WeightRandom
 * @Description: 加权随机
 * @Date 2018-08-20 18:35
 * @Version 1.0
 **/
public class WeightRandom {
    public static void main(String[] args) {

    }
    public  static String testWeightRandom(){
        //重建Map
        Map<String,Integer> serviceMap=new HashMap<String,Integer>();
        serviceMap.putAll(serviceMap);
        //取得ip地址List
        Set<String> keySet= serviceMap.keySet();
        Iterator<String> it= keySet.iterator();
        List<String> serverList= new ArrayList<String>();
        while (it.hasNext()){
            String server=it.next();
            Integer weight= serviceMap.get(server);
            for (int i=0;i<weight;i++){
                serverList.add(server);
            }
        }
        Random random=new Random();
        int randomPos= random.nextInt(serverList.size());
        String server= serverList.get(randomPos);

        return server;
    }
}

加权轮询
package com.lwl.zookeeper;

import java.util.*;

/**
 * @author liuweilong
 * @ClassName: WeightRoundRobin
 * @Description: 加权轮询
 * @Date 2018-08-20 18:18
 * @Version 1.0
 **/
public class WeightRoundRobin {
    public static void main(String[] args) {

    }
    public  static String testWeightRoundRobin(){
        //重建Map
        Map<String,Integer> serviceMap=new HashMap<String,Integer>();
        serviceMap.putAll(serviceMap);
        //取得ip地址List
        Set<String> keySet= serviceMap.keySet();
        Iterator<String> it= keySet.iterator();
        ArrayList<String> serverList= new ArrayList<String>();
        while (it.hasNext()){
            String server=it.next();
            Integer weight= serviceMap.get(server);
            for (int i=0;i<weight;i++){
                serverList.add(server);
            }
        }
        String server=null;
        Integer pos=null;
        synchronized (pos){
            if (pos>=serverList.size()){
                pos=0;
            }
            server=serverList.get(pos);
            pos++;
        }

         return server;

    }
}

猜你喜欢

转载自blog.csdn.net/lwl2014100338/article/details/81876053