负载均衡算法Java实现

IP地址类:

package com.way.LB;

import java.util.HashMap;

public class IPMap {

    public static HashMap<String,Integer> serverWeightMap=new HashMap<String,Integer>();

    static{
        serverWeightMap.put("http://www.baidu.com",1);
        serverWeightMap.put("http://www.qq.com", 2);
        serverWeightMap.put("http://www.taobao.com", 3);
    }

}

哈希法

package com.way.LB.LBAlgorithm;

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

import com.way.LB.IPMap;

public class Hash {

    public static String getServer(){
        Map<String,Integer> ipServerMap=new HashMap<String,Integer>();
        ipServerMap.putAll(IPMap.serverWeightMap);

        Set<String> ipSet=ipServerMap.keySet();
        ArrayList<String> ipList=new ArrayList<String>();

        ipList.addAll(ipSet);

        String clientIP="127.0.0.1";
        int hashCode=clientIP.hashCode();

        int ipPos=hashCode%ipList.size();

        return ipList.get(ipPos);

    }
}

随机数法

package com.way.LB.LBAlgorithm;

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

import com.way.LB.IPMap;

public class Random {

    public static String getServer(){

        Map<String,Integer> serverIPMap=new HashMap<String,Integer>();
        serverIPMap.putAll(IPMap.serverWeightMap);

        Set<String> ipSet=serverIPMap.keySet();
        ArrayList<String> ipList=new ArrayList<String>();
        ipList.addAll(ipSet);

        java.util.Random random=new java.util.Random();
        int randompos=random.nextInt(ipList.size());


        return ipList.get(randompos);
    }

}

轮询法:

package com.way.LB.LBAlgorithm;

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

import com.way.LB.IPMap;

public class RoundRobin {
    private static Integer pos=0;

    public static String getServer(){
        // 重建一个Map,避免服务器的上下线导致的并发问题
        Map<String, Integer> serverMap =
                new HashMap<String, Integer>();
        serverMap.putAll(IPMap.serverWeightMap);

        // 取得Ip地址List
        Set<Entry<String, Integer>> entrySet = serverMap.entrySet();
        ArrayList<Entry<String, Integer>> keyList = new ArrayList<Entry<String, Integer>>();
        keyList.addAll(entrySet);

        String server = null;
        synchronized (pos)
        {
            if (pos > entrySet.size())
                pos = 0;
            server = keyList.get(pos).getKey();
            pos ++;
        }

        return server;
    }
}

加权随机法:

package com.way.LB.LBAlgorithm;

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

import com.way.LB.IPMap;

public class WeightRandom {

    public static String getString(){
        Map<String,Integer> ipServerMap=new HashMap<String,Integer>();
        ipServerMap.putAll(IPMap.serverWeightMap);
        Set<Entry<String,Integer>> ipSet=ipServerMap.entrySet();
        ArrayList<String> ipList=new ArrayList<String>();

        for(Entry<String,Integer> entry:ipSet){
            for(int i=0;i<entry.getValue();i++){
                ipList.add(entry.getKey());
            }
        }

        java.util.Random random=new java.util.Random();

        int randomPos=random.nextInt(ipList.size());
        return ipList.get(randomPos);
    }
}

加权轮询法

package com.way.LB.LBAlgorithm;

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

import com.way.LB.IPMap;

public class WeightRoundRobin {
    private static Integer pos;

    public static String getServer(){

        Map<String,Integer> ipServerMap=new HashMap<String,Integer>();
        ipServerMap.putAll(IPMap.serverWeightMap);

        Set<Entry<String,Integer>> ipSet=ipServerMap.entrySet();

        ArrayList<String> ipList=new ArrayList<String>();

        for(Entry<String,Integer> entry:ipSet){
            for(int i=0;i<entry.getValue();i++){
                ipList.add(entry.getKey());
            }

        }

        String ipStr=null;
        synchronized (pos) {
            if(pos>ipList.size()){
                pos=0;
            }
            pos++;
            ipStr=ipList.get(pos);
        }

        return ipStr;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值