<code id="ymukc"><xmp id="ymukc">

負載均衡的幾種算法Java實現代碼

ScaNIV的頭像 ScaNIV 0 2016-03-04 14:59 2

 基本信息

× 1   

瀏覽數: 33588

分享時間: 2 年 前

19

輪詢

package class2.zookeeper.loadbalance;

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

/**
 * 負載均衡算法,輪詢法
 * @author guoy
 *
 */
public class TestRoundRobin {

	
	static Map<String,Integer> serverWeigthMap  = new HashMap<String,Integer>();

	 static{
		serverWeigthMap.put("192.168.1.12", 1);
		serverWeigthMap.put("192.168.1.13", 1);
		serverWeigthMap.put("192.168.1.14", 2);
		serverWeigthMap.put("192.168.1.15", 2);
		serverWeigthMap.put("192.168.1.16", 3);
		serverWeigthMap.put("192.168.1.17", 3);
		serverWeigthMap.put("192.168.1.18", 1);
		serverWeigthMap.put("192.168.1.19", 2);
	}
	 Integer  pos = 0;
	 public  String roundRobin()
		{
			//重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
			Map<String,Integer> serverMap  = new HashMap<String,Integer>();
			serverMap.putAll(serverWeigthMap);
			//獲取ip列表list
			Set<String> keySet = serverMap.keySet();
			ArrayList<String> keyList = new ArrayList<String>();
			keyList.addAll(keySet);
			
			String server = null;
			
			synchronized (pos) {
				if(pos >=keySet.size()){
					pos = 0;
				}
				server = keyList.get(pos);
				pos ++;
			}
			return server;
		}
		
		public static void main(String[] args) {
			TestRoundRobin robin = new TestRoundRobin();
			for (int i = 0; i < 20; i++) {
				String serverIp = robin.roundRobin();
				System.out.println(serverIp);
			}
		}
}

加權輪詢

package class2.zookeeper.loadbalance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
 * 加權隨機載均衡算法
 * @author guoy
 *
 */
public class TestWeightRandom {
	
	static Map<String,Integer> serverWeigthMap  = new HashMap<String,Integer>();

	 static{
		serverWeigthMap.put("192.168.1.12", 1);
		serverWeigthMap.put("192.168.1.13", 1);
		serverWeigthMap.put("192.168.1.14", 2);
		serverWeigthMap.put("192.168.1.15", 2);
		serverWeigthMap.put("192.168.1.16", 3);
		serverWeigthMap.put("192.168.1.17", 3);
		serverWeigthMap.put("192.168.1.18", 1);
		serverWeigthMap.put("192.168.1.19", 2);
	}

	public static String weightRandom()
	{
		//重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
		Map<String,Integer> serverMap  = new HashMap<String,Integer>();
		serverMap.putAll(serverWeigthMap);
		//獲取ip列表list
		Set<String> keySet = serverMap.keySet();
		Iterator<String> it = keySet.iterator();

		List<String> serverList = new ArrayList<String>();

		while (it.hasNext()) {
			String server = it.next();
			Integer weight = serverMap.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;
	}
	
	public static void main(String[] args) {
		String serverIp = weightRandom();
		System.out.println(serverIp);
	}
}

隨機

package class2.zookeeper.loadbalance;

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

/**
 * 隨機負載均衡算法
 * @author guoy
 *
 */
public class TestRandom {
	
	static Map<String,Integer> serverWeigthMap  = new HashMap<String,Integer>();

	 static{
		serverWeigthMap.put("192.168.1.12", 1);
		serverWeigthMap.put("192.168.1.13", 1);
		serverWeigthMap.put("192.168.1.14", 2);
		serverWeigthMap.put("192.168.1.15", 2);
		serverWeigthMap.put("192.168.1.16", 3);
		serverWeigthMap.put("192.168.1.17", 3);
		serverWeigthMap.put("192.168.1.18", 1);
		serverWeigthMap.put("192.168.1.19", 2);
	}

	public static String random()
	{
		//重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
		Map<String,Integer> serverMap  = new HashMap<String,Integer>();
		serverMap.putAll(serverWeigthMap);
		//獲取ip列表list
		Set<String> keySet = serverMap.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;
	}
	
	public static void main(String[] args) {
		String serverIp = random();
		System.out.println(serverIp);
	}
}

加權隨機

package class2.zookeeper.loadbalance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
 * 加權隨機載均衡算法
 * @author guoy
 *
 */
public class TestWeightRandom {
	
	static Map<String,Integer> serverWeigthMap  = new HashMap<String,Integer>();

	 static{
		serverWeigthMap.put("192.168.1.12", 1);
		serverWeigthMap.put("192.168.1.13", 1);
		serverWeigthMap.put("192.168.1.14", 2);
		serverWeigthMap.put("192.168.1.15", 2);
		serverWeigthMap.put("192.168.1.16", 3);
		serverWeigthMap.put("192.168.1.17", 3);
		serverWeigthMap.put("192.168.1.18", 1);
		serverWeigthMap.put("192.168.1.19", 2);
	}

	public static String weightRandom()
	{
		//重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
		Map<String,Integer> serverMap  = new HashMap<String,Integer>();
		serverMap.putAll(serverWeigthMap);
		//獲取ip列表list
		Set<String> keySet = serverMap.keySet();
		Iterator<String> it = keySet.iterator();

		List<String> serverList = new ArrayList<String>();

		while (it.hasNext()) {
			String server = it.next();
			Integer weight = serverMap.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;
	}
	
	public static void main(String[] args) {
		String serverIp = weightRandom();
		System.out.println(serverIp);
	}
}

ip hash

package class2.zookeeper.loadbalance;

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

/**
 * 負載均衡 ip_hash算法
 * @author guoy
 *
 */
public class TestIpHash {

	
	static Map<String,Integer> serverWeigthMap  = new HashMap<String,Integer>();

	 static{
		serverWeigthMap.put("192.168.1.12", 1);
		serverWeigthMap.put("192.168.1.13", 1);
		serverWeigthMap.put("192.168.1.14", 2);
		serverWeigthMap.put("192.168.1.15", 2);
		serverWeigthMap.put("192.168.1.16", 3);
		serverWeigthMap.put("192.168.1.17", 3);
		serverWeigthMap.put("192.168.1.18", 1);
		serverWeigthMap.put("192.168.1.19", 2);
	}

	 /**
	  * 獲取請求服務器地址
	  * @param remoteIp 負載均衡服務器ip
	  * @return
	  */
	public static String ipHash(String remoteIp)
	{
		//重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
		Map<String,Integer> serverMap  = new HashMap<String,Integer>();
		serverMap.putAll(serverWeigthMap);
		//獲取ip列表list
		Set<String> keySet = serverMap.keySet();
		ArrayList<String> keyList = new ArrayList<String>();
		keyList.addAll(keySet);
		
		int hashCode =remoteIp.hashCode();
		int serverListSize = keyList.size();
		int serverPos = hashCode % serverListSize;
		
		return keyList.get(serverPos);
	}
	
	public static void main(String[] args) {
		String serverIp = ipHash("192.168.1.12");
		System.out.println(serverIp);
	}

}

 


12 [下一頁]

  • lzjlzj43的頭像 lzjlzj43 2016-10-24 20:14 代碼數:0

    加權輪詢里面的代碼居然是加權隨機的代碼,估計是代碼貼錯了吧

  • 龐召權的頭像 龐召權 2016-06-27 15:54 代碼數:0

    ipHash()代碼

    int hashCode =remoteIp.hashCode(); 會出現負值。

     

  • cixexey的頭像 cixexey 2018-05-21 17:41 代碼數:0

    New web site is looking good. Thanks for the great effort. Colorado SEO services

  • cixexey的頭像 cixexey 2018-05-22 18:20 代碼數:0

    This was really an interesting topic and I kinda agree with what you have mentioned here! click here

  • 哈哈哈哈的頭像 哈哈哈哈 2016-07-17 10:57 代碼數:0

    受教了。。。

  • cixexey的頭像 cixexey 2018-05-23 18:49 代碼數:0

    This is the type of information I’ve long been trying to find. Thank you for writing this information.moving companies

  • cixexey的頭像 cixexey 2018-05-23 20:08 代碼數:0

    I can set up my new idea from this post. It gives in depth information. Thanks for this valuable information for all,..Woodworking Sanders

  • cixexey的頭像 cixexey 2018-05-26 20:45 代碼數:0

    I high appreciate this post. It’s hard to find the good from the bad sometimes, but I think you’ve nailed it! would you mind updating your blog with more information? invisalign near me

  • cixexey的頭像 cixexey 2018-05-27 17:54 代碼數:0

    Wow what a Great Information about World Day its very nice informative post. thanks for the post.Website nhap hang Mona Media

  • cixexey的頭像 cixexey 2018-05-27 19:39 代碼數:0

    This is a brilliant blog! I'm very happy with the comments!..read more

您的評論:

  
六合特码资料
<code id="ymukc"><xmp id="ymukc">
<code id="ymukc"><xmp id="ymukc">