4-19-3
parent
6ec34a607b
commit
47840b2c5a
|
@ -39,75 +39,54 @@ public class ALYunEcsService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据实例ID和实例名称查询实例信息
|
* 根据实例ID和实例名称查询实例信息
|
||||||
* @param ecsSelectModel
|
* @param instanceName
|
||||||
* @return 返回实例集合信息
|
* @return 返回实例集合信息
|
||||||
*/
|
*/
|
||||||
public List<InstanceInfo> selectEscList(EcsSelectModel ecsSelectModel){
|
public List<InstanceInfo> selectECS(String instanceName) throws Exception {
|
||||||
log.info("当前对象的参数值:" + ecsSelectModel.toString());
|
|
||||||
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
|
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
|
||||||
.setRegionId(aliConfig.getRegionId());
|
.setRegionId(aliConfig.getRegionId())
|
||||||
|
.setInstanceName(instanceName)
|
||||||
|
.setPageSize(10);
|
||||||
|
|
||||||
//判断实例名称 == null 或 实例名称 == 空
|
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||||
if (ecsSelectModel.getInstanceNameList().toString() != null) {
|
|
||||||
//不为空
|
|
||||||
describeInstancesRequest.setInstanceName(Common.toJSONString(ecsSelectModel.getInstanceNameList()));
|
|
||||||
|
|
||||||
} else {
|
List<InstanceInfo> instanceInfos = new ArrayList<>(); // 用于存储查询到的实例信息
|
||||||
// 为空 加 * 号
|
|
||||||
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;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 复制代码运行请自行打印 API 的返回值
|
|
||||||
DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(describeInstancesRequest, runtime);
|
DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(describeInstancesRequest, runtime);
|
||||||
DescribeInstancesResponseBody body = describeInstancesResponse.getBody();
|
DescribeInstancesResponseBody body = describeInstancesResponse.getBody();
|
||||||
DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstances instances = body.getInstances();
|
DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstances instances = body.getInstances();
|
||||||
//能返回的值
|
List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance> instanceList = instances.getInstance();
|
||||||
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();
|
|
||||||
|
|
||||||
}).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) {
|
} catch (TeaException error) {
|
||||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
error.printStackTrace();
|
||||||
// 错误 message
|
// 异常处理
|
||||||
log.error("状态码是:[{}],信息是:[{}],返回的结果是:[{}]", error.getCode(), error.getMessage(), error);
|
|
||||||
} catch (Exception _error) {
|
} catch (Exception _error) {
|
||||||
TeaException error = new TeaException();
|
_error.printStackTrace();
|
||||||
log.error("code:[{}],信息是:[{}],返回的结果是:[{}]",error.getCode(), error.getMessage(), error);
|
// 异常处理
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ArrayList<>();
|
return instanceInfos;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建实例方法
|
* 创建实例方法
|
||||||
* @throws Exception
|
* @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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -105,10 +106,10 @@ public class GateWayLoadServicelmpl implements GateWayLoadService {
|
||||||
List<WorkGatewayNode> workGatewayNodes = nodeScoreCache.getNodeScore();
|
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;
|
long vehicleOnlineNum = vehicleMaxOnlineNUm - veicleOnlineNowNum;
|
||||||
|
@ -168,30 +169,39 @@ public class GateWayLoadServicelmpl implements GateWayLoadService {
|
||||||
// refreshLoadLock.unlock();
|
// 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 * * * * ?")
|
@Scheduled(cron = "0/10 * * * * ?")
|
||||||
public void scheduledEcsCompanding() {
|
public void scheduledEcsCompanding() throws Exception {
|
||||||
|
|
||||||
//查询阿里云是否存在实例
|
//查询阿里云是否存在实例
|
||||||
EcsSelectModel ecsSelectModel = new EcsSelectModel();
|
// EcsSelectModel ecsSelectModel = new EcsSelectModel();
|
||||||
List<String> addArryList = new ArrayList<>();
|
// List<String> addArryList = new ArrayList<>();
|
||||||
addArryList.add("Myname");
|
// addArryList.add("Myname");
|
||||||
ecsSelectModel.setInstanceNameList(addArryList);
|
// ecsSelectModel.setInstanceNameList(addArryList);
|
||||||
//实例集合
|
//实例集合
|
||||||
List<InstanceInfo> instanceLists = alYunEcsService.selectEscList(ecsSelectModel);
|
List<InstanceInfo> instanceLists = alYunEcsService.selectECS("Myname");
|
||||||
|
|
||||||
//节点计数
|
//节点计数
|
||||||
Long nodeNumber = 0L;
|
Long nodeNumber = 0L;
|
||||||
|
@ -168,12 +168,14 @@ public class Collection {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
//封装节点数量和节点连接总数
|
if (connectionTotal > 0){
|
||||||
TotalNumber totalNumber = new TotalNumber();
|
//封装节点数量和节点连接总数
|
||||||
totalNumber.setConnectionTotal(connectionTotal);
|
TotalNumber totalNumber = new TotalNumber();
|
||||||
totalNumber.setNodeNumber(nodeNumber);
|
totalNumber.setConnectionTotal(connectionTotal);
|
||||||
//调用扩容方法去判断是否需要扩缩容
|
totalNumber.setNodeNumber(nodeNumber);
|
||||||
contractionVolume.contractionVolume(totalNumber);
|
//调用扩容方法去判断是否需要扩容
|
||||||
|
contractionVolume.contractionVolume(totalNumber);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,7 +84,7 @@ public class ContractionVolume {
|
||||||
log.info("Node 节点负载达到 :" + value + "%,达到扩容一台的条件☑");
|
log.info("Node 节点负载达到 :" + value + "%,达到扩容一台的条件☑");
|
||||||
try {
|
try {
|
||||||
//创建实例方法 【1台】
|
//创建实例方法 【1台】
|
||||||
alYunEcsService.createAnServer(null);
|
//alYunEcsService.createAnServer(null);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("扩容失败!!!!!");
|
log.error("扩容失败!!!!!");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -96,7 +96,7 @@ public class ContractionVolume {
|
||||||
log.info("Node 节点负载达到 :" + value + "%,达到扩容两台的条件☑");
|
log.info("Node 节点负载达到 :" + value + "%,达到扩容两台的条件☑");
|
||||||
try {
|
try {
|
||||||
//创建实例方法 【2台】
|
//创建实例方法 【2台】
|
||||||
alYunEcsService.createAnServer(LoadConstants.IS_NULL);
|
// alYunEcsService.createAnServer(LoadConstants.IS_NULL);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("扩容失败!!!!!");
|
log.error("扩容失败!!!!!");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -116,9 +116,9 @@ public class ContractionVolume {
|
||||||
//获取节点连接总数
|
//获取节点连接总数
|
||||||
Long connectionTotal = totalNumber.getConnectionTotal();
|
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;
|
long roundLoadPercentage = Math.round(loadPercentage) * LoadConstants.BE_COMMON;
|
||||||
//返回百分比
|
//返回百分比
|
||||||
|
|
Loading…
Reference in New Issue