From eaeb3f4f269426d7f85a58017978f4106741824d Mon Sep 17 00:00:00 2001
From: 31353 <31353751672@qq.com>
Date: Fri, 19 Apr 2024 11:19:15 +0800
Subject: [PATCH] 4-19
---
.idea/inspectionProfiles/Project_Default.xml | 14 ++
.../java/com/guo/aly/ALYunEcsService.java | 7 +-
.../com/guo/controller/GateWayController.java | 2 +-
.../guo/gateway/cache/LoadSeriesCache.java | 2 +-
.../guo/service/impl/GateWayLoadService.java | 2 +-
.../service/impl/GateWayLoadServicelmpl.java | 127 +++++++++++++++++-
src/main/java/com/guo/task/Collection.java | 5 +-
7 files changed, 145 insertions(+), 14 deletions(-)
create mode 100644 .idea/inspectionProfiles/Project_Default.xml
diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
new file mode 100644
index 0000000..ee2c34b
--- /dev/null
+++ b/.idea/inspectionProfiles/Project_Default.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/java/com/guo/aly/ALYunEcsService.java b/src/main/java/com/guo/aly/ALYunEcsService.java
index 05aa242..cfe86da 100644
--- a/src/main/java/com/guo/aly/ALYunEcsService.java
+++ b/src/main/java/com/guo/aly/ALYunEcsService.java
@@ -44,16 +44,17 @@ public class ALYunEcsService {
public List selectEscList(EcsSelectModel ecsSelectModel){
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
.setRegionId(aliConfig.getRegionId());
- if (ecsSelectModel.getInstanceNameList() == null || ecsSelectModel.getInstanceIdList().isEmpty()){
+ if (ecsSelectModel.getInstanceNameList() == null || (ecsSelectModel.getInstanceIdList() != null && ecsSelectModel.getInstanceIdList().isEmpty())) {
describeInstancesRequest.setInstanceName("*");
- }else {
+ } else {
describeInstancesRequest.setInstanceName(Common.toJSONString(ecsSelectModel.getInstanceNameList()));
}
- if (ecsSelectModel.getInstanceIdList() != null || !ecsSelectModel.getInstanceIdList().isEmpty()){
+ if (ecsSelectModel.getInstanceIdList() != null && !ecsSelectModel.getInstanceIdList().isEmpty()) {
describeInstancesRequest.setInstanceIds(Common.toJSONString(ecsSelectModel.getInstanceIdList()).toString());
} else {
describeInstancesRequest.setInstanceName(Common.toJSONString(ecsSelectModel.getInstanceNameList()));
}
+
describeInstancesRequest.setPageSize(10);
RuntimeOptions runtime = new RuntimeOptions();
diff --git a/src/main/java/com/guo/controller/GateWayController.java b/src/main/java/com/guo/controller/GateWayController.java
index 725def9..afabfeb 100644
--- a/src/main/java/com/guo/controller/GateWayController.java
+++ b/src/main/java/com/guo/controller/GateWayController.java
@@ -21,7 +21,7 @@ public class GateWayController {
/**
* 获取负载节点
- * @return 负载节点
+ * @return 返回公网IP
*/
@GetMapping("/load/node")
public Result loadNode(){
diff --git a/src/main/java/com/guo/gateway/cache/LoadSeriesCache.java b/src/main/java/com/guo/gateway/cache/LoadSeriesCache.java
index 55d58e6..2d05335 100644
--- a/src/main/java/com/guo/gateway/cache/LoadSeriesCache.java
+++ b/src/main/java/com/guo/gateway/cache/LoadSeriesCache.java
@@ -25,7 +25,7 @@ public class LoadSeriesCache extends CacheAbs {
*/
@PostConstruct
public void init(){
- redisService.setCacheObject(encode(gatewayLoadSeriesKey),0L);
+ redisService.setCacheObject(encode(gatewayLoadSeriesKey),0);
}
/**
diff --git a/src/main/java/com/guo/service/impl/GateWayLoadService.java b/src/main/java/com/guo/service/impl/GateWayLoadService.java
index ba47c8e..76ae778 100644
--- a/src/main/java/com/guo/service/impl/GateWayLoadService.java
+++ b/src/main/java/com/guo/service/impl/GateWayLoadService.java
@@ -9,7 +9,7 @@ public interface GateWayLoadService {
/**
* 获取负载节点
- * @return 返回负载节点
+ * @return 返回公网IP
*/
String loadNode();
diff --git a/src/main/java/com/guo/service/impl/GateWayLoadServicelmpl.java b/src/main/java/com/guo/service/impl/GateWayLoadServicelmpl.java
index 3c786cc..194bd50 100644
--- a/src/main/java/com/guo/service/impl/GateWayLoadServicelmpl.java
+++ b/src/main/java/com/guo/service/impl/GateWayLoadServicelmpl.java
@@ -1,10 +1,23 @@
package com.guo.service.impl;
+import com.alibaba.fastjson2.JSONObject;
import com.guo.gateway.cache.*;
import com.guo.gateway.model.NodeInfo;
+import com.guo.gateway.model.NodeJoin;
import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import lombok.ToString;
+import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+
/**
* @author gxb
* @description 负载实现层
@@ -12,7 +25,8 @@ import org.springframework.stereotype.Service;
*/
@Service
@AllArgsConstructor
-public class GateWayLoadServicelmpl implements GateWayLoadService{
+@Log4j2
+public class GateWayLoadServicelmpl implements GateWayLoadService {
/**
* 负载的长度
@@ -50,17 +64,116 @@ public class GateWayLoadServicelmpl implements GateWayLoadService{
private final VehicleLineNodeCache vehicleLineNodeCache;
-
/**
* 获取负载节点
- * @return 返回负载节点
+ *
+ * @return 返回公网IP
*/
@Override
public String loadNode() {
+ //初始化序列
+ loadSeriesCache.reset();
+
+ //new一个WorkGatewayNode类的集合
+ List nodeIdList = new ArrayList<>();
+
+ //获取缓存内节点信息及连接数
+ List LinkingValue = nodeScoreCache.get();
+
+ //遍历
+ if (!LinkingValue.isEmpty()) {
+ for (String nodejoin : LinkingValue) {
+ //转型
+ NodeJoin nodeJoin = JSONObject.parseObject(nodejoin, NodeJoin.class);
+
+ nodeIdList.add(new WorkGatewayNode(nodeJoin.getNodeId(), nodeJoin.getLinkingNumber().intValue()));
+ }
+ }
+
+ List loadNodeList = new ArrayList<>();
+
+ int count = nodeIdList.stream().mapToInt(WorkGatewayNode::getWeight).sum();
+
+ if (count < 100) {
+ List list = nodeIdList.stream().sorted((o1, o2) -> o2.getWeight() - o1.getWeight()).toList();
+
+ int countWeight = 0;
+ for (long i = count; i < 100; i++) {
+ WorkGatewayNode workGatewayNode = list.get(countWeight++ % list.size());
+ workGatewayNode.setWeight(workGatewayNode.getWeight() + 1);
+ }
+ }
+
+ whFor:
+ while (true) {
+ for (WorkGatewayNode workGatewayNode : nodeIdList) {
+ int weight = workGatewayNode.getWeight();
+ if (weight > 0) {
+ loadNodeList.add(
+ workGatewayNode.getNodeId()
+ );
+ workGatewayNode.setWeight(weight - 1);
+
+ }
+
+ }
+ int sum = nodeIdList.stream().
+ mapToInt(WorkGatewayNode::getWeight).sum();
+ if (sum <= 0) {
+ break whFor;
+ }
+ }
+ //打印负载节点信息
+ log.info(loadNodeList);
+ //存负载集合
+ loadNodeCache.put(loadNodeList);
+
+ //获取自增序列值
Long seriesLoad = loadSeriesCache.incrementAndGet(); //获取自增序列值
- Long seriesLoadIndex = seriesLoad % nodeLength; //获取负载下标
- String loadNodeId = loadNodeCache.getFindByIndex(seriesLoadIndex); //通过获取节点ID
- NodeInfo nodeInfo = nodeCache.get(loadNodeId); //获取缓存内节点的公网/内网信息
- return nodeInfo.getPublicIdAddress(); //返回公网IP
+ Long seriesLoadIndex = seriesLoad % nodeLength;
+ //获取负载下标
+ String loadNodeId = loadNodeCache.getFindByIndex(seriesLoadIndex);
+ //通过获取节点ID
+ NodeInfo nodeInfo = nodeCache.get(loadNodeId);
+ //获取缓存内节点的公网/内网信息
+ return nodeInfo.getPublicIdAddress();
+ //返回公网IP
}
}
+
+/**
+ * 统计
+ */
+class StiNode {
+ // 使用 ConcurrentHashMap 保证线程安全
+ private static final Map stiNodeMap = new ConcurrentHashMap<>();
+
+ public static void sti(String nodeId) {
+ // 使用 computeIfAbsent 方法确保原子性操作
+ stiNodeMap.computeIfAbsent(nodeId, key -> new AtomicInteger()).incrementAndGet();
+ }
+
+ public static Map show() {
+ Map resultMap = new HashMap<>();
+ stiNodeMap.forEach((key, val) -> {
+ resultMap.put(key, (long) val.get());
+ System.out.println(key + "▷◁▷◁▷◁▷◁▷◁▷◁▷▷◁▷◁▷◁☛☛" + val.get());
+ });
+ return resultMap;
+ }
+}
+
+
+/**
+ * 节点ID 、 权重
+ */
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+@ToString
+class WorkGatewayNode {
+
+ private String nodeId;
+ private int weight;
+
+}
diff --git a/src/main/java/com/guo/task/Collection.java b/src/main/java/com/guo/task/Collection.java
index 9592373..e82e328 100644
--- a/src/main/java/com/guo/task/Collection.java
+++ b/src/main/java/com/guo/task/Collection.java
@@ -20,6 +20,7 @@ import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -87,7 +88,9 @@ public class Collection {
//查询阿里云是否存在实例
EcsSelectModel ecsSelectModel = new EcsSelectModel();
- ecsSelectModel.setInstanceNameList(Arrays.asList("Myname"));
+ List addArryList = new ArrayList<>();
+ addArryList.add("Myname");
+ ecsSelectModel.setInstanceNameList(addArryList);
//实例集合
List instanceLists = alYunEcsService.selectEscList(ecsSelectModel);