mirror of
https://gitee.com/dromara/sms4j.git
synced 2025-12-06 17:08:40 +08:00
添加了一个平滑加权算法的负载均衡调用
This commit is contained in:
parent
379605f0d2
commit
bd6cb06d7e
@ -0,0 +1,48 @@
|
||||
package org.dromara.sms4j.core.load;
|
||||
|
||||
import org.dromara.sms4j.api.SmsBlend;
|
||||
|
||||
public class LoadServer {
|
||||
private SmsBlend smsServer;
|
||||
private int weight;
|
||||
private int currentWeight;
|
||||
|
||||
protected LoadServer(SmsBlend smsServer, int weight, int currentWeight) {
|
||||
this.smsServer = smsServer;
|
||||
this.weight = weight;
|
||||
this.currentWeight = currentWeight;
|
||||
}
|
||||
|
||||
protected SmsBlend getSmsServer() {
|
||||
return smsServer;
|
||||
}
|
||||
|
||||
protected int getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
protected int getCurrentWeight() {
|
||||
return currentWeight;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 smsServer
|
||||
*/
|
||||
protected void setSmsServer(SmsBlend smsServer) {
|
||||
this.smsServer = smsServer;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 weight
|
||||
*/
|
||||
protected void setWeight(int weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 currentWeight
|
||||
*/
|
||||
protected void setCurrentWeight(int currentWeight) {
|
||||
this.currentWeight = currentWeight;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,70 @@
|
||||
package org.dromara.sms4j.core.load;
|
||||
|
||||
import org.dromara.sms4j.api.SmsBlend;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* SmsLoad
|
||||
* <p> 自定义的一个平滑加权负载服务,可以用于存放多个短信实现负载
|
||||
* @author :Wind
|
||||
* 2023/4/21 20:49
|
||||
**/
|
||||
public class SmsLoad {
|
||||
// 服务器列表,每个服务器有一个权重和当前权重
|
||||
private static final List<LoadServer> LoadServers = new ArrayList<>();
|
||||
|
||||
private SmsLoad() {
|
||||
}
|
||||
|
||||
/**
|
||||
* addLoadServer
|
||||
* <p> 添加服务及其权重
|
||||
* <p>添加权重应注意,不要把某个权重设置的与其他权重相差过大,否则容易出现无法负载,单一选择的情况
|
||||
* @param LoadServer 短信实现
|
||||
* @param weight 权重
|
||||
* @author :Wind
|
||||
*/
|
||||
public static void addLoadServer(SmsBlend LoadServer, int weight) {
|
||||
LoadServers.add(new LoadServer(LoadServer, weight, weight));
|
||||
}
|
||||
|
||||
/**
|
||||
* removeLoadServer
|
||||
* <p>移除短信服务
|
||||
* @param LoadServer 要移除的服务
|
||||
* @author :Wind
|
||||
*/
|
||||
public static void removeLoadServer(SmsBlend LoadServer) {
|
||||
for (int i = 0; i < LoadServers.size(); i++) {
|
||||
if (LoadServers.get(i).getSmsServer().equals(LoadServer)) {
|
||||
LoadServers.remove(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取一个服务
|
||||
public static SmsBlend getLoadServer() {
|
||||
int totalWeight = 0;
|
||||
LoadServer selectedLoadServer = null;
|
||||
// 计算所有服务器的权重总和,并选择当前权重最大的服务器
|
||||
for (LoadServer LoadServer : LoadServers) {
|
||||
totalWeight += LoadServer.getWeight();
|
||||
int currentWeight = LoadServer.getCurrentWeight() + LoadServer.getWeight();
|
||||
LoadServer.setCurrentWeight(currentWeight);
|
||||
if (selectedLoadServer == null || LoadServer.getCurrentWeight() > selectedLoadServer.getCurrentWeight()) {
|
||||
selectedLoadServer = LoadServer;
|
||||
}
|
||||
}
|
||||
// 如果没有服务器,则返回空
|
||||
if (selectedLoadServer == null) {
|
||||
return null;
|
||||
}
|
||||
// 更新选择的服务器的当前权重,并返回其地址
|
||||
int i = selectedLoadServer.getCurrentWeight() - totalWeight;
|
||||
selectedLoadServer.setCurrentWeight(i);
|
||||
return selectedLoadServer.getSmsServer();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user