負載均衡的幾種演算法Java實現程式碼
https://www.cnblogs.com/wxd0108/p/5465938.html
輪詢
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);
}
}
相關文章
- 幾行程式碼實現負載均衡輪詢演算法行程負載演算法
- nignx 負載均衡的幾種演算法介紹負載演算法
- 伺服器負載均衡的幾種演算法伺服器負載演算法
- 6種負載均衡演算法負載演算法
- 六種實現負載均衡技術的方式負載
- Ribbon - 幾種自定義負載均衡策略負載
- Nginx 做負載均衡的幾種輪詢策略Nginx負載
- Java實現負載均衡演算法--輪詢和加權輪詢Java負載演算法
- 淺談負載均衡演算法與實現負載演算法
- Ribbon實現負載均衡負載
- GRPC 負載均衡實現RPC負載
- nginx實現負載均衡Nginx負載
- 負載均衡的種類負載
- 一篇有趣的負載均衡演算法實現負載演算法
- 什麼是負載均衡?有哪幾種策略?負載
- 每個Java程式設計師都必須知道的四種負載均衡演算法Java程式設計師負載演算法
- HaProxy 實現 MySQL 負載均衡MySql負載
- 解密負載均衡技術和負載均衡演算法解密負載演算法
- Nginx實現簡單的負載均衡Nginx負載
- 【演算法】使用Golang實現加權負載均衡演算法演算法Golang負載
- 自適應負載均衡演算法原理與實現負載演算法
- 很全!淺談幾種常用負載均衡架構負載架構
- nginx+tomcat實現負載均衡NginxTomcat負載
- dubbo(三):負載均衡實現解析負載
- 使用YARP來實現負載均衡負載
- Python實現簡單負載均衡Python負載
- 面試官:說說你知道的幾種負載均衡分類面試負載
- Dubbo原始碼分析(九)負載均衡演算法原始碼負載演算法
- SpringCloud微服務中使用RestTemplate+Ribbon實現負載均衡(實現方法+實現原理+替換負載均衡策略)SpringGCCloud微服務REST負載
- Kafka的Consumer負載均衡演算法Kafka負載演算法
- Haproxy搭建 Web 群集實現負載均衡Web負載
- 在Linux中,如何實現負載均衡?Linux負載
- Nginx如何實現四層負載均衡?Nginx負載
- Keepalived實現Nginx負載均衡高可用Nginx負載
- Docker Compose+nginx實現負載均衡DockerNginx負載
- orleans叢集及負載均衡實現負載
- 超實用:實現負載均衡技術的方式負載
- 幾種排序演算法的原理以及 Java 實現排序演算法Java