Java가 부하 균형을 이루는 몇 가지 알고리즘 코드를 상세히 설명하다
9444 단어 부하 균형
폴링:
package class2.zookeeper.loadbalance;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* d ,
* @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, F е Kl}
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;
/**
* SC
* @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, F е Kl}
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;
/**
* SC
* @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, F е Kl}
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;
/**
* SC
* @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, F е Kl}
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, F е Kl}
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);
}
}
이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되고 저희를 많이 응원해 주십시오.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Haproxy 웹 클러스터 구축실험 준비: Haproxy 서버 1대, Nginx 서버 2대, 클라이언트 1대(로컬 컴퓨터 사용) Nginx 서버: ### 참고: 둘 다 비슷한 작업을 수행해야 합니다. Haproxy 서비스: 테스트: 클라이언트가 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.