在一本书里看到负载均衡算法,自己写了一遍,感觉很简单。以后复习用。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MyLoadBalance {
private int position = 0;
private Object o = new Object();
private HashMap<String, String> servers = new HashMap<String, String>()
{
/**
* @Fields serialVersionUID : TODO
*/
private static final long serialVersionUID = 1L;
{
put("182.168.1.11", "1");
put("182.168.1.15", "5");
put("182.168.1.19", "9");
}
};
/**
* 随机
*/
public void ramdomBobin() {
// 结果服务列表
List<String> services = new ArrayList<String>();
// 原服务
HashMap<String, String> newServices = servers;
Iterator<String> iterators = newServices.keySet().iterator();
while (iterators.hasNext()) {
services.add(iterators.next());
}
Random r = new Random();
int index = r.nextInt(services.size());
System.out.println(services.get(index));
}
/**
* 加权随机
*/
public void weightRandomBobin() {
// 结果服务列表
List<String> services = new ArrayList<String>();
// 原服务
HashMap<String, String> newServices = servers;
Iterator<String> iterators = newServices.keySet().iterator();
while (iterators.hasNext()) {
String key = iterators.next();
int weight = Integer.parseInt(newServices.get(key));
for (int i = 0; i < weight; i++) {
services.add(key);
}
}
Random r = new Random();
int index = r.nextInt(services.size());
System.out.println(services.get(index));
}
/**
* 轮询
*/
public void roundRobin() {
// 结果服务列表
List<String> services = new ArrayList<String>();
// 原服务
HashMap<String, String> newServices = servers;
Set<String> keys = newServices.keySet();
Iterator<String> iterators = keys.iterator();
while (iterators.hasNext()) {
services.add(iterators.next());
}
synchronized (o) {
position++;
if (position > services.size() - 1) {
position = 0;
}
System.out.println(services.get(position));
}
}
/**
* 加权轮询
*/
public void weightRoundRobin() {
// 结果服务列表
List<String> services = new ArrayList<String>();
// 原服务
HashMap<String, String> newServices = servers;
Set<String> keys = newServices.keySet();
Iterator<String> iterators = keys.iterator();
while (iterators.hasNext()) {
String key = iterators.next();
int weight = Integer.parseInt(newServices.get(key));
for (int i = 0; i < weight; i++) {
services.add(key);
}
}
synchronized (o) {
position++;
if (position > services.size() - 1) {
position = 0;
}
System.out.println(services.get(position));
}
}
/**
* 源地址hash
*/
public void ipHash() {
// 结果服务列表
List<String> services = new ArrayList<String>();
// 原服务
HashMap<String, String> newServices = servers;
Iterator<String> iterators = newServices.keySet().iterator();
while (iterators.hasNext()) {
services.add(iterators.next());
}
String remoteIp = "192.168.1.21";
int remoteHash = remoteIp.hashCode();
//
int index = remoteHash % services.size();
System.out.println(remoteHash + " " + index + " " + services.get(index));
}
/**
* 最小连接数
*/
public void lestConnection() {
}
/**
*
* @param args
*/
public static void main(String[] args) {
// 创建线程池
ExecutorService executor = Executors.newCachedThreadPool();
// 创建实例
final MyLoadBalance mb = new MyLoadBalance();
//
for (int i = 0; i < 10000; i++) {
executor.submit(new Runnable() {
@Override
public void run() {
// mb.ramdomBobin();
// mb.weightRandomBobin();
// mb.roundRobin();
// mb.weightRoundRobin();
mb.ipHash();
}
});
}
}
}