16 动态扩缩容优化

master
liuyunhu 2024-04-20 09:02:55 +08:00
parent 273029f953
commit 77e9045d6f
1 changed files with 52 additions and 66 deletions

View File

@ -46,14 +46,14 @@ public class Timer {
/**
*
*
*
* @Date: 2024/4/19 9:44
* @Date: 2024/4/20 8:52
* @Param: []
* @Return: void
**/
@Scheduled(cron = "0/10 * * * * ?")
public void dynamicExpansion() {
public void dynamicScaling() {
//先获取所有的负载列表
List<IpAndLoadCount> ipAndLoadCounts = gatewayIpAndLoadCountCache.get();
if (ipAndLoadCounts.isEmpty()) {
@ -64,54 +64,11 @@ public class Timer {
//计算所有节点的负载
int connectSize = ipAndLoadCounts.stream().mapToInt(IpAndLoadCount::getLoadCount).sum();
//求出平均值
int avg = connectSize / ipAndLoadCounts.size();
//求出节点数量
int nodeCount = ipAndLoadCounts.size();
if (avg >= 80) {
//执行节点扩容
//返回实例的ID
String instanceId;
try {
instanceId = aliYunEcsService.createAndRunInstance();
} catch (Exception e) {
throw new RuntimeException("节点扩容失败!" + e.getMessage());
}
if (!instanceId.isEmpty()) {
log.info("扩容 成功扩容的节点ip为{}" , instanceId);
}
} else {
log.info("暂时不需要扩容");
}
}
/**
*
*
* @Date: 2024/4/19 9:44
* @Param: []
* @Return: void
**/
@Scheduled(cron = "0/10 * * * * ?")
public void dynamicReduction() {
//求出所有的节点ID
List<String> nodeIds = gatewayNodeIdCache.get();
//如果节点数量小于等于1则不执行缩容至少保留一个节点
if (nodeIds.size() <= 1) {
log.info("暂无节点可删除!");
return;
}
//先获取所有的负载列表
List<IpAndLoadCount> ipAndLoadCounts = gatewayIpAndLoadCountCache.get();
if (ipAndLoadCounts.size() <= 1) {
log.error("负载列表为空!");
return;
}
//计算平均负载
int avgLoad = connectSize / nodeCount;
//获取节点信息IP集合
List<GatewayNodeInfo> gatewayNodeInfoList = gatewayNodeInfoCache.get();
@ -120,27 +77,56 @@ public class Timer {
return;
}
//判断哪个节点的负载小于30
//处理节点扩容或缩容
boolean scalingPerformed = false;
for (GatewayNodeInfo gatewayNodeInfo : gatewayNodeInfoList) {//获取节点的IP
String ip = gatewayNodeInfo.getPublicIpAddress();
//获取当前循环节点的负载
int loadCount = ipAndLoadCounts.stream().filter(ipAndLoadCount -> ipAndLoadCount.getIp().equals(ip)).findFirst().get().getLoadCount();
//判断节点的负载是否小于30
if (loadCount < 30) {
//对这个节点进行缩容
String instanceId = gatewayNodeInfo.getInstanceId();
try {
aliYunEcsService.releaseInstances(instanceId);
} catch (Exception e) {
throw new RuntimeException("节点缩容失败!" + e.getMessage());
if (avgLoad >= 5) {
//执行节点扩容
try {
String instanceId = aliYunEcsService.createAndRunInstance();
if (!instanceId.isEmpty()) {
log.info("**********************扩容 成功!**********************扩容的节点ip为{}", instanceId);
scalingPerformed = true;
}
} catch (Exception e) {
throw new RuntimeException("节点扩容失败!" + e.getMessage());
}
} else {
//检查是否有节点需要缩容
if (nodeCount > 1) {
for (GatewayNodeInfo gatewayNodeInfo : gatewayNodeInfoList) {
String ip = gatewayNodeInfo.getPublicIpAddress();
//获取当前循环IP 的负载量
Integer loadCount = ipAndLoadCounts.stream()
.filter(ipAndLoadCount -> ipAndLoadCount.getIp().equals(ip))
.findFirst()
.get()
.getLoadCount();
//如果负载小于30则执行缩容
if (loadCount < 3) {
String instanceId = gatewayNodeInfo.getInstanceId();
try {
aliYunEcsService.releaseInstances(instanceId);
log.info("**********************节点缩容成功!**********************");
scalingPerformed = true;
break;//一次只释放一个节点
} catch (Exception e) {
throw new RuntimeException("节点缩容失败!" + e.getMessage());
}
}
}
log.info("节点缩容成功");
//一次只删除一个节点
break;
}
}
if (!scalingPerformed) {
log.info("**********************当前系统负载均衡,无需进行节点扩容或缩容**********************");
}
}