import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
class Server {
String name;
int weight;
public Server(String name, int weight) {
this.name = name;
this.weight = weight;
}
}
class WeightedRoundRobinLoadBalancer {
List<Server> servers;
AtomicInteger currentIndex;
int currentWeight;
int maxWeight;
int gcd;
public WeightedRoundRobinLoadBalancer(List<Server> servers) {
this.servers = servers;
this.currentIndex = new AtomicInteger(0);
this.currentWeight = 0;
this.maxWeight = servers.stream().mapToInt(server -> server.weight).max().orElse(1);
this.gcd = calculateGCD(servers);
}
public Server getServer() {
while (true) {
int index = currentIndex.getAndIncrement() % servers.size();
if (index < 0) {
index += servers.size();
}
Server selectedServer = servers.get(index);
currentWeight += gcd;
if (currentWeight > maxWeight) {
currentWeight = 0;
}
if (selectedServer.weight >= currentWeight) {
return selectedServer;
}
}
}
private static int calculateGCD(List<Server> servers) { //计算最大公约数
int result = servers.get(0).weight;
for (Server server : servers) {
result = calculateGCD(result, server.weight);
}
return result;
}
private static int calculateGCD(int a, int b) {
return b == 0 ? a : calculateGCD(b, a % b);
}
}
public class Main {
public static void main(String[] args) {
Server server1 = new Server("Server 1", 2);
Server server2 = new Server("Server 2", 1);
Server server3 = new Server("Server 3", 2);
Server server4 = new Server("Server 4", 3);
Server server5 = new Server("Server 5", 2);
List<Server> servers = new ArrayList<>();
servers.add(server1);
servers.add(server2);
servers.add(server3);
servers.add(server4);
servers.add(server5);
WeightedRoundRobinLoadBalancer loadBalancer = new WeightedRoundRobinLoadBalancer(servers);
int j = 0;
int k = 0;
int l= 0;
// Perform some requests
for (int i = 0; i < 100000; i++) {
Server selectedServer = loadBalancer.getServer();
if (selectedServer.weight == 3) {
j++;
}
if (selectedServer.weight == 2) {
k++;
}
if (selectedServer.weight == 1) {
l++;
}
System.out.println("Request " + (i + 1) + " sent to: " + selectedServer.name);
}
System.out.println("3==" +j);
System.out.println("2==" +k);
System.out.println("1==" +l);
}
}
简单版本
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class Server {
String name;
int weight;
public Server(String name, int weight) {
this.name = name;
this.weight = weight;
}
}
class LoadBalancer {
List<Server> servers;
Random random;
public LoadBalancer(List<Server> servers) {
this.servers = servers;
this.random = new Random();
}
public Server getServer() {
// 计算总权重
int totalWeight = servers.stream().mapToInt(server -> server.weight).sum();
// 生成一个介于0和总权重之间的随机数
int randomNum = random.nextInt(totalWeight);
System.out.println("权重随机数:" + randomNum);
int currentWeight = 0;
for (Server server : servers) {
currentWeight += server.weight;
if (randomNum < currentWeight) {
return server;
}
}
return servers.get(servers.size() - 1);
}
}
public class Main {
public static void main(String[] args) {
Server server1 = new Server("Server 1", 20);
Server server2 = new Server("Server 2", 10);
Server server3 = new Server("Server 3", 10);
Server server4 = new Server("Server 4", 40);
Server server5 = new Server("Server 5", 20);
Server server6 = new Server("Server 6", 10);
List<Server> servers = new ArrayList<>();
servers.add(server1);
servers.add(server2);
servers.add(server3);
servers.add(server4);
servers.add(server5);
servers.add(server6);
LoadBalancer loadBalancer = new LoadBalancer(servers);
int j = 0;
int k = 0;
int d = 0;
for (int i = 0; i < 10000; i++) {
Server selectedServer = loadBalancer.getServer();
if (selectedServer.weight == 40) {
j++;
}
if (selectedServer.weight == 20) {
k++;
}
if (selectedServer.weight == 10) {
d++;
}
System.out.println("Request " + (i + 1) + " sent to: " + selectedServer.name);
}
System.out.println("40==" + j);
System.out.println("20==" + k);
System.out.println("10==" + d);
}
}