4-19-3
parent
6ec34a607b
commit
47840b2c5a
|
@ -39,75 +39,54 @@ public class ALYunEcsService {
|
|||
|
||||
/**
|
||||
* 根据实例ID和实例名称查询实例信息
|
||||
* @param ecsSelectModel
|
||||
* @param instanceName
|
||||
* @return 返回实例集合信息
|
||||
*/
|
||||
public List<InstanceInfo> selectEscList(EcsSelectModel ecsSelectModel){
|
||||
log.info("当前对象的参数值:" + ecsSelectModel.toString());
|
||||
public List<InstanceInfo> selectECS(String instanceName) throws Exception {
|
||||
|
||||
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
|
||||
.setRegionId(aliConfig.getRegionId());
|
||||
.setRegionId(aliConfig.getRegionId())
|
||||
.setInstanceName(instanceName)
|
||||
.setPageSize(10);
|
||||
|
||||
//判断实例名称 == null 或 实例名称 == 空
|
||||
if (ecsSelectModel.getInstanceNameList().toString() != null) {
|
||||
//不为空
|
||||
describeInstancesRequest.setInstanceName(Common.toJSONString(ecsSelectModel.getInstanceNameList()));
|
||||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||
|
||||
} else {
|
||||
// 为空 加 * 号
|
||||
describeInstancesRequest.setInstanceName("*");
|
||||
}
|
||||
|
||||
|
||||
if (ecsSelectModel.getInstanceIdList() != null) {
|
||||
describeInstancesRequest.setInstanceIds(Common.toJSONString(ecsSelectModel.getInstanceIdList()).toString());
|
||||
}
|
||||
//else {
|
||||
// describeInstancesRequest.setInstanceName(Common.toJSONString(ecsSelectModel.getInstanceNameList()));
|
||||
// }
|
||||
describeInstancesRequest.setPageSize(10);
|
||||
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
|
||||
//初始化返回值
|
||||
List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance> instanceList = null;
|
||||
List<InstanceInfo> instanceInfos = new ArrayList<>(); // 用于存储查询到的实例信息
|
||||
|
||||
try {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(describeInstancesRequest, runtime);
|
||||
DescribeInstancesResponseBody body = describeInstancesResponse.getBody();
|
||||
DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstances instances = body.getInstances();
|
||||
//能返回的值
|
||||
instanceList = instances.getInstance();
|
||||
if (instanceList == null || instanceList.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return instanceList.stream()
|
||||
.map(instance -> {
|
||||
return InstanceInfo.builder()
|
||||
.instanceId(instance.getInstanceId())
|
||||
.instanceName(instance.getInstanceName())
|
||||
.publicIpAddress(instance.getPublicIpAddress().getIpAddress().toString())
|
||||
.privateIpAddress(instance.getVpcAttributes().getPrivateIpAddress().ipAddress.toString())
|
||||
.build();
|
||||
List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance> instanceList = instances.getInstance();
|
||||
|
||||
}).toList();
|
||||
// 修改 selectECS 方法中 IP 地址的处理部分
|
||||
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance item : instanceList) {
|
||||
InstanceInfo instanceInfo = new InstanceInfo();
|
||||
instanceInfo.setInstanceId(item.getInstanceId());
|
||||
instanceInfo.setInstanceName(item.getInstanceName());
|
||||
String publicIpAddress = item.getPublicIpAddress().getIpAddress().toString();
|
||||
// 去掉方括号
|
||||
publicIpAddress = publicIpAddress.substring(1, publicIpAddress.length() - 1);
|
||||
instanceInfo.setPublicIpAddress(publicIpAddress);
|
||||
String privateIpAddress = item.getVpcAttributes().getPrivateIpAddress().ipAddress.toString();
|
||||
// 去掉方括号
|
||||
privateIpAddress = privateIpAddress.substring(1, privateIpAddress.length() - 1);
|
||||
instanceInfo.setPrivateIpAddress(privateIpAddress);
|
||||
instanceInfos.add(instanceInfo);
|
||||
}
|
||||
} catch (TeaException error) {
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
log.error("状态码是:[{}],信息是:[{}],返回的结果是:[{}]", error.getCode(), error.getMessage(), error);
|
||||
error.printStackTrace();
|
||||
// 异常处理
|
||||
} catch (Exception _error) {
|
||||
TeaException error = new TeaException();
|
||||
log.error("code:[{}],信息是:[{}],返回的结果是:[{}]",error.getCode(), error.getMessage(), error);
|
||||
_error.printStackTrace();
|
||||
// 异常处理
|
||||
}
|
||||
|
||||
return new ArrayList<>();
|
||||
|
||||
return instanceInfos;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 创建实例方法
|
||||
* @throws Exception
|
||||
|
|
|
@ -33,6 +33,15 @@ public class NodeScoreCache extends CacheAbs<String> {
|
|||
|
||||
}
|
||||
|
||||
public Long getNodeNowNum(){
|
||||
List<WorkGatewayNode> workGatewayNodes = getNodeScore();
|
||||
//目前连接数
|
||||
Long vehicleOnlineNowNum= Long.valueOf(String.valueOf(
|
||||
workGatewayNodes.stream().mapToDouble(WorkGatewayNode::getWeight).sum()
|
||||
));
|
||||
return vehicleOnlineNowNum;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
@ -16,6 +16,7 @@ import org.redisson.api.RedissonClient;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -105,10 +106,10 @@ public class GateWayLoadServicelmpl implements GateWayLoadService {
|
|||
List<WorkGatewayNode> workGatewayNodes = nodeScoreCache.getNodeScore();
|
||||
|
||||
//车辆上线总数量
|
||||
long vehicleMaxOnlineNUm = workGatewayNodes.size() * 80;
|
||||
long vehicleMaxOnlineNUm = getNodeMaxOnlineNum();
|
||||
|
||||
//目前连接数
|
||||
long veicleOnlineNowNum = Long.valueOf(String.valueOf(workGatewayNodes.stream().mapToDouble(WorkGatewayNode::getWeight).sum()));
|
||||
Long veicleOnlineNowNum = nodeScoreCache.getNodeNowNum();
|
||||
|
||||
//空余连接数
|
||||
long vehicleOnlineNum = vehicleMaxOnlineNUm - veicleOnlineNowNum;
|
||||
|
@ -168,30 +169,39 @@ public class GateWayLoadServicelmpl implements GateWayLoadService {
|
|||
// refreshLoadLock.unlock();
|
||||
//}
|
||||
|
||||
/**
|
||||
* 动态ECS
|
||||
*/
|
||||
public void dynamicECS(){
|
||||
|
||||
//车辆上线总数量
|
||||
long vehicleMaxOnlineNUm = getNodeMaxOnlineNum();
|
||||
|
||||
//目前连接数
|
||||
Long nodeNowNum = nodeScoreCache.getNodeNowNum();
|
||||
|
||||
//负载率
|
||||
BigDecimal loadRate = new BigDecimal(vehicleMaxOnlineNUm).divide(new BigDecimal(nodeNowNum), 0, BigDecimal.ROUND_HALF_UP);
|
||||
|
||||
if (loadRate.longValue() > 80){
|
||||
//扩容
|
||||
}else if (loadRate.longValue() <20 ){
|
||||
//缩容
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取最大连接数
|
||||
* @return
|
||||
*/
|
||||
public Long getNodeMaxOnlineNum(){
|
||||
List<WorkGatewayNode> workGatewayNodes = nodeScoreCache.getNodeScore();
|
||||
return workGatewayNodes.size() * 80L;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计
|
||||
*/
|
||||
//class StiNode {
|
||||
// // 使用 ConcurrentHashMap 保证线程安全
|
||||
// private static final Map<String, AtomicInteger> stiNodeMap = new ConcurrentHashMap<>();
|
||||
//
|
||||
// public static void sti(String nodeId) {
|
||||
// // 使用 computeIfAbsent 方法确保原子性操作
|
||||
// stiNodeMap.computeIfAbsent(nodeId, key -> new AtomicInteger()).incrementAndGet();
|
||||
// }
|
||||
//
|
||||
// public static Map<String, Long> show() {
|
||||
// Map<String, Long> resultMap = new HashMap<>();
|
||||
// stiNodeMap.forEach((key, val) -> {
|
||||
// resultMap.put(key, (long) val.get());
|
||||
// System.out.println(key + "--------------" + val.get());
|
||||
// });
|
||||
// return resultMap;
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -82,15 +82,15 @@ public class Collection {
|
|||
* 定时扫描节点信息
|
||||
*/
|
||||
@Scheduled(cron = "0/10 * * * * ?")
|
||||
public void scheduledEcsCompanding() {
|
||||
public void scheduledEcsCompanding() throws Exception {
|
||||
|
||||
//查询阿里云是否存在实例
|
||||
EcsSelectModel ecsSelectModel = new EcsSelectModel();
|
||||
List<String> addArryList = new ArrayList<>();
|
||||
addArryList.add("Myname");
|
||||
ecsSelectModel.setInstanceNameList(addArryList);
|
||||
// EcsSelectModel ecsSelectModel = new EcsSelectModel();
|
||||
// List<String> addArryList = new ArrayList<>();
|
||||
// addArryList.add("Myname");
|
||||
// ecsSelectModel.setInstanceNameList(addArryList);
|
||||
//实例集合
|
||||
List<InstanceInfo> instanceLists = alYunEcsService.selectEscList(ecsSelectModel);
|
||||
List<InstanceInfo> instanceLists = alYunEcsService.selectECS("Myname");
|
||||
|
||||
//节点计数
|
||||
Long nodeNumber = 0L;
|
||||
|
@ -168,12 +168,14 @@ public class Collection {
|
|||
}
|
||||
|
||||
}
|
||||
//封装节点数量和节点连接总数
|
||||
TotalNumber totalNumber = new TotalNumber();
|
||||
totalNumber.setConnectionTotal(connectionTotal);
|
||||
totalNumber.setNodeNumber(nodeNumber);
|
||||
//调用扩容方法去判断是否需要扩缩容
|
||||
contractionVolume.contractionVolume(totalNumber);
|
||||
if (connectionTotal > 0){
|
||||
//封装节点数量和节点连接总数
|
||||
TotalNumber totalNumber = new TotalNumber();
|
||||
totalNumber.setConnectionTotal(connectionTotal);
|
||||
totalNumber.setNodeNumber(nodeNumber);
|
||||
//调用扩容方法去判断是否需要扩容
|
||||
contractionVolume.contractionVolume(totalNumber);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -84,7 +84,7 @@ public class ContractionVolume {
|
|||
log.info("Node 节点负载达到 :" + value + "%,达到扩容一台的条件☑");
|
||||
try {
|
||||
//创建实例方法 【1台】
|
||||
alYunEcsService.createAnServer(null);
|
||||
//alYunEcsService.createAnServer(null);
|
||||
} catch (Exception e) {
|
||||
log.error("扩容失败!!!!!");
|
||||
e.printStackTrace();
|
||||
|
@ -96,7 +96,7 @@ public class ContractionVolume {
|
|||
log.info("Node 节点负载达到 :" + value + "%,达到扩容两台的条件☑");
|
||||
try {
|
||||
//创建实例方法 【2台】
|
||||
alYunEcsService.createAnServer(LoadConstants.IS_NULL);
|
||||
// alYunEcsService.createAnServer(LoadConstants.IS_NULL);
|
||||
} catch (Exception e) {
|
||||
log.error("扩容失败!!!!!");
|
||||
e.printStackTrace();
|
||||
|
@ -116,9 +116,9 @@ public class ContractionVolume {
|
|||
//获取节点连接总数
|
||||
Long connectionTotal = totalNumber.getConnectionTotal();
|
||||
//计算空余连接数
|
||||
Long vacantNumber = sumNodeNumber - connectionTotal;
|
||||
// Long vacantNumber = sumNodeNumber - connectionTotal;
|
||||
//计算当前负载情况
|
||||
double loadPercentage = (double)vacantNumber / sumNodeNumber;
|
||||
double loadPercentage = (double)connectionTotal / sumNodeNumber;
|
||||
//进行四舍五入取整
|
||||
long roundLoadPercentage = Math.round(loadPercentage) * LoadConstants.BE_COMMON;
|
||||
//返回百分比
|
||||
|
|
Loading…
Reference in New Issue