From f0a9228de69e8a09801e315fe6707a02dce4c8c2 Mon Sep 17 00:00:00 2001 From: DongZeLiang <2746733890@qq.com> Date: Thu, 18 Apr 2024 10:58:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/muyu/LoadCenterApplication.java | 16 +++ .../java/com/muyu/common/domain/Result.java | 112 ++++++++++++++++++ .../muyu/controller/GatewayController.java | 27 +++++ .../gateway/cache/GatewayLoadNodeCache.java | 50 ++++++++ .../gateway/cache/GatewayLoadSeriesCache.java | 53 +++++++++ .../muyu/gateway/cache/GatewayNodeCache.java | 45 +++++++ .../gateway/cache/GatewayNodeScoreCache.java | 10 ++ .../gateway/cache/GatewayNodeSetVinCache.java | 10 ++ .../cache/GatewayVehicleLineNodeCache.java | 10 ++ .../gateway/cache/abs/GatewayCacheAbs.java | 22 ++++ .../muyu/gateway/model/GatewayNodeInfo.java | 35 ++++++ .../com/muyu/service/GatewayLoadService.java | 16 +++ .../service/impl/GatewayLoadServiceImpl.java | 52 ++++++++ 13 files changed, 458 insertions(+) create mode 100644 src/main/java/com/muyu/LoadCenterApplication.java create mode 100644 src/main/java/com/muyu/common/domain/Result.java create mode 100644 src/main/java/com/muyu/controller/GatewayController.java create mode 100644 src/main/java/com/muyu/gateway/cache/GatewayLoadNodeCache.java create mode 100644 src/main/java/com/muyu/gateway/cache/GatewayLoadSeriesCache.java create mode 100644 src/main/java/com/muyu/gateway/cache/GatewayNodeCache.java create mode 100644 src/main/java/com/muyu/gateway/cache/GatewayNodeScoreCache.java create mode 100644 src/main/java/com/muyu/gateway/cache/GatewayNodeSetVinCache.java create mode 100644 src/main/java/com/muyu/gateway/cache/GatewayVehicleLineNodeCache.java create mode 100644 src/main/java/com/muyu/gateway/cache/abs/GatewayCacheAbs.java create mode 100644 src/main/java/com/muyu/gateway/model/GatewayNodeInfo.java create mode 100644 src/main/java/com/muyu/service/GatewayLoadService.java create mode 100644 src/main/java/com/muyu/service/impl/GatewayLoadServiceImpl.java diff --git a/src/main/java/com/muyu/LoadCenterApplication.java b/src/main/java/com/muyu/LoadCenterApplication.java new file mode 100644 index 0000000..4360390 --- /dev/null +++ b/src/main/java/com/muyu/LoadCenterApplication.java @@ -0,0 +1,16 @@ +package com.muyu; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author DongZl + * @description: 负载中心启动类 + * @Date 2024/4/12 下午5:11 + */ +@SpringBootApplication +public class LoadCenterApplication { + public static void main (String[] args) { + SpringApplication.run(LoadCenterApplication.class, args); + } +} diff --git a/src/main/java/com/muyu/common/domain/Result.java b/src/main/java/com/muyu/common/domain/Result.java new file mode 100644 index 0000000..c482818 --- /dev/null +++ b/src/main/java/com/muyu/common/domain/Result.java @@ -0,0 +1,112 @@ +package com.muyu.common.domain; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + * 响应信息主体 + * + * @author muyu + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class Result implements Serializable { + /** + * 成功 + */ + public static final int SUCCESS = 200; + /** + * 失败 + */ + public static final int FAIL = 500; + /** + * 警告 + */ + public static final int WARN = 501; + + private static final long serialVersionUID = 1L; + private int code; + + private String msg; + + private T data; + + public static Result buildCode(int code, String msg, T data){ + return restResult(data, code, msg); + } + + public static Result success () { + return restResult(null, SUCCESS, null); + } + + public static Result success (T data) { + return restResult(data, SUCCESS, null); + } + + public static Result success (T data, String msg) { + return restResult(data, SUCCESS, msg); + } + + public static Result error () { + return restResult(null, FAIL, null); + } + + public static Result error (String msg) { + return restResult(null, FAIL, msg); + } + + public static Result error (T data) { + return restResult(data, FAIL, null); + } + + public static Result error (T data, String msg) { + return restResult(data, FAIL, msg); + } + + public static Result error (int code, String msg) { + return restResult(null, code, msg); + } + + public static Result warn () { + return restResult(null, WARN, null); + } + + public static Result warn (String msg) { + return restResult(null, WARN, msg); + } + + public static Result warn (T data) { + return restResult(data, WARN, null); + } + + public static Result warn (T data, String msg) { + return restResult(data, WARN, msg); + } + + public static Result warn (int code, String msg) { + return restResult(null, code, msg); + } + + private static Result restResult (T data, int code, String msg) { + return Result.builder() + .code(code) + .data(data) + .msg(msg) + .build(); + } + + public static Boolean isError (Result ret) { + return !isSuccess(ret); + } + + public static Boolean isSuccess (Result ret) { + return Result.SUCCESS == ret.getCode(); + } + +} diff --git a/src/main/java/com/muyu/controller/GatewayController.java b/src/main/java/com/muyu/controller/GatewayController.java new file mode 100644 index 0000000..f476578 --- /dev/null +++ b/src/main/java/com/muyu/controller/GatewayController.java @@ -0,0 +1,27 @@ +package com.muyu.controller; + +import com.muyu.common.domain.Result; +import com.muyu.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; + +/** + * @Author: DongZeLiang + * @date: 2024/4/18 + * @Description: 网关控制层 + * @Version: 1.0 + */ +@RestController +@RequestMapping("/gateway") +public class GatewayController { + + @Autowired + private GatewayLoadService gatewayLoadService; + + @GetMapping("/load/node") + public Result loadNode() { + return Result.success(gatewayLoadService.loadNode()); + } +} diff --git a/src/main/java/com/muyu/gateway/cache/GatewayLoadNodeCache.java b/src/main/java/com/muyu/gateway/cache/GatewayLoadNodeCache.java new file mode 100644 index 0000000..7c86592 --- /dev/null +++ b/src/main/java/com/muyu/gateway/cache/GatewayLoadNodeCache.java @@ -0,0 +1,50 @@ +package com.muyu.gateway.cache; + +import com.muyu.gateway.cache.abs.GatewayCacheAbs; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @Author: DongZeLiang + * @date: 2024/4/18 + * @Description: 网关负载节点缓存 + * @Version: 1.0 + */ +@Component +public class GatewayLoadNodeCache extends GatewayCacheAbs { + + private final static String gatewayLoadNodeKey = "node"; + + @Override + public String getPre () { return "gateway:load:";} + + /** + * 存负载集合 + * @param nodeList 节点权重集合 + */ + public void put(List nodeList){ + redisService.deleteObject(encode(gatewayLoadNodeKey)); + redisService.setCacheList(encode(gatewayLoadNodeKey), nodeList); + } + + /** + * 获取所有负载节点 + * @return 负载节点集合 + */ + public List get(){ + return redisService.getCacheList(encode(gatewayLoadNodeKey)); + } + + /** + * 通过下标获取节点 + * @param index 下标 + * @return 指定节点 + */ + public String getBydIndex(Long index){ + if (index == null || index > 100){ + throw new RuntimeException("下标违法,0-100"); + } + return redisService.getCacheListValue(encode(gatewayLoadNodeKey), index); + } +} diff --git a/src/main/java/com/muyu/gateway/cache/GatewayLoadSeriesCache.java b/src/main/java/com/muyu/gateway/cache/GatewayLoadSeriesCache.java new file mode 100644 index 0000000..2648d84 --- /dev/null +++ b/src/main/java/com/muyu/gateway/cache/GatewayLoadSeriesCache.java @@ -0,0 +1,53 @@ +package com.muyu.gateway.cache; + +import com.muyu.common.redis.service.RedisService; +import com.muyu.gateway.cache.abs.GatewayCacheAbs; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; + +/** + * @Author: DongZeLiang + * @date: 2024/4/18 + * @Description: 网关负载序列 + * @Version: 1.0 + */ +@Component +public class GatewayLoadSeriesCache extends GatewayCacheAbs { + + private final static String gatewayLoadSeriesKey = "series"; + + @Override + public String getPre () { return "gateway:load:";} + + /** + * bean创建完成之后执行方法 + */ + @PostConstruct + public void init(){ + redisService.setCacheObject(encode(gatewayLoadSeriesKey), 0L); + } + + /** + * 获取当前序列值 + * @return 序列值 + */ + public Long get(){ + return redisService.getCacheObject(encode(gatewayLoadSeriesKey)); + } + + /** + * 获取自增序列值 + * @return 自增后的值 + */ + public Long incrementAndGet(){ + return redisService.increment(encode(gatewayLoadSeriesKey), 1L); + } + + /** + * 重置 + */ + public void reset(){ + this.init(); + } +} diff --git a/src/main/java/com/muyu/gateway/cache/GatewayNodeCache.java b/src/main/java/com/muyu/gateway/cache/GatewayNodeCache.java new file mode 100644 index 0000000..3f10885 --- /dev/null +++ b/src/main/java/com/muyu/gateway/cache/GatewayNodeCache.java @@ -0,0 +1,45 @@ +package com.muyu.gateway.cache; + +import com.muyu.gateway.cache.abs.GatewayCacheAbs; +import com.muyu.gateway.model.GatewayNodeInfo; +import org.springframework.stereotype.Component; + +/** + * @Author: DongZeLiang + * @date: 2024/4/18 + * @Description: 网关节点缓存 + * @Version: 1.0 + */ +@Component +public class GatewayNodeCache extends GatewayCacheAbs { + + @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/muyu/gateway/cache/GatewayNodeScoreCache.java b/src/main/java/com/muyu/gateway/cache/GatewayNodeScoreCache.java new file mode 100644 index 0000000..c87215a --- /dev/null +++ b/src/main/java/com/muyu/gateway/cache/GatewayNodeScoreCache.java @@ -0,0 +1,10 @@ +package com.muyu.gateway.cache; + +/** + * @Author: DongZeLiang + * @date: 2024/4/18 + * @Description: 网关节点分数 + * @Version: 1.0 + */ +public class GatewayNodeScoreCache { +} diff --git a/src/main/java/com/muyu/gateway/cache/GatewayNodeSetVinCache.java b/src/main/java/com/muyu/gateway/cache/GatewayNodeSetVinCache.java new file mode 100644 index 0000000..43a2c0d --- /dev/null +++ b/src/main/java/com/muyu/gateway/cache/GatewayNodeSetVinCache.java @@ -0,0 +1,10 @@ +package com.muyu.gateway.cache; + +/** + * @Author: DongZeLiang + * @date: 2024/4/18 + * @Description: 网关节点存储VIN详情 + * @Version: 1.0 + */ +public class GatewayNodeSetVinCache { +} diff --git a/src/main/java/com/muyu/gateway/cache/GatewayVehicleLineNodeCache.java b/src/main/java/com/muyu/gateway/cache/GatewayVehicleLineNodeCache.java new file mode 100644 index 0000000..9f52e0d --- /dev/null +++ b/src/main/java/com/muyu/gateway/cache/GatewayVehicleLineNodeCache.java @@ -0,0 +1,10 @@ +package com.muyu.gateway.cache; + +/** + * @Author: DongZeLiang + * @date: 2024/4/18 + * @Description: 网关车辆对应网关节点 + * @Version: 1.0 + */ +public class GatewayVehicleLineNodeCache { +} diff --git a/src/main/java/com/muyu/gateway/cache/abs/GatewayCacheAbs.java b/src/main/java/com/muyu/gateway/cache/abs/GatewayCacheAbs.java new file mode 100644 index 0000000..d2c2109 --- /dev/null +++ b/src/main/java/com/muyu/gateway/cache/abs/GatewayCacheAbs.java @@ -0,0 +1,22 @@ +package com.muyu.gateway.cache.abs; + +import com.muyu.common.redis.service.RedisService; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * @Author: DongZeLiang + * @date: 2024/4/18 + * @Description: 缓存抽象类 + * @Version: 1.0 + */ +public abstract class GatewayCacheAbs { + + @Autowired + public RedisService redisService; + + public abstract String getPre(); + + public String encode(K key){ + return getPre() + key; + } +} diff --git a/src/main/java/com/muyu/gateway/model/GatewayNodeInfo.java b/src/main/java/com/muyu/gateway/model/GatewayNodeInfo.java new file mode 100644 index 0000000..add6d0d --- /dev/null +++ b/src/main/java/com/muyu/gateway/model/GatewayNodeInfo.java @@ -0,0 +1,35 @@ +package com.muyu.gateway.model; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @Author: DongZeLiang + * @date: 2024/4/18 + * @Description: 网关节点信息 + * @Version: 1.0 + */ +@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/muyu/service/GatewayLoadService.java b/src/main/java/com/muyu/service/GatewayLoadService.java new file mode 100644 index 0000000..b458b1c --- /dev/null +++ b/src/main/java/com/muyu/service/GatewayLoadService.java @@ -0,0 +1,16 @@ +package com.muyu.service; + +/** + * @Author: DongZeLiang + * @date: 2024/4/18 + * @Description: 网关负载业务 + * @Version: 1.0 + */ +public interface GatewayLoadService { + + /** + * 负载节点 + * @return 返回负载节点 + */ + String loadNode (); +} diff --git a/src/main/java/com/muyu/service/impl/GatewayLoadServiceImpl.java b/src/main/java/com/muyu/service/impl/GatewayLoadServiceImpl.java new file mode 100644 index 0000000..29e9f75 --- /dev/null +++ b/src/main/java/com/muyu/service/impl/GatewayLoadServiceImpl.java @@ -0,0 +1,52 @@ +package com.muyu.service.impl; + +import com.muyu.gateway.cache.GatewayLoadNodeCache; +import com.muyu.gateway.cache.GatewayLoadSeriesCache; +import com.muyu.gateway.cache.GatewayNodeCache; +import com.muyu.gateway.model.GatewayNodeInfo; +import com.muyu.service.GatewayLoadService; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Service; + +/** + * @Author: DongZeLiang + * @date: 2024/4/18 + * @Description: 负载实现层 + * @Version: 1.0 + */ +@Service +@AllArgsConstructor +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.getBydIndex(seriesLoadIndex); + GatewayNodeInfo gatewayNodeInfo = gatewayNodeCache.get(loadNodeId); + return gatewayNodeInfo.getPublicIdAddress(); + } +}