编程知识 cdmana.com

Five load balancing algorithms implemented in Java

Concept

Load balancing is to access the client request , Forward to each by pre agreed rules server. There are several classical algorithms , Now let's use Java Implement these algorithms .

 picture

Polling algorithm

The polling algorithm assigns each new connection request to the next server in order , Finally, all requests are shared equally among all servers .

advantage : Absolutely fair

shortcoming : Cannot allocate based on server performance , Unable to make rational use of server resources .

package com.monkeyjava.learn.basic.robin;

import com.google.common.collect.Lists;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestRound {

private Integer index = 0;
private List<String> ips = Lists.newArrayList("192.168.1.1", "192.168.1.2", "192.168.1.3");


public String roundRobin(){
String serverIp;
synchronized(index){
if (index >= ips.size()){
index = 0;
}
serverIp= ips.get(index);
// polling +1
index ++;
}
return serverIp;
}

public static void main(String[] args) {
TestRound testRoundRobin =new TestRound();
for (int i=0;i< 10 ;i++){
String serverIp= testRoundRobin.roundRobin();
System.out.println(serverIp);
}
}
}

Output results :

192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.1

Weighted polling

In this algorithm , The number of connections accepted by each machine is distributed in proportion to the weight . This is an improvement on the ordinary polling algorithm , For example, you can set : The processing capacity of the third machine is twice that of the first machine , Then the load balancer will allocate twice the number of connections to the second 3 Taiwan machine , Polling can assign the request order to the back end according to the weight .

package com.monkeyjava.learn.basic.robin;

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

public class TestWeight {
private Integer index = 0;
static Map<String, Integer> ipMap=new HashMap<String, Integer>(16);
static {
// 1.map, key-ip,value- The weight
ipMap.put("192.168.1.1", 1);
ipMap.put("192.168.1.2", 2);
ipMap.put("192.168.1.3", 4);

}

public List<String> getServerIpByWeight() {
List<String> ips = new ArrayList<String>(32);
for (Map.Entry<String, Integer> entry : ipMap.entrySet()) {
String ip = entry.getKey();
Integer weight = entry.getValue();
// Depending on the weight , Put in list The number in is equal to the weight , The number of times polled is equal to the weight
for (int ipCount =0; ipCount < weight; ipCount++) {
ips.add(ip);
}
}
return ips;
}

public String weightRobin(){
List<String> ips = this.getServerIpByWeight();
if (index >= ips.size()){
index = 0;
}
String serverIp= ips.get(index);
index ++;
return serverIp;
}

public static void main(String[] args) {
TestWeight testWeightRobin=new TestWeight();
for (int i =0;i< 10 ;i++){
String server=testWeightRobin.weightRobin();
System.out.println(server);
}
}
}

Output results :

192.168.1.1
192.168.1.3
192.168.1.3
192.168.1.3
192.168.1.3
192.168.1.2
192.168.1.2
192.168.1.1
192.168.1.3
192.168.1.3

Weighted random method

Get random numbers with weights , Random this kind of thing , Can't see absolutely , Only relative , We don't have to index Control subscripts for polling , Only random Random sampling ip, Implementation algorithm .

package com.monkeyjava.learn.basic.robin;

import java.util.*;

public class TestRandomWeight {

static Map<String, Integer> ipMap=new HashMap<String, Integer>(16);
static {
// 1.map, key-ip,value- The weight
ipMap.put("192.168.1.1", 1);
ipMap.put("192.168.1.2", 2);
ipMap.put("192.168.1.3", 4);

}

public List<String> getServerIpByWeight() {
List<String> ips = new ArrayList<String>(32);
for (Map.Entry<String, Integer> entry : ipMap.entrySet()) {
String ip = entry.getKey();
Integer weight = entry.getValue();
// Depending on the weight , Put in list The number in is equal to the weight , The number of times polled is equal to the weight
for (int ipCount =0; ipCount < weight; ipCount++) {
ips.add(ip);
}
}
return ips;
}

public String randomWeightRobin(){
List<String> ips = this.getServerIpByWeight();
// Cyclic random number
Random random=new Random();
int index =random.nextInt(ips.size());
String serverIp = ips.get(index);
return serverIp;
}

public static void main(String[] args) {
TestRandomWeight testRandomWeightRobin=new TestRandomWeight();
for (int i =0;i< 10 ;i++){
String server= testRandomWeightRobin.randomWeightRobin();
System.out.println(server);
}
}
}

Output results :

192.168.1.3
192.168.1.3
192.168.1.2
192.168.1.1
192.168.1.2
192.168.1.1
192.168.1.3
192.168.1.2
192.168.1.2
192.168.1.3

Random method

The load balancing method randomly distributes the load to each available server , Select a server through the random number generation algorithm , This implementation algorithm is the simplest , The number of calls increases , This algorithm can achieve that the number of requests per server is close to the average .

package com.monkeyjava.learn.basic.robin;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.Random;

public class TestRandom {


private List<String> ips = Lists.newArrayList("192.168.1.1", "192.168.1.2", "192.168.1.3");


public String randomRobin(){
// random number
Random random=new Random();
int index =random.nextInt(ips.size());
String serverIp= ips.get(index);
return serverIp;

}

public static void main(String[] args) {
TestRandom testRandomdRobin =new TestRandom();
for (int i=0;i< 10 ;i++){
String serverIp= testRandomdRobin.randomRobin();
System.out.println(serverIp);
}
}
}

Output

192.168.1.3
192.168.1.3
192.168.1.1
192.168.1.2
192.168.1.1
192.168.1.3
192.168.1.2
192.168.1.3
192.168.1.3
192.168.1.2

IP_Hash Algorithm

hash(ip)%N Algorithm , Through a hashing algorithm, the client source IP The request is allocated to different servers according to the hash modulus algorithm

advantage : The same client is guaranteed IP The address will be hashed to the same back-end server , Until the back-end server list changes . According to this feature, stateful communication can be established between service consumers and service providers session conversation

shortcoming : If the server goes offline , Source IP Routing server IP Will become another , If the server does not do session Shared words , Can cause session The loss of .

package com.monkeyjava.learn.basic.robin;

import com.google.common.collect.Lists;

import java.util.List;

public class TestIpHash {


private List<String> ips = Lists.newArrayList("192.168.1.1", "192.168.1.2", "192.168.1.3");


public String ipHashRobin(String clientIp){
int hashCode=clientIp.hashCode();
int serverListsize=ips.size();
int index = hashCode%serverListsize;
String serverIp= ips.get(index);
return serverIp;

}

public static void main(String[] args) {
TestIpHash testIpHash =new TestIpHash();
String servername= testIpHash.ipHashRobin("192.168.88.2");
System.out.println(servername);
}
}

Output results

192.168.1.3

The result is the same every time

 Brother monkey said Java
Brother monkey said Java
An Internet coder ,Java Technology enthusiasts , Like to share
12 Original content
official account


版权声明
本文为[Monkey said Java]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/10/20211002145640985d.html

Scroll to Top