4-19
parent
30f4424ac1
commit
eaeb3f4f26
|
@ -0,0 +1,14 @@
|
|||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="AliAccessStaticViaInstance" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliArrayNamingShouldHaveBracket" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliControlFlowStatementWithoutBraces" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliDeprecation" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliEqualsAvoidNull" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliLongLiteralsEndingWithLowercaseL" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliMissingOverrideAnnotation" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="AliWrapperTypeEquality" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="MapOrSetKeyShouldOverrideHashCodeEquals" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
</profile>
|
||||
</component>
|
|
@ -44,16 +44,17 @@ public class ALYunEcsService {
|
|||
public List<InstanceInfo> 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 {
|
||||
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();
|
||||
|
|
|
@ -21,7 +21,7 @@ public class GateWayController {
|
|||
|
||||
/**
|
||||
* 获取负载节点
|
||||
* @return 负载节点
|
||||
* @return 返回公网IP
|
||||
*/
|
||||
@GetMapping("/load/node")
|
||||
public Result<String> loadNode(){
|
||||
|
|
|
@ -25,7 +25,7 @@ public class LoadSeriesCache extends CacheAbs<String> {
|
|||
*/
|
||||
@PostConstruct
|
||||
public void init(){
|
||||
redisService.setCacheObject(encode(gatewayLoadSeriesKey),0L);
|
||||
redisService.setCacheObject(encode(gatewayLoadSeriesKey),0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -9,7 +9,7 @@ public interface GateWayLoadService {
|
|||
|
||||
/**
|
||||
* 获取负载节点
|
||||
* @return 返回负载节点
|
||||
* @return 返回公网IP
|
||||
*/
|
||||
String loadNode();
|
||||
|
||||
|
|
|
@ -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,6 +25,7 @@ import org.springframework.stereotype.Service;
|
|||
*/
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
@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<WorkGatewayNode> nodeIdList = new ArrayList<>();
|
||||
|
||||
//获取缓存内节点信息及连接数
|
||||
List<String> 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<String> loadNodeList = new ArrayList<>();
|
||||
|
||||
int count = nodeIdList.stream().mapToInt(WorkGatewayNode::getWeight).sum();
|
||||
|
||||
if (count < 100) {
|
||||
List<WorkGatewayNode> 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<String, AtomicInteger> stiNodeMap = new ConcurrentHashMap<>();
|
||||
|
||||
public static void sti(String nodeId) {
|
||||
// 使用 computeIfAbsent 方法确保原子性操作
|
||||
stiNodeMap.computeIfAbsent(nodeId, key -> new AtomicInteger()).incrementAndGet();
|
||||
}
|
||||
|
||||
public static Map<String, Long> show() {
|
||||
Map<String, Long> 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;
|
||||
|
||||
}
|
||||
|
|
|
@ -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<String> addArryList = new ArrayList<>();
|
||||
addArryList.add("Myname");
|
||||
ecsSelectModel.setInstanceNameList(addArryList);
|
||||
//实例集合
|
||||
List<InstanceInfo> instanceLists = alYunEcsService.selectEscList(ecsSelectModel);
|
||||
|
||||
|
|
Loading…
Reference in New Issue