138 lines
4.3 KiB
Java
138 lines
4.3 KiB
Java
package com.loadCenter.aliyun.job;
|
||
|
||
import com.loadCenter.aliyun.common.aliyun.service.AliYunEcsService;
|
||
import com.loadCenter.aliyun.gateway.cache.GatewayIpLoadCountKey;
|
||
import com.loadCenter.aliyun.gateway.cache.GatewayNodeIDCache;
|
||
import com.loadCenter.aliyun.gateway.cache.GatewayNodeInfoCache;
|
||
import com.loadCenter.aliyun.gateway.model.GatewayNodeInfo;
|
||
import com.loadCenter.aliyun.gateway.model.NodeLoadNum;
|
||
import lombok.extern.log4j.Log4j2;
|
||
import org.springframework.scheduling.annotation.Scheduled;
|
||
import org.springframework.stereotype.Component;
|
||
|
||
import java.util.List;
|
||
|
||
|
||
/**
|
||
* @ClassName ScheduledTask
|
||
* @Author ZeJinG.Su
|
||
*/
|
||
@Component
|
||
@Log4j2
|
||
public class ScheduledTask {
|
||
|
||
/*
|
||
* 阿里云api接口类
|
||
* */
|
||
private final AliYunEcsService aliYunEcsService;
|
||
|
||
/*
|
||
* 操作缓存
|
||
* */
|
||
private final GatewayIpLoadCountKey gatewayIpLoadCountKey;
|
||
|
||
private final GatewayNodeIDCache gatewayNodeIdCache;
|
||
|
||
private final GatewayNodeInfoCache gatewayNodeInfoCache;
|
||
|
||
public ScheduledTask(AliYunEcsService aliYunEcsService, GatewayIpLoadCountKey gatewayIpLoadCountKey, GatewayNodeIDCache gatewayNodeIdCache, GatewayNodeInfoCache gatewayNodeInfoCache) {
|
||
this.aliYunEcsService = aliYunEcsService;
|
||
this.gatewayIpLoadCountKey = gatewayIpLoadCountKey;
|
||
this.gatewayNodeIdCache = gatewayNodeIdCache;
|
||
this.gatewayNodeInfoCache = gatewayNodeInfoCache;
|
||
}
|
||
|
||
|
||
@Scheduled(cron = "0/10 * * * * ?")
|
||
/**
|
||
* 动态扩容
|
||
*/
|
||
public void dynamicExpansion() {
|
||
//先获取所有的负载列表
|
||
List<NodeLoadNum> ipAndLoadCounts = gatewayIpLoadCountKey.get();
|
||
if (ipAndLoadCounts.isEmpty()) {
|
||
log.error("负载量列表为空!");
|
||
return;
|
||
}
|
||
|
||
//计算所有节点的负载
|
||
Long connectSize = ipAndLoadCounts.stream().mapToLong(NodeLoadNum::getLoadNum).sum();
|
||
|
||
//求出平均值
|
||
Long avg = connectSize / 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("暂时不需要扩容");
|
||
}
|
||
}
|
||
|
||
|
||
@Scheduled(cron = "0/10 * * * * ?")
|
||
/**
|
||
* 动态缩容
|
||
*/
|
||
public void dynamicReduction() {
|
||
//求出所有的节点ID
|
||
List<String> nodeIds = gatewayNodeIdCache.get();
|
||
|
||
//如果节点数量小于等于1,则不执行缩容,至少保留一个节点
|
||
if (nodeIds.size() <= 1) {
|
||
log.info("暂无节点可删除!");
|
||
return;
|
||
}
|
||
|
||
|
||
//先获取所有的负载列表
|
||
List<NodeLoadNum> ipAndLoadCounts = gatewayIpLoadCountKey.get();
|
||
if (ipAndLoadCounts.size() <= 1) {
|
||
log.error("负载列表为空!");
|
||
return;
|
||
}
|
||
|
||
//获取节点信息(IP)集合
|
||
List<GatewayNodeInfo> gatewayNodeInfoList = gatewayNodeInfoCache.get();
|
||
if (gatewayNodeInfoList.isEmpty()) {
|
||
log.error("节点信息为空!");
|
||
return;
|
||
}
|
||
|
||
//判断哪个节点的负载小于30
|
||
|
||
for (GatewayNodeInfo gatewayNodeInfo : gatewayNodeInfoList) {//获取节点的IP
|
||
String ip = gatewayNodeInfo.getPublicIpAddress();
|
||
//获取当前循环节点的负载
|
||
Long loadCount = ipAndLoadCounts.stream().filter(ipAndLoadCount -> ipAndLoadCount.getIpName().equals(ip)).findFirst().get().getLoadNum();
|
||
//判断节点的负载是否小于30
|
||
if (loadCount < 30) {
|
||
//对这个节点进行缩容
|
||
String instanceId = gatewayNodeInfo.getInstanceId();
|
||
try {
|
||
aliYunEcsService.releaseInstances(instanceId);
|
||
} catch (Exception e) {
|
||
throw new RuntimeException("节点缩容失败!" + e.getMessage());
|
||
}
|
||
log.info("节点缩容成功");
|
||
//一次只删除一个节点
|
||
break;
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
|
||
}
|