Load balance algorithm with Java

Captain
Captain
Sep 7, 2018 · 2 min read

it is a simple demo to implement a load balance module in java.and the basic algorithms are RoundRobin,WeightRoundRobin,IpHash,Random,WeightRandom.

IpPool and LoadBalance Interface:

package com.spacex.concurrent.loadbalance;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class IpPool {
public static Map<String, Integer> ipMap = new ConcurrentHashMap<>();

static {
ipMap.put("192.168.1.1", 10);
ipMap.put("192.168.1.2", 10);
ipMap.put("192.168.1.3", 10);
ipMap.put("192.168.1.4", 10);
ipMap.put("192.168.1.5", 10);
ipMap.put("192.168.1.6", 10);
ipMap.put("192.168.1.7", 10);
ipMap.put("192.168.1.8", 10);
ipMap.put("192.168.1.9", 10);
ipMap.put("192.168.1.10", 10);
}
}
package com.spacex.concurrent.loadbalance;

public interface LoadBalance {
String getServer(String clientIp);
}

RoundRobin:

package com.spacex.concurrent.loadbalance;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class RoundRobin implements LoadBalance {
private static Integer position = 0;

@Override
public String getServer(String clientIp) {
Set<String> servers = IpPool.ipMap.keySet();
List<String> serverList = new ArrayList<>();
serverList.addAll(servers);
String target = null;

synchronized (position) {
if (position > serverList.size() - 1) {
position = 0;
}
target = serverList.get(position);
position++;
}
return target;
}
}

Random:

package com.spacex.concurrent.loadbalance;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class RandomLoadBalance implements LoadBalance {

@Override
public String getServer(String clientIp) {
Set<String> servers = IpPool.ipMap.keySet();
List<String> serverList = new ArrayList<>();
serverList.addAll(servers);
int randomIndex = new Random().nextInt(serverList.size());
String target = serverList.get(randomIndex);

return target;
}
}

WeightRandom:

package com.spacex.concurrent.loadbalance;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class WeightRandom implements LoadBalance {
@Override
public String getServer(String clientIp) {
Set<String> servers = IpPool.ipMap.keySet();
List<String> serverList = new ArrayList<>();

Iterator<String> iterator = servers.iterator();
while (iterator.hasNext()) {
String server = iterator.next();
Integer weight = IpPool.ipMap.get(server);
if (weight != null && weight > 0) {
for (int i = 0; i < weight; i++) {
serverList.add(server);
}
}
}

Integer index = new Random().nextInt(serverList.size());
String target = serverList.get(index);
return target;
}
}

WeightRoundRobin:

package com.spacex.concurrent.loadbalance;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class WeightRoundRobin implements LoadBalance {

private static Integer position = 0;

@Override
public String getServer(String clientIp) {
Set<String> servers = IpPool.ipMap.keySet();
List<String> serverList = new ArrayList<>();

Iterator<String> iterator = servers.iterator();
while (iterator.hasNext()) {
String serverItem = iterator.next();
Integer weight = IpPool.ipMap.get(serverItem);
if (weight > 0) {
for (int i = 0; i < weight; i++) {
serverList.add(serverItem);
}
}

}

synchronized (position) {
if (position > serverList.size()) {
position = 0;
}

String target = serverList.get(position);
position++;
return target;
}
}
}

IpHash:

package com.spacex.concurrent.loadbalance;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class IpHash implements LoadBalance {

@Override
public String getServer(String clientIp) {
if (clientIp == null) {
clientIp = "127.0.0.1";
}
Set<String> servers = IpPool.ipMap.keySet();
List<String> serverList = new ArrayList<>();
serverList.addAll(servers);
String remoteId = clientIp;
Integer index = remoteId.hashCode() % serverList.size();
String target = serverList.get(index);
return target;
}
}

Test Case:

package com.spacex.concurrent.loadbalance;

public class LoadBalanceMain {
public static void main(String[] args) {
run();
}

public static void run() {
loadBalance();
}

public static void loadBalance() {
doGetServer(new RoundRobin());
doGetServer(new RandomLoadBalance());
doGetServer(new IpHash());
doGetServer(new WeightRoundRobin());
doGetServer(new WeightRandom());
}


public static void doGetServer(LoadBalance loadBalance) {
doGetServer(loadBalance, 100);
}

private static void doGetServer(LoadBalance loadBalance, int queryTimes) {
for (int i = 0; i < queryTimes; i++) {
String serverId = loadBalance.getServer(String.valueOf(i));
System.out.println(String.format("[%s] index:%s,%s", loadBalance.getClass().getSimpleName(), i, serverId));
}
}
}

if you have any questions, just feel free to text me.

Written by

Captain

code,eat and sleep like there is no tomorrow

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade