书中代码-自己写写测试
Posted 512178509
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了书中代码-自己写写测试相关的知识,希望对你有一定的参考价值。
package com.fenghao.loadbalancing; 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; import org.junit.Test; /** * * <P>负载均衡算法简单写</P> * @ClassName: loadTest * @author 冯浩 2016年11月9日 上午9:12:49 * @see TODO */ public class loadTest { private final Map<String,Integer> serverWeightMap=new HashMap<String,Integer>(); public loadTest(){ serverWeightMap.put("192.168.1.100", 1); serverWeightMap.put("192.168.1.101", 1); serverWeightMap.put("192.168.1.102", 4); serverWeightMap.put("192.168.1.103", 1); serverWeightMap.put("192.168.1.104", 1); serverWeightMap.put("192.168.1.105", 3); serverWeightMap.put("192.168.1.106", 1); serverWeightMap.put("192.168.1.107", 2); serverWeightMap.put("192.168.1.108", 1); serverWeightMap.put("192.168.1.109", 1); serverWeightMap.put("192.168.1.110", 1); } @Test public void Test(){ int le=4; // roundRobinTest(le); RandomTest(); } /** * * <p>Title: roundRobinTest</p> * <p>轮询</p> * @param pos * @return * @author 冯浩 2016年11月9日 下午7:41:52 */ public String roundRobinTest(Integer pos){ Map<String,Integer> serverMap=new HashMap<String,Integer>(); serverMap.putAll(serverWeightMap); 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++; System.out.println("\npos is "+pos); } System.out.println("\nserver is "+server); return server; } /** * * <p>Title: RandomTest</p> * <p>随机法</p> * @return * @author 冯浩 2016年11月9日 下午7:49:24 */ public String RandomTest(){ Map<String,Integer> serverMap=new HashMap<String,Integer>(); serverMap.putAll(serverWeightMap); Set<String> keySet = serverMap.keySet(); ArrayList<String> keyList=new ArrayList<String>(); keyList.addAll(keySet); Random random=new Random(); int pos = random.nextInt(keyList.size()); System.out.println("\npos is "+pos); String server = keyList.get(pos); System.out.println("\nserver is "+server); return server; } /** * * <p>Title: HashTest</p> * <p>源地址Hash法</p> * @author 冯浩 2016年11月10日 上午8:46:43 */ public String HashTest(String remoteip){ Map<String,Integer> serverMap=new HashMap<String,Integer>(); serverMap.putAll(serverWeightMap); Set<String> keySet = serverMap.keySet(); ArrayList<String> keyList=new ArrayList<String>(); keyList.addAll(keySet); int hashCode = remoteip.hashCode(); int size = keyList.size(); int pos=hashCode % size; System.out.println("\npos is "+pos); return keyList.get(pos); } /** * * <p>Title: weightRoundRobin</p> * <p>加权轮询法</p> * @return * @author 冯浩 2016年11月10日 上午8:57:32 */ public String weightRoundRobin(Integer pos){ Map<String,Integer> serverMap=new HashMap<String,Integer>(); serverMap.putAll(serverWeightMap); 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); } } String server=null; synchronized (pos) { if(pos>=serverList.size()){ pos=0; } server=serverList.get(pos); pos++; } return server; } /** * * <p>Title: WeightRandom</p> * <p>加权随机法</p> * @return * @author 冯浩 2016年11月11日 上午8:43:33 */ public String WeightRandom(){ Map<String,Integer> serverMap=new HashMap<String,Integer>(); serverMap.putAll(serverWeightMap); 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; } }
以上是关于书中代码-自己写写测试的主要内容,如果未能解决你的问题,请参考以下文章