From d1cae9e9222f9be137d6efc85f047c7d62179980 Mon Sep 17 00:00:00 2001 From: Su ZeJing <3039179835@qq.com> Date: Fri, 19 Apr 2024 14:23:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=9F=E8=BD=BD=E4=B8=AD=E5=BF=83=E5=88=9D?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aliyun/controller/GatewayController.java | 27 ++++++++++ .../gateway/cache/GatewayLoadNodeCache.java | 50 +++++++++++++++++ .../gateway/cache/GatewayLoadSeriesCache.java | 53 +++++++++++++++++++ .../gateway/cache/GatewayNodeCache.java | 46 ++++++++++++++++ .../gateway/cache/GatewayNodeScoreCache.java | 16 ++++++ .../gateway/cache/GatewayNodeSetVinCache.java | 17 ++++++ .../cache/GatewayVehicleLineNodeCache.java | 16 ++++++ .../cache/abs/GatewayNodeCacheAbs.java | 16 ++++++ .../aliyun/gateway/model/GatewayNodeInfo.java | 33 ++++++++++++ .../aliyun/service/GatewayLoadService.java | 15 ++++++ .../service/impl/GatewayLoadServiceImpl.java | 46 ++++++++++++++++ 11 files changed, 335 insertions(+) create mode 100644 src/main/java/com/loadCenter/aliyun/controller/GatewayController.java create mode 100644 src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayLoadNodeCache.java create mode 100644 src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayLoadSeriesCache.java create mode 100644 src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeCache.java create mode 100644 src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeScoreCache.java create mode 100644 src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeSetVinCache.java create mode 100644 src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayVehicleLineNodeCache.java create mode 100644 src/main/java/com/loadCenter/aliyun/gateway/cache/abs/GatewayNodeCacheAbs.java create mode 100644 src/main/java/com/loadCenter/aliyun/gateway/model/GatewayNodeInfo.java create mode 100644 src/main/java/com/loadCenter/aliyun/service/GatewayLoadService.java create mode 100644 src/main/java/com/loadCenter/aliyun/service/impl/GatewayLoadServiceImpl.java diff --git a/src/main/java/com/loadCenter/aliyun/controller/GatewayController.java b/src/main/java/com/loadCenter/aliyun/controller/GatewayController.java new file mode 100644 index 0000000..c5cf7f1 --- /dev/null +++ b/src/main/java/com/loadCenter/aliyun/controller/GatewayController.java @@ -0,0 +1,27 @@ +package com.loadCenter.aliyun.controller; + +import com.loadCenter.aliyun.domain.Result; +import com.loadCenter.aliyun.service.GatewayLoadService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @ClassName GatewayController + * @Description 网关控制层 + * @Author ZeJinG.Su + * @Date 20:58 2024/4/18 + */ +@RestController +@RequestMapping("/gateway") +public class GatewayController { + + @Autowired + private GatewayLoadService service; + + @GetMapping("/locd/node") + public Result loadNode(){ + return Result.success(service.loadNode()); + } +} diff --git a/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayLoadNodeCache.java b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayLoadNodeCache.java new file mode 100644 index 0000000..f40fdae --- /dev/null +++ b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayLoadNodeCache.java @@ -0,0 +1,50 @@ +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 new file mode 100644 index 0000000..aeb4dcc --- /dev/null +++ b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayLoadSeriesCache.java @@ -0,0 +1,53 @@ +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 new file mode 100644 index 0000000..30f4bcc --- /dev/null +++ b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeCache.java @@ -0,0 +1,46 @@ +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/GatewayNodeScoreCache.java b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeScoreCache.java new file mode 100644 index 0000000..92bad04 --- /dev/null +++ b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeScoreCache.java @@ -0,0 +1,16 @@ +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 new file mode 100644 index 0000000..c7f446a --- /dev/null +++ b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayNodeSetVinCache.java @@ -0,0 +1,17 @@ +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/GatewayVehicleLineNodeCache.java b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayVehicleLineNodeCache.java new file mode 100644 index 0000000..a49ce09 --- /dev/null +++ b/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayVehicleLineNodeCache.java @@ -0,0 +1,16 @@ +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/abs/GatewayNodeCacheAbs.java b/src/main/java/com/loadCenter/aliyun/gateway/cache/abs/GatewayNodeCacheAbs.java new file mode 100644 index 0000000..3ca786f --- /dev/null +++ b/src/main/java/com/loadCenter/aliyun/gateway/cache/abs/GatewayNodeCacheAbs.java @@ -0,0 +1,16 @@ +package com.loadCenter.aliyun.gateway.cache.abs; + +import com.loadCenter.aliyun.utils.redis.RedisService; +import org.springframework.beans.factory.annotation.Autowired; + +public abstract class GatewayNodeCacheAbs { + + @Autowired + public RedisService redisService; + + public abstract String getPre(); + + public String encode(K key){ + return getPre()+key; + } +} diff --git a/src/main/java/com/loadCenter/aliyun/gateway/model/GatewayNodeInfo.java b/src/main/java/com/loadCenter/aliyun/gateway/model/GatewayNodeInfo.java new file mode 100644 index 0000000..e357236 --- /dev/null +++ b/src/main/java/com/loadCenter/aliyun/gateway/model/GatewayNodeInfo.java @@ -0,0 +1,33 @@ +package com.loadCenter.aliyun.gateway.model; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +/** + * @ClassName GatewayNodeInfo + * @Description 网关节点信息 + * @Author ZeJinG.Su + * @Date 16:08 2024/4/18 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class GatewayNodeInfo { + /** + * 节点ID + */ + private String nodeId; + + /** + * 公网IP地址 + */ + private String publicIdAddress; + + /** + * 内网IP地址 + */ + private String privateIdAddress; + +} diff --git a/src/main/java/com/loadCenter/aliyun/service/GatewayLoadService.java b/src/main/java/com/loadCenter/aliyun/service/GatewayLoadService.java new file mode 100644 index 0000000..f349e42 --- /dev/null +++ b/src/main/java/com/loadCenter/aliyun/service/GatewayLoadService.java @@ -0,0 +1,15 @@ +package com.loadCenter.aliyun.service; +/** + * @ClassName GatewayLoadService + * @Description 网关负载业务 + * @Author ZeJinG.Su + * @Date 22:30 2024/4/18 + */ +public interface GatewayLoadService { + /** + * 负载节点 + * @return 返回负载节点 + */ + String loadNode(); + +} diff --git a/src/main/java/com/loadCenter/aliyun/service/impl/GatewayLoadServiceImpl.java b/src/main/java/com/loadCenter/aliyun/service/impl/GatewayLoadServiceImpl.java new file mode 100644 index 0000000..00aec66 --- /dev/null +++ b/src/main/java/com/loadCenter/aliyun/service/impl/GatewayLoadServiceImpl.java @@ -0,0 +1,46 @@ +package com.loadCenter.aliyun.service.impl; + + +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; +import org.springframework.stereotype.Service; + +@Service +@AllArgsConstructor +public class GatewayLoadServiceImpl implements GatewayLoadService { + private final Long nodeLength = 100L; + + + /** + * 负载信息 + */ + private final GatewayLoadNodeCache gatewayLoadNodeCache; + + /** + * 负载序列 + */ + private final GatewayLoadSeriesCache gatewayLoadSeriesCache; + + /** + * 节点信息 + */ + private final GatewayNodeCache gatewayNodeCache; + + @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(); + } +}