diff --git a/src/main/java/com/loadCenter/aliyun/controller/GatewayController.java b/src/main/java/com/loadCenter/aliyun/controller/GatewayController.java index 729e7b8..b5822e0 100644 --- a/src/main/java/com/loadCenter/aliyun/controller/GatewayController.java +++ b/src/main/java/com/loadCenter/aliyun/controller/GatewayController.java @@ -21,11 +21,6 @@ public class GatewayController { @Autowired private GatewayLoadService service; - @GetMapping("/locd/node") - public Result loadNode(){ - return Result.success(service.loadNode()); - } - @PostMapping("/getAssignedServer") public Result getAssignedServer(){ return service.getAssignedServer(); diff --git a/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayIpLoadCountKey.java b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayIpLoadCountKey.java new file mode 100644 index 0000000..8aeed8b --- /dev/null +++ b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayIpLoadCountKey.java @@ -0,0 +1,51 @@ +package com.loadCenter.aliyun.gateway.cache; + +import com.alibaba.fastjson2.JSON; + +import com.loadCenter.aliyun.gateway.cache.abs.GatewayNodeCacheAbs; +import com.loadCenter.aliyun.gateway.model.NodeLoadNum; +import org.springframework.stereotype.Component; + +import java.util.List; + + +/** + * @ClassName GatewayIpLoadCountKey + * @Description 缓存实例IP和负载量 + * @Author ZeJinG.Su + * @Date 22:29 2024/4/19 + */ +@Component +public class GatewayIpLoadCountKey extends GatewayNodeCacheAbs { + private final static String gatewayIpLoadCountKey = "ipAndCount"; + + @Override + public String getPre() { + return "gateway:load:"; + } + + /** + * 增加缓存数据 + * @param ipAndLoadCounts + */ + public void put(List ipAndLoadCounts) { + this.remove(); + redisService.setCacheList(encode(gatewayIpLoadCountKey), ipAndLoadCounts); + } + + /** + * 获取缓存数据 + * @return + */ + public List get() { + List cacheList = redisService.getCacheList(encode(gatewayIpLoadCountKey)); + return JSON.parseArray(JSON.toJSONString(cacheList), NodeLoadNum.class); + } + + /** + * 获取缓存数据 + */ + public void remove() { + redisService.deleteObject(encode(gatewayIpLoadCountKey)); + } +} diff --git a/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayLoadNodeCache.java b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayLoadNodeCache.java deleted file mode 100644 index f40fdae..0000000 --- a/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayLoadNodeCache.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.loadCenter.aliyun.gateway.cache; - - -import com.loadCenter.aliyun.gateway.cache.abs.GatewayNodeCacheAbs; -import org.springframework.stereotype.Component; - -import java.util.List; - -/** - * @ClassName GatewayNodeCacheAbs - * @Description 网关节点缓存 - * @Author ZeJinG.Su - * @Date 16:16 2024/4/18 - */ -@Component -public class GatewayLoadNodeCache extends GatewayNodeCacheAbs { - - private final static String GATEWAY_LOAD_NODE_KEY="node"; - - - @Override - public String getPre() { - return "gateway:load:"; - } - - /** - * 存放负载集合 - * @param nodeList 节点权重集合 - */ - public void put(List nodeList){ - redisService.deleteObject(encode(GATEWAY_LOAD_NODE_KEY)); - redisService.setCacheList(encode(GATEWAY_LOAD_NODE_KEY),nodeList); - } - - /** - * 获取所有负载节点 - * @return 负载节点集合 - */ - public List get() { - return redisService.getCacheList(encode(GATEWAY_LOAD_NODE_KEY)); - } - - - public String getByIndex(Long index){ - if(index == null || index >100){ - throw new RuntimeException("下标违法,0-100"); - } - return redisService.getCacheListValue(encode(GATEWAY_LOAD_NODE_KEY),index); - } -} diff --git a/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayLoadSeriesCache.java b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayLoadSeriesCache.java deleted file mode 100644 index aeb4dcc..0000000 --- a/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayLoadSeriesCache.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.loadCenter.aliyun.gateway.cache; - -import com.loadCenter.aliyun.gateway.cache.abs.GatewayNodeCacheAbs; -import org.springframework.stereotype.Component; - -import javax.annotation.PostConstruct; -/** - * @ClassName GatewayLoadSeriesCache - * @Description 网关负载序列 - * @Author ZeJinG.Su - * @Date 19:47 2024/4/18 - */ -@Component -public class GatewayLoadSeriesCache extends GatewayNodeCacheAbs { - private final static String GATEWAY_LOAD_SERIES_KEY = "series"; - - @Override - public String getPre() { - return "gateway:load:"; - } - - - /** - * bean创建完成之后执行方法 - */ - @PostConstruct - public void init(){ - redisService.setCacheObject(encode(GATEWAY_LOAD_SERIES_KEY),0); - } - - /** - * 获取当前序列值 - * @return 序列值 - */ - public Long get(){ - return redisService.getCacheObject(encode(GATEWAY_LOAD_SERIES_KEY)); - } - - /** - * 获取自增序列值 - * @return 自增序列值 - */ - public Long incrementAndGet() { - return redisService.increment(encode(GATEWAY_LOAD_SERIES_KEY),1L); - } - - /** - * 重置序列值 - */ - public void reset() { - this.init(); - } -} diff --git a/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeCache.java b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeCache.java deleted file mode 100644 index d5db210..0000000 --- a/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeCache.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.loadCenter.aliyun.gateway.cache; - - -import com.loadCenter.aliyun.gateway.cache.abs.GatewayNodeCacheAbs; -import com.loadCenter.aliyun.gateway.model.GatewayNodeInfo; -import org.springframework.stereotype.Component; -/** - * @ClassName GatewayNodeCache - * @Description 网关节点缓存 - * @Author ZeJinG.Su - * @Date 19:50 2024/4/18 - */ -@Component -public class GatewayNodeCache extends GatewayNodeCacheAbs { - - @Override - public String getPre() { - return "gateway:node:info:"; - } - - /** - * 增加缓存数据 - * @param gatewayNodeInfo 节点信息 - */ - public void put(GatewayNodeInfo gatewayNodeInfo){ - redisService.setCacheObject(encode(gatewayNodeInfo.getNodeId()),gatewayNodeInfo); - } - - /** - * 获取缓存数据 - * @param nodeId 节点ID - * @return 节点信息 - */ - public GatewayNodeInfo get(String nodeId){ - return redisService.getCacheObject(encode(nodeId)); - } - - /** - * 删除网关节点 - * @param nodeId 节点ID - */ - public void remove(String nodeId) { - redisService.deleteObject(encode(nodeId)); - } - -} diff --git a/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeIDCache.java b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeIDCache.java new file mode 100644 index 0000000..eadd69d --- /dev/null +++ b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeIDCache.java @@ -0,0 +1,46 @@ +package com.loadCenter.aliyun.gateway.cache; + +import com.loadCenter.aliyun.gateway.cache.abs.GatewayNodeCacheAbs; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @ClassName GatewayNodeIDCache + * @Description 节点id缓存 + * @Author ZeJinG.Su + * @Date 22:31 2024/4/19 + */ +@Component +public class GatewayNodeIDCache extends GatewayNodeCacheAbs { + private final static String gatewayLoadNodeIDKey = "NodeId"; + + @Override + public String getPre() { + return "gateway:load:"; + } + + /** + * 增加 + * @param gatewayNodeIds + */ + public void put(List gatewayNodeIds) { + this.remove(); + redisService.setCacheList(encode(gatewayLoadNodeIDKey), gatewayNodeIds); + } + + /** + * 获取 + * @return + */ + public List get() { + return redisService.getCacheList(encode(gatewayLoadNodeIDKey)); + } + + /** + * 删除 + */ + public void remove() { + redisService.deleteObject(encode(gatewayLoadNodeIDKey)); + } +} diff --git a/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeIPCache.java b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeIPCache.java new file mode 100644 index 0000000..00139fc --- /dev/null +++ b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeIPCache.java @@ -0,0 +1,46 @@ +package com.loadCenter.aliyun.gateway.cache; + +import com.loadCenter.aliyun.gateway.cache.abs.GatewayNodeCacheAbs; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @ClassName GatewayNodeIPCache + * @Description 负载节点IP缓存 + * @Author ZeJinG.Su + * @Date 22:35 2024/4/19 + */ +@Component +public class GatewayNodeIPCache extends GatewayNodeCacheAbs { + private final static String gatewayLoadNodeIPKey = "NodeIp"; + + @Override + public String getPre() { + return "gateway:load:"; + } + + /** + * 增加缓存数据 + * @param gatewayNodeIps + */ + public void put(List gatewayNodeIps) { + this.remove(); + redisService.setCacheList(encode(gatewayLoadNodeIPKey), gatewayNodeIps); + } + + /** + * 获取缓存数据 + * @return + */ + public List get() { + return redisService.getCacheList(encode(gatewayLoadNodeIPKey)); + } + + /** + * 删除缓存数据 + */ + public void remove() { + redisService.deleteObject(encode(gatewayLoadNodeIPKey)); + } +} diff --git a/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeInfoCache.java b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeInfoCache.java new file mode 100644 index 0000000..fcb978d --- /dev/null +++ b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeInfoCache.java @@ -0,0 +1,52 @@ +package com.loadCenter.aliyun.gateway.cache; + +import com.alibaba.fastjson2.JSON; +import com.loadCenter.aliyun.gateway.cache.abs.GatewayNodeCacheAbs; +import com.loadCenter.aliyun.gateway.model.GatewayNodeInfo; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @ClassName GatewayNodeInfoCache + * @Description 网关节点缓存 + * @Author ZeJinG.Su + * @Date 22:34 2024/4/19 + */ + +@Component +public class GatewayNodeInfoCache extends GatewayNodeCacheAbs { + //redis Key + private final static String gatewayLoadInfoKey = "node:"; + + @Override + public String getPre() { + return "gateway:load:"; + } + + /** + * 增加缓存数据 + * @param gatewayNodeInfos + */ + public void put(List gatewayNodeInfos) { + this.remove(); + redisService.setCacheList(encode(gatewayLoadInfoKey), gatewayNodeInfos); + } + + /** + * 获取缓存数据 + * @return + */ + public List get() { + List cacheList = redisService.getCacheList(encode(gatewayLoadInfoKey)); + + return JSON.parseArray(JSON.toJSONString(cacheList), GatewayNodeInfo.class); + } + + /** + * 删除缓存数据 + */ + public void remove() { + redisService.deleteObject(encode(gatewayLoadInfoKey)); + } +} diff --git a/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeScoreCache.java b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeScoreCache.java deleted file mode 100644 index 92bad04..0000000 --- a/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeScoreCache.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.loadCenter.aliyun.gateway.cache; - -import com.loadCenter.aliyun.gateway.cache.abs.GatewayNodeCacheAbs; - -/** - * @ClassName GatewayNodeScoreCache - * @Description 网关节点分数 - * @Author ZeJinG.Su - * @Date 19:57 2024/4/18 - */ -public class GatewayNodeScoreCache extends GatewayNodeCacheAbs { - @Override - public String getPre() { - return null; - } -} diff --git a/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeSetVinCache.java b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeSetVinCache.java deleted file mode 100644 index c7f446a..0000000 --- a/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeSetVinCache.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.loadCenter.aliyun.gateway.cache; - - -import com.loadCenter.aliyun.gateway.cache.abs.GatewayNodeCacheAbs; - -/** - * @ClassName GatewayNodeSetVinCache - * @Description 网关而几点存储VIN详情 - * @Author ZeJinG.Su - * @Date 19:58 2024/4/18 - */ -public class GatewayNodeSetVinCache extends GatewayNodeCacheAbs { - @Override - public String getPre() { - return null; - } -} diff --git a/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayOrderCache.java b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayOrderCache.java new file mode 100644 index 0000000..d0c1fb8 --- /dev/null +++ b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayOrderCache.java @@ -0,0 +1,47 @@ +package com.loadCenter.aliyun.gateway.cache; + +import com.loadCenter.aliyun.gateway.cache.abs.GatewayNodeCacheAbs; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @ClassName GatewayOrderCache + * @Description 节点IP序列缓存 + * @Author ZeJinG.Su + * @Date 22:36 2024/4/19 + */ +@Component +public class GatewayOrderCache extends GatewayNodeCacheAbs { + private final static String gatewayOrderCache = "order"; + + @Override + public String getPre() { + return "gateway:load:"; + } + + /** + * 增加缓存数据 + * @param gatewayNodeOrders + */ + public void put(List gatewayNodeOrders) { + this.remove(); + redisService.setCacheList(encode(gatewayOrderCache), gatewayNodeOrders); + } + + + /** + * 获取全部缓存数据 + * @return + */ + public List get() { + return redisService.getCacheList(encode(gatewayOrderCache)); + } + + /** + * 删除缓存数据 + */ + public void remove() { + redisService.deleteObject(encode(gatewayOrderCache)); + } +} diff --git a/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayVehicleLineNodeCache.java b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayVehicleLineNodeCache.java deleted file mode 100644 index a49ce09..0000000 --- a/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayVehicleLineNodeCache.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.loadCenter.aliyun.gateway.cache; - -import com.loadCenter.aliyun.gateway.cache.abs.GatewayNodeCacheAbs; - -/** - * @ClassName GatewayVehicleLineNodeCache - * @Description 网关车辆对应网关节点 - * @Author ZeJinG.Su - * @Date 19:58 2024/4/18 - */ -public class GatewayVehicleLineNodeCache extends GatewayNodeCacheAbs { - @Override - public String getPre() { - return null; - } -} diff --git a/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayWeightCache.java b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayWeightCache.java new file mode 100644 index 0000000..b6f1266 --- /dev/null +++ b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayWeightCache.java @@ -0,0 +1,49 @@ +package com.loadCenter.aliyun.gateway.cache; + +import com.alibaba.fastjson2.JSON; +import com.loadCenter.aliyun.gateway.cache.abs.GatewayNodeCacheAbs; +import com.loadCenter.aliyun.gateway.model.NodeLoadWeight; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @ClassName GatewayWeightCache + * @Description 缓存IP和权重 + * @Author ZeJinG.Su + * @Date 22:37 2024/4/19 + */ +@Component +public class GatewayWeightCache extends GatewayNodeCacheAbs { + private final static String gatewayWeightKey = "ipAndWeight"; + + @Override + public String getPre() { + return "gateway:load:"; + } + + /** + * 增加缓存数据 + * @param ipAndWeights + */ + public void put(List ipAndWeights) { + this.remove(); + redisService.setCacheList(encode(gatewayWeightKey), ipAndWeights); + } + + /** + * 获取缓存数据 + * @return + */ + public List get() { + List cacheList = redisService.getCacheList(encode(gatewayWeightKey)); + return JSON.parseArray(JSON.toJSONString(cacheList), NodeLoadWeight.class); + } + + /** + * 删除缓存数据 + */ + public void remove() { + redisService.deleteObject(encode(gatewayWeightKey)); + } +} diff --git a/src/main/java/com/loadCenter/aliyun/handle/HandleCache.java b/src/main/java/com/loadCenter/aliyun/handle/HandleCache.java new file mode 100644 index 0000000..db5705f --- /dev/null +++ b/src/main/java/com/loadCenter/aliyun/handle/HandleCache.java @@ -0,0 +1,88 @@ +package com.loadCenter.aliyun.handle; + +import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody; +import com.loadCenter.aliyun.common.aliyun.service.AliYunEcsService; +import com.loadCenter.aliyun.common.utils.MqttUtil; +import com.loadCenter.aliyun.common.utils.UserUtil; +import com.loadCenter.aliyun.gateway.cache.*; +import com.loadCenter.aliyun.gateway.model.GatewayNodeInfo; +import com.loadCenter.aliyun.gateway.model.NodeLoadNum; +import com.loadCenter.aliyun.gateway.model.NodeLoadWeight; +import lombok.AllArgsConstructor; +import lombok.extern.log4j.Log4j2; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.util.ArrayList; +import java.util.List; + +/** + * @ClassName HandleCache + * @Description 操作缓存 + * @Author ZeJinG.Su + * @Date 20:03 2024/4/19 + */ + +@Log4j2 +@Component +@AllArgsConstructor +public class HandleCache { + @Autowired + private MqttUtil mqttUtil; + + @Autowired + private AliYunEcsService aliYunEcsService; + + /* + * 操作网关节点缓存 + * */ + @Autowired + private GatewayNodeInfoCache gatewayNodeInfoCache; + + /* + * 操作负载节点IP缓存 + * */ + @Autowired + private GatewayNodeIPCache gatewayNodeIpCache; + + /* + * 操作负载节点ID缓存 + * */ + @Autowired + private GatewayNodeIDCache gatewayNodeIdCache; + + /* + * 操作实例IP和负载量缓存 + * */ + @Autowired + private GatewayIpLoadCountKey gatewayIpLoadCountKey; + + /* + * 操作实例IP和权重缓存 + * */ + @Autowired + private GatewayWeightCache gatewayWeightCache; + + /* + * 操作实例IP序列缓存 + * */ + @Autowired + private GatewayOrderCache gatewayOrderCache; + +// //刷新所有的缓存 +// @Scheduled(cron = "0/2 * * * * ?") +// public void refreshAllCache() { +// this.getNodeIDList(); +// this.getNodeInfos(); +// this.getNodeIPList(); +// this.getIpAndLoadCounts(); +// this.getIpAndWeights(); +// this.getLoadNodeOrderListByIpAndWeights(); +// } + + + + +} diff --git a/src/main/java/com/loadCenter/aliyun/service/GatewayLoadService.java b/src/main/java/com/loadCenter/aliyun/service/GatewayLoadService.java index 417c95f..98b75f5 100644 --- a/src/main/java/com/loadCenter/aliyun/service/GatewayLoadService.java +++ b/src/main/java/com/loadCenter/aliyun/service/GatewayLoadService.java @@ -9,11 +9,7 @@ import com.loadCenter.aliyun.common.aliyun.config.Result; * @Date 22:30 2024/4/18 */ public interface GatewayLoadService { - /** - * 负载节点 - * @return 返回负载节点 - */ - String loadNode(); + Result getAssignedServer(); diff --git a/src/main/java/com/loadCenter/aliyun/service/impl/GatewayLoadServiceImpl.java b/src/main/java/com/loadCenter/aliyun/service/impl/GatewayLoadServiceImpl.java index 35d1bb6..86f2f1e 100644 --- a/src/main/java/com/loadCenter/aliyun/service/impl/GatewayLoadServiceImpl.java +++ b/src/main/java/com/loadCenter/aliyun/service/impl/GatewayLoadServiceImpl.java @@ -2,9 +2,6 @@ package com.loadCenter.aliyun.service.impl; import com.loadCenter.aliyun.common.aliyun.config.Result; -import com.loadCenter.aliyun.gateway.cache.GatewayLoadNodeCache; -import com.loadCenter.aliyun.gateway.cache.GatewayLoadSeriesCache; -import com.loadCenter.aliyun.gateway.cache.GatewayNodeCache; import com.loadCenter.aliyun.gateway.model.GatewayNodeInfo; import com.loadCenter.aliyun.service.GatewayLoadService; import lombok.AllArgsConstructor; @@ -16,38 +13,6 @@ public class GatewayLoadServiceImpl implements GatewayLoadService { private final Long nodeLength = 100L; - /** - * 负载信息 - */ - private final GatewayLoadNodeCache gatewayLoadNodeCache; - - /** - * 负载序列 - */ - private final GatewayLoadSeriesCache gatewayLoadSeriesCache; - - /** - * 节点信息 - */ - private final GatewayNodeCache gatewayNodeCache; - - /** - * 获得负载节点外网地址 - * @return - */ - @Override - public String loadNode() { - Long seriesLoad = gatewayLoadSeriesCache.incrementAndGet(); - - Long seriesLoadIndex= seriesLoad % nodeLength; - - String loadNodeId = gatewayLoadNodeCache.getByIndex(seriesLoadIndex); - - GatewayNodeInfo gatewayNodeInfo = gatewayNodeCache.get(loadNodeId); - - //返回外网节点 - return gatewayNodeInfo.getPublicIdAddress(); - } @Override public Result getAssignedServer() {