master
31353 2024-04-20 11:29:08 +08:00
parent 47840b2c5a
commit 27c65908d1
7 changed files with 105 additions and 32 deletions

View File

@ -39,21 +39,32 @@ public class ALYunEcsService {
/** /**
* ID * ID
* @param instanceName * @param ecsSelectModel
* @return * @return
*/ */
public List<InstanceInfo> selectECS(String instanceName) throws Exception { public List<InstanceInfo> selectECS(EcsSelectModel ecsSelectModel) throws Exception {
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest() DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
.setRegionId(aliConfig.getRegionId()) .setRegionId(aliConfig.getRegionId())
.setInstanceName(instanceName)
.setPageSize(10); .setPageSize(10);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); // 判断是否指定了实例ID或实例名称
if (ecsSelectModel.getInstanceIdList() != null && !ecsSelectModel.getInstanceIdList().isEmpty()) {
// 如果指定了实例ID则设置实例ID查询条件
describeInstancesRequest.setInstanceIds(String.join(",", ecsSelectModel.getInstanceIdList()));
} else if (ecsSelectModel.getInstanceNameList() != null && !ecsSelectModel.getInstanceNameList().isEmpty()) {
// 如果指定了实例名称,则设置实例名称查询条件
describeInstancesRequest.setInstanceName(String.join(",", ecsSelectModel.getInstanceNameList()));
} else {
// 如果既没有指定实例ID也没有指定实例名称则抛出异常或者返回空列表视情况而定
throw new IllegalArgumentException("Please provide at least one instance ID or instance name.");
// 或者直接返回空列表
// return new ArrayList<>();
}
List<InstanceInfo> instanceInfos = new ArrayList<>(); // 用于存储查询到的实例信息 List<InstanceInfo> instanceInfos = new ArrayList<>(); // 用于存储查询到的实例信息
try { try {
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
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();
@ -68,7 +79,7 @@ public class ALYunEcsService {
// 去掉方括号 // 去掉方括号
publicIpAddress = publicIpAddress.substring(1, publicIpAddress.length() - 1); publicIpAddress = publicIpAddress.substring(1, publicIpAddress.length() - 1);
instanceInfo.setPublicIpAddress(publicIpAddress); instanceInfo.setPublicIpAddress(publicIpAddress);
String privateIpAddress = item.getVpcAttributes().getPrivateIpAddress().ipAddress.toString(); String privateIpAddress = item.getVpcAttributes().getPrivateIpAddress().getIpAddress().toString();
// 去掉方括号 // 去掉方括号
privateIpAddress = privateIpAddress.substring(1, privateIpAddress.length() - 1); privateIpAddress = privateIpAddress.substring(1, privateIpAddress.length() - 1);
instanceInfo.setPrivateIpAddress(privateIpAddress); instanceInfo.setPrivateIpAddress(privateIpAddress);
@ -76,9 +87,13 @@ public class ALYunEcsService {
} }
} catch (TeaException error) { } catch (TeaException error) {
error.printStackTrace(); error.printStackTrace();
// 可以打印错误信息以便排查问题
System.out.println("TeaException occurred: " + error.getMessage());
// 异常处理 // 异常处理
} catch (Exception _error) { } catch (Exception _error) {
_error.printStackTrace(); _error.printStackTrace();
// 可以打印错误信息以便排查问题
System.out.println("Exception occurred: " + _error.getMessage());
// 异常处理 // 异常处理
} }
@ -87,6 +102,7 @@ public class ALYunEcsService {
/** /**
* *
* @throws Exception * @throws Exception

View File

@ -4,6 +4,7 @@ import com.guo.common.domain.Result;
import com.guo.service.impl.GateWayLoadService; import com.guo.service.impl.GateWayLoadService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -23,7 +24,7 @@ public class GateWayController {
* *
* @return IP * @return IP
*/ */
@GetMapping("/load/node") @PostMapping("/load/node")
public Result<String> loadNode(){ public Result<String> loadNode(){
return Result.success(gateWayLoadService.loadNode()); return Result.success(gateWayLoadService.loadNode());
} }

View File

@ -1,5 +1,6 @@
package com.guo.gateway.cache; package com.guo.gateway.cache;
import com.alibaba.fastjson2.JSONObject;
import com.guo.common.constant.CacheConstants; import com.guo.common.constant.CacheConstants;
import com.guo.gateway.cache.abs.CacheAbs; import com.guo.gateway.cache.abs.CacheAbs;
import com.guo.gateway.model.NodeInfo; import com.guo.gateway.model.NodeInfo;
@ -32,9 +33,19 @@ public class NodeCache extends CacheAbs<String> {
* @return * @return
*/ */
public NodeInfo get(String nodeId){ public NodeInfo get(String nodeId){
return redisService.getCacheObject(encode(nodeId)); JSONObject jsonObject = redisService.getCacheObject(encode(nodeId));
if (jsonObject != null) {
NodeInfo nodeInfo = new NodeInfo();
nodeInfo.setNodeId(jsonObject.getString("nodeId"));
nodeInfo.setPublicIdAddress(jsonObject.getString("publicIdAddress"));
nodeInfo.setPrivateIdAddress(jsonObject.getString("privateIdAddress"));
return nodeInfo;
} else {
return null;
}
} }
/** /**
* *
* @param nodeId * @param nodeId

View File

@ -1,15 +1,16 @@
package com.guo.gateway.cache; package com.guo.gateway.cache;
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONObject;
import com.guo.common.constant.CacheConstants; import com.guo.common.constant.CacheConstants;
import com.guo.gateway.cache.abs.CacheAbs; import com.guo.gateway.cache.abs.CacheAbs;
import com.guo.gateway.model.NodeJoin; import com.guo.gateway.model.NodeJoin;
import com.guo.gateway.model.WorkGatewayNode; import com.guo.gateway.model.WorkGatewayNode;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.ZSetOperations; import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.List; import java.util.*;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -25,33 +26,74 @@ public class NodeScoreCache extends CacheAbs<String> {
return CacheConstants.GATEWAY_COMMON; return CacheConstants.GATEWAY_COMMON;
} }
public List<WorkGatewayNode> getNodeScore(){ /**
Set<ZSetOperations.TypedTuple<String>> range = redisService.redisTemplate.opsForZSet().rangeWithScores(encode(CacheConstants.GATEWAY_NODE_SCORE_CACHE), 0, -1); *
return range.stream() * @return
.map(ZSet -> WorkGatewayNode.builder().nodeId(ZSet.getValue()).weight(Integer.valueOf(String.valueOf(ZSet.getScore()))).build()) */
.toList(); public List<WorkGatewayNode> getNodeScore() {
Set<ZSetOperations.TypedTuple<Object>> range = redisService
.redisTemplate.opsForZSet()
.rangeWithScores(encode(CacheConstants.GATEWAY_NODE_SCORE_CACHE), 0, -1);
if (range == null || range.isEmpty()) {
return Collections.emptyList();
}
return range.stream()
.map(tuple -> {
try {
JSONObject jsonObject = (JSONObject) tuple.getValue();
// 从 JSONObject 中获取字段值
String nodeId = jsonObject.getString("nodeId");
long linkingNumber = jsonObject.getLongValue("linkingNumber");
NodeJoin nodeJoin = new NodeJoin();
nodeJoin.setNodeId(nodeId);
nodeJoin.setLinkingNumber(linkingNumber);
// double score = tuple.getScore(); // 获取分数
return WorkGatewayNode.builder()
.nodeId(nodeId)
.weight(Integer.valueOf(String.valueOf(nodeJoin.getLinkingNumber()))) // 假设分数是double类型这里将其转换为int类型
.build();
} catch (JSONException e) {
// JSON 解析异常处理
e.printStackTrace();
return null;
}
})
.filter(Objects::nonNull) // 过滤掉解析失败的节点
.toList();
} }
public Long getNodeNowNum(){ public Long getNodeNowNum(){
List<WorkGatewayNode> workGatewayNodes = getNodeScore(); List<WorkGatewayNode> workGatewayNodes = getNodeScore();
//目前连接数 // 直接将 double 类型结果转换为 Long 类型
Long vehicleOnlineNowNum= Long.valueOf(String.valueOf( Long vehicleOnlineNowNum = (long) workGatewayNodes.stream()
workGatewayNodes.stream().mapToDouble(WorkGatewayNode::getWeight).sum() .mapToDouble(WorkGatewayNode::getWeight)
)); .sum();
return vehicleOnlineNowNum; return vehicleOnlineNowNum;
} }
/** /**
* ID * ID
* @param nodeJoin * @param nodeJoin
*/ */
public void save(NodeJoin nodeJoin){ public void save(NodeJoin nodeJoin) {
redisService.setCacheSets(encode(CacheConstants.GATEWAY_NODE_SCORE_CACHE),nodeJoin); ZSetOperations.TypedTuple<Object> tuple = new DefaultTypedTuple<>(nodeJoin, 0.0); // 使用默认的分数为0.0
redisService.redisTemplate.opsForZSet().add(encode(CacheConstants.GATEWAY_NODE_SCORE_CACHE), Collections.singleton(tuple));
} }
/** /**
* *
* @return * @return

View File

@ -103,13 +103,16 @@ public class GateWayLoadServicelmpl implements GateWayLoadService {
// 尝试获取锁最多等待10秒持有锁60秒后自动释放 // 尝试获取锁最多等待10秒持有锁60秒后自动释放
// if (refreshLoadLock.tryLock(10, 60, TimeUnit.SECONDS)) { // if (refreshLoadLock.tryLock(10, 60, TimeUnit.SECONDS)) {
// 在锁内执行刷新负载的逻辑 // 在锁内执行刷新负载的逻辑
List<WorkGatewayNode> workGatewayNodes = nodeScoreCache.getNodeScore(); List<WorkGatewayNode> workGatewayNodes = nodeScoreCache.getNodeScore();
log.info("实例数量 " + workGatewayNodes.size());
//车辆上线总数量 //车辆上线总数量
long vehicleMaxOnlineNUm = getNodeMaxOnlineNum(); long vehicleMaxOnlineNUm = getNodeMaxOnlineNum();
//目前连接数 //目前连接数
Long veicleOnlineNowNum = nodeScoreCache.getNodeNowNum(); Long veicleOnlineNowNum = nodeScoreCache.getNodeNowNum();
//空余连接数 //空余连接数
long vehicleOnlineNum = vehicleMaxOnlineNUm - veicleOnlineNowNum; long vehicleOnlineNum = vehicleMaxOnlineNUm - veicleOnlineNowNum;

View File

@ -85,12 +85,12 @@ public class Collection {
public void scheduledEcsCompanding() throws Exception { 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.selectECS("Myname"); List<InstanceInfo> instanceLists = alYunEcsService.selectECS(ecsSelectModel);
//节点计数 //节点计数
Long nodeNumber = 0L; Long nodeNumber = 0L;
@ -168,14 +168,14 @@ public class Collection {
} }
} }
if (connectionTotal > 0){
//封装节点数量和节点连接总数 //封装节点数量和节点连接总数
TotalNumber totalNumber = new TotalNumber(); TotalNumber totalNumber = new TotalNumber();
totalNumber.setConnectionTotal(connectionTotal); totalNumber.setConnectionTotal(connectionTotal);
totalNumber.setNodeNumber(nodeNumber); totalNumber.setNodeNumber(nodeNumber);
//调用扩容方法去判断是否需要扩容 //调用扩容方法去判断是否需要扩容
contractionVolume.contractionVolume(totalNumber); contractionVolume.contractionVolume(totalNumber);
}
} }
} }

View File

@ -68,7 +68,7 @@ public class ContractionVolume {
log.error("当前未存在节点信息"); log.error("当前未存在节点信息");
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();