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
* @param instanceName
* @param ecsSelectModel
* @return
*/
public List<InstanceInfo> selectECS(String instanceName) throws Exception {
public List<InstanceInfo> selectECS(EcsSelectModel ecsSelectModel) throws Exception {
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
.setRegionId(aliConfig.getRegionId())
.setInstanceName(instanceName)
.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<>(); // 用于存储查询到的实例信息
try {
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(describeInstancesRequest, runtime);
DescribeInstancesResponseBody body = describeInstancesResponse.getBody();
DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstances instances = body.getInstances();
@ -68,7 +79,7 @@ public class ALYunEcsService {
// 去掉方括号
publicIpAddress = publicIpAddress.substring(1, publicIpAddress.length() - 1);
instanceInfo.setPublicIpAddress(publicIpAddress);
String privateIpAddress = item.getVpcAttributes().getPrivateIpAddress().ipAddress.toString();
String privateIpAddress = item.getVpcAttributes().getPrivateIpAddress().getIpAddress().toString();
// 去掉方括号
privateIpAddress = privateIpAddress.substring(1, privateIpAddress.length() - 1);
instanceInfo.setPrivateIpAddress(privateIpAddress);
@ -76,9 +87,13 @@ public class ALYunEcsService {
}
} catch (TeaException error) {
error.printStackTrace();
// 可以打印错误信息以便排查问题
System.out.println("TeaException occurred: " + error.getMessage());
// 异常处理
} catch (Exception _error) {
_error.printStackTrace();
// 可以打印错误信息以便排查问题
System.out.println("Exception occurred: " + _error.getMessage());
// 异常处理
}
@ -87,6 +102,7 @@ public class ALYunEcsService {
/**
*
* @throws Exception

View File

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

View File

@ -1,5 +1,6 @@
package com.guo.gateway.cache;
import com.alibaba.fastjson2.JSONObject;
import com.guo.common.constant.CacheConstants;
import com.guo.gateway.cache.abs.CacheAbs;
import com.guo.gateway.model.NodeInfo;
@ -32,8 +33,18 @@ public class NodeCache extends CacheAbs<String> {
* @return
*/
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;
}
}
/**
*

View File

@ -1,15 +1,16 @@
package com.guo.gateway.cache;
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONObject;
import com.guo.common.constant.CacheConstants;
import com.guo.gateway.cache.abs.CacheAbs;
import com.guo.gateway.model.NodeJoin;
import com.guo.gateway.model.WorkGatewayNode;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
/**
@ -25,33 +26,74 @@ public class NodeScoreCache extends CacheAbs<String> {
return CacheConstants.GATEWAY_COMMON;
}
/**
*
* @return
*/
public List<WorkGatewayNode> getNodeScore() {
Set<ZSetOperations.TypedTuple<String>> range = redisService.redisTemplate.opsForZSet().rangeWithScores(encode(CacheConstants.GATEWAY_NODE_SCORE_CACHE), 0, -1);
return range.stream()
.map(ZSet -> WorkGatewayNode.builder().nodeId(ZSet.getValue()).weight(Integer.valueOf(String.valueOf(ZSet.getScore()))).build())
.toList();
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(){
List<WorkGatewayNode> workGatewayNodes = getNodeScore();
//目前连接数
Long vehicleOnlineNowNum= Long.valueOf(String.valueOf(
workGatewayNodes.stream().mapToDouble(WorkGatewayNode::getWeight).sum()
));
// 直接将 double 类型结果转换为 Long 类型
Long vehicleOnlineNowNum = (long) workGatewayNodes.stream()
.mapToDouble(WorkGatewayNode::getWeight)
.sum();
return vehicleOnlineNowNum;
}
/**
* ID
* @param 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

View File

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

View File

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