16 动态扩缩容优化
parent
273029f953
commit
77e9045d6f
|
@ -46,14 +46,14 @@ public class Timer {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 动态扩容
|
* 动态扩缩容
|
||||||
*
|
*
|
||||||
* @Date: 2024/4/19 9:44
|
* @Date: 2024/4/20 8:52
|
||||||
* @Param: []
|
* @Param: []
|
||||||
* @Return: void
|
* @Return: void
|
||||||
**/
|
**/
|
||||||
@Scheduled(cron = "0/10 * * * * ?")
|
@Scheduled(cron = "0/10 * * * * ?")
|
||||||
public void dynamicExpansion() {
|
public void dynamicScaling() {
|
||||||
//先获取所有的负载列表
|
//先获取所有的负载列表
|
||||||
List<IpAndLoadCount> ipAndLoadCounts = gatewayIpAndLoadCountCache.get();
|
List<IpAndLoadCount> ipAndLoadCounts = gatewayIpAndLoadCountCache.get();
|
||||||
if (ipAndLoadCounts.isEmpty()) {
|
if (ipAndLoadCounts.isEmpty()) {
|
||||||
|
@ -64,54 +64,11 @@ public class Timer {
|
||||||
//计算所有节点的负载
|
//计算所有节点的负载
|
||||||
int connectSize = ipAndLoadCounts.stream().mapToInt(IpAndLoadCount::getLoadCount).sum();
|
int connectSize = ipAndLoadCounts.stream().mapToInt(IpAndLoadCount::getLoadCount).sum();
|
||||||
|
|
||||||
//求出平均值
|
//求出节点数量
|
||||||
int avg = connectSize / ipAndLoadCounts.size();
|
int nodeCount = ipAndLoadCounts.size();
|
||||||
|
|
||||||
if (avg >= 80) {
|
//计算平均负载
|
||||||
//执行节点扩容
|
int avgLoad = connectSize / nodeCount;
|
||||||
|
|
||||||
//返回实例的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;
|
|
||||||
}
|
|
||||||
|
|
||||||
//获取节点信息(IP)集合
|
//获取节点信息(IP)集合
|
||||||
List<GatewayNodeInfo> gatewayNodeInfoList = gatewayNodeInfoCache.get();
|
List<GatewayNodeInfo> gatewayNodeInfoList = gatewayNodeInfoCache.get();
|
||||||
|
@ -120,27 +77,56 @@ public class Timer {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//判断哪个节点的负载小于30
|
//处理节点扩容或缩容
|
||||||
|
boolean scalingPerformed = false;
|
||||||
|
|
||||||
for (GatewayNodeInfo gatewayNodeInfo : gatewayNodeInfoList) {//获取节点的IP
|
|
||||||
String ip = gatewayNodeInfo.getPublicIpAddress();
|
if (avgLoad >= 5) {
|
||||||
//获取当前循环节点的负载
|
//执行节点扩容
|
||||||
int loadCount = ipAndLoadCounts.stream().filter(ipAndLoadCount -> ipAndLoadCount.getIp().equals(ip)).findFirst().get().getLoadCount();
|
try {
|
||||||
//判断节点的负载是否小于30
|
String instanceId = aliYunEcsService.createAndRunInstance();
|
||||||
if (loadCount < 30) {
|
if (!instanceId.isEmpty()) {
|
||||||
//对这个节点进行缩容
|
log.info("**********************扩容 成功!**********************扩容的节点ip为:{}", instanceId);
|
||||||
String instanceId = gatewayNodeInfo.getInstanceId();
|
scalingPerformed = true;
|
||||||
try {
|
}
|
||||||
aliYunEcsService.releaseInstances(instanceId);
|
} catch (Exception e) {
|
||||||
} catch (Exception e) {
|
throw new RuntimeException("节点扩容失败!" + e.getMessage());
|
||||||
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("**********************当前系统负载均衡,无需进行节点扩容或缩容**********************");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue