parent
9c409d7ab5
commit
5f5972e6b3
|
@ -18,6 +18,6 @@ public class WorkGatewayNode {
|
||||||
/**
|
/**
|
||||||
* 分数
|
* 分数
|
||||||
*/
|
*/
|
||||||
private Double source;
|
private int weight;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -137,12 +137,12 @@ public class GatewayLoadServiceImpl implements GatewayLoadService {
|
||||||
//空余连接数
|
//空余连接数
|
||||||
Long vehicleOnlineNum = vehicleMaxOnlineNum - vehicleNowOnlineNum;
|
Long vehicleOnlineNum = vehicleMaxOnlineNum - vehicleNowOnlineNum;
|
||||||
|
|
||||||
List<String> loadNodeList =new ArrayList<>();
|
List<String> loadNodeList = new ArrayList<>();
|
||||||
List<WorkGatewayNodeSource> workGatewayNodeSources = workGatewayNodes.stream()
|
List<WorkGatewayNodeSource> workGatewayNodeSources = workGatewayNodes.stream()
|
||||||
.map(workGatewayNode ->
|
.map(workGatewayNode ->
|
||||||
WorkGatewayNodeSource.builder()
|
WorkGatewayNodeSource.builder()
|
||||||
.nodeId(workGatewayNode.getNodeId())
|
.nodeId(workGatewayNode.getNodeId())
|
||||||
.weight(Integer.parseInt(String.valueOf(vehicleOnlineNum / (nodeMaxNum - workGatewayNode.getSource()))))
|
.weight(Integer.parseInt(String.valueOf(vehicleOnlineNum / (nodeMaxNum - workGatewayNode.getWeight()))))
|
||||||
.build())
|
.build())
|
||||||
.toList();
|
.toList();
|
||||||
// 计算节点列表中所有节点的权重之和
|
// 计算节点列表中所有节点的权重之和
|
||||||
|
@ -164,7 +164,8 @@ public class GatewayLoadServiceImpl implements GatewayLoadService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 当所有节点权重为0时,跳出循环
|
// 当所有节点权重为0时,跳出循环
|
||||||
whFor:while (true) {
|
whFor:
|
||||||
|
while (true) {
|
||||||
// 遍历节点列表,将权重大于0的节点ID添加到loadNodeList中,并将节点权重减1
|
// 遍历节点列表,将权重大于0的节点ID添加到loadNodeList中,并将节点权重减1
|
||||||
for (WorkGatewayNodeSource workGatewayNodeSource : workGatewayNodeSources) {
|
for (WorkGatewayNodeSource workGatewayNodeSource : workGatewayNodeSources) {
|
||||||
int weight = workGatewayNodeSource.getWeight();
|
int weight = workGatewayNodeSource.getWeight();
|
||||||
|
@ -187,7 +188,7 @@ public class GatewayLoadServiceImpl implements GatewayLoadService {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 动态ECS
|
* 动态ECS
|
||||||
*/
|
*/
|
||||||
public void dynamicEcs() throws Exception {
|
public void dynamicEcs() throws Exception {
|
||||||
//上线最大数量
|
//上线最大数量
|
||||||
|
@ -196,15 +197,15 @@ public class GatewayLoadServiceImpl implements GatewayLoadService {
|
||||||
Long vehicleOnlineNowNum = gatewayNodeScoreCache.getNodeNowNum();
|
Long vehicleOnlineNowNum = gatewayNodeScoreCache.getNodeNowNum();
|
||||||
|
|
||||||
BigDecimal loadRate = new BigDecimal(vehicleMaxOnlineNum).divide(new BigDecimal(vehicleOnlineNowNum), 0, BigDecimal.ROUND_HALF_UP);
|
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();
|
ArrayList<WorkGatewayNode> nodeList = gatewayZSetNodeCache.get();
|
||||||
if(loadRate.longValue()>=80L){
|
if (loadRate.longValue() >= 80L) {
|
||||||
//调用扩容逻辑
|
//调用扩容逻辑
|
||||||
log.info("负载过高,开始扩容");
|
log.info("负载过高,开始扩容");
|
||||||
String instanceId = aliYunEcsService.RunInstances();
|
String instanceId = aliYunEcsService.RunInstances();
|
||||||
|
|
||||||
log.info("扩容的节点是:[{}]",instanceId);
|
log.info("扩容的节点是:[{}]", instanceId);
|
||||||
|
|
||||||
//休眠5秒确保新实例创建完成
|
//休眠5秒确保新实例创建完成
|
||||||
Thread.sleep(5000);
|
Thread.sleep(5000);
|
||||||
|
@ -219,11 +220,37 @@ public class GatewayLoadServiceImpl implements GatewayLoadService {
|
||||||
|
|
||||||
gatewayNodeCache.put(gatewayNodeInfo);
|
gatewayNodeCache.put(gatewayNodeInfo);
|
||||||
|
|
||||||
gatewayZSetNodeCache.put(instancesInformation.getPublicIpAddress(),0);
|
gatewayZSetNodeCache.put(instancesInformation.getPublicIpAddress(), 0);
|
||||||
log.info("实例id和公网ip存入redis");
|
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());
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue