缩容
master
JangCan 2024-04-20 10:48:49 +08:00
parent 9c409d7ab5
commit 5f5972e6b3
2 changed files with 37 additions and 10 deletions

View File

@ -18,6 +18,6 @@ public class WorkGatewayNode {
/**
*
*/
private Double source;
private int weight;
}

View File

@ -137,12 +137,12 @@ public class GatewayLoadServiceImpl implements GatewayLoadService {
//空余连接数
Long vehicleOnlineNum = vehicleMaxOnlineNum - vehicleNowOnlineNum;
List<String> loadNodeList =new ArrayList<>();
List<String> loadNodeList = new ArrayList<>();
List<WorkGatewayNodeSource> workGatewayNodeSources = workGatewayNodes.stream()
.map(workGatewayNode ->
WorkGatewayNodeSource.builder()
.nodeId(workGatewayNode.getNodeId())
.weight(Integer.parseInt(String.valueOf(vehicleOnlineNum / (nodeMaxNum - workGatewayNode.getSource()))))
.weight(Integer.parseInt(String.valueOf(vehicleOnlineNum / (nodeMaxNum - workGatewayNode.getWeight()))))
.build())
.toList();
// 计算节点列表中所有节点的权重之和
@ -164,7 +164,8 @@ public class GatewayLoadServiceImpl implements GatewayLoadService {
}
}
// 当所有节点权重为0时跳出循环
whFor:while (true) {
whFor:
while (true) {
// 遍历节点列表将权重大于0的节点ID添加到loadNodeList中并将节点权重减1
for (WorkGatewayNodeSource workGatewayNodeSource : workGatewayNodeSources) {
int weight = workGatewayNodeSource.getWeight();
@ -187,7 +188,7 @@ public class GatewayLoadServiceImpl implements GatewayLoadService {
}
/**
* ECS
* ECS
*/
public void dynamicEcs() throws Exception {
//上线最大数量
@ -196,15 +197,15 @@ public class GatewayLoadServiceImpl implements GatewayLoadService {
Long vehicleOnlineNowNum = gatewayNodeScoreCache.getNodeNowNum();
BigDecimal loadRate = new BigDecimal(vehicleMaxOnlineNum).divide(new BigDecimal(vehicleOnlineNowNum), 0, BigDecimal.ROUND_HALF_UP);
log.info("负载率:[{}]",loadRate);
log.info("负载率:[{}]", loadRate);
ArrayList<WorkGatewayNode> nodeList = gatewayZSetNodeCache.get();
if(loadRate.longValue()>=80L){
if (loadRate.longValue() >= 80L) {
//调用扩容逻辑
log.info("负载过高,开始扩容");
String instanceId = aliYunEcsService.RunInstances();
log.info("扩容的节点是:[{}]",instanceId);
log.info("扩容的节点是:[{}]", instanceId);
//休眠5秒确保新实例创建完成
Thread.sleep(5000);
@ -219,11 +220,37 @@ public class GatewayLoadServiceImpl implements GatewayLoadService {
gatewayNodeCache.put(gatewayNodeInfo);
gatewayZSetNodeCache.put(instancesInformation.getPublicIpAddress(),0);
gatewayZSetNodeCache.put(instancesInformation.getPublicIpAddress(), 0);
log.info("实例id和公网ip存入redis");
}else if (loadRate.longValue()<20L){
} else if (loadRate.longValue() < 20L) {
if (nodeList.size() > 2) {
log.info("负载率:[{}]", loadRate);
log.info("负载为:[{]]过低,开始缩容", loadRate.longValue());
WorkGatewayNode minConnectionNode = null;
int minConnections = Integer.MAX_VALUE;
for (WorkGatewayNode node : nodeList) {
int nodeConnections = node.getWeight();
if (nodeConnections < minConnections) {
minConnections = nodeConnections;
minConnectionNode = node;
}
}
// minConnectionNode 现在存储了连接数最少的节点
log.info("连接数最少的节点为:[{}]", minConnectionNode.getNodeId());
//先删除zSet,不让车连接
gatewayZSetNodeCache.remove(minConnectionNode.getNodeId());
GatewayNodeInfo gatewayNodeInfo = gatewayNodeCache.get(minConnectionNode.getNodeId());
log.info("删除节点:[{}]",gatewayNodeInfo.getNodeId());
}
}
}