初始化
parent
de22a25f44
commit
f0a9228de6
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<T> 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 <T> Result<T> buildCode(int code, String msg, T data){
|
||||
return restResult(data, code, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> success () {
|
||||
return restResult(null, SUCCESS, null);
|
||||
}
|
||||
|
||||
public static <T> Result<T> success (T data) {
|
||||
return restResult(data, SUCCESS, null);
|
||||
}
|
||||
|
||||
public static <T> Result<T> success (T data, String msg) {
|
||||
return restResult(data, SUCCESS, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> error () {
|
||||
return restResult(null, FAIL, null);
|
||||
}
|
||||
|
||||
public static <T> Result<T> error (String msg) {
|
||||
return restResult(null, FAIL, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> error (T data) {
|
||||
return restResult(data, FAIL, null);
|
||||
}
|
||||
|
||||
public static <T> Result<T> error (T data, String msg) {
|
||||
return restResult(data, FAIL, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> error (int code, String msg) {
|
||||
return restResult(null, code, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> warn () {
|
||||
return restResult(null, WARN, null);
|
||||
}
|
||||
|
||||
public static <T> Result<T> warn (String msg) {
|
||||
return restResult(null, WARN, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> warn (T data) {
|
||||
return restResult(data, WARN, null);
|
||||
}
|
||||
|
||||
public static <T> Result<T> warn (T data, String msg) {
|
||||
return restResult(data, WARN, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> warn (int code, String msg) {
|
||||
return restResult(null, code, msg);
|
||||
}
|
||||
|
||||
private static <T> Result<T> restResult (T data, int code, String msg) {
|
||||
return Result.<T>builder()
|
||||
.code(code)
|
||||
.data(data)
|
||||
.msg(msg)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static <T> Boolean isError (Result<T> ret) {
|
||||
return !isSuccess(ret);
|
||||
}
|
||||
|
||||
public static <T> Boolean isSuccess (Result<T> ret) {
|
||||
return Result.SUCCESS == ret.getCode();
|
||||
}
|
||||
|
||||
}
|
|
@ -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<String> loadNode() {
|
||||
return Result.success(gatewayLoadService.loadNode());
|
||||
}
|
||||
}
|
|
@ -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<String> {
|
||||
|
||||
private final static String gatewayLoadNodeKey = "node";
|
||||
|
||||
@Override
|
||||
public String getPre () { return "gateway:load:";}
|
||||
|
||||
/**
|
||||
* 存负载集合
|
||||
* @param nodeList 节点权重集合
|
||||
*/
|
||||
public void put(List<String> nodeList){
|
||||
redisService.deleteObject(encode(gatewayLoadNodeKey));
|
||||
redisService.setCacheList(encode(gatewayLoadNodeKey), nodeList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有负载节点
|
||||
* @return 负载节点集合
|
||||
*/
|
||||
public List<String> 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);
|
||||
}
|
||||
}
|
|
@ -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<String> {
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
|
@ -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<String> {
|
||||
|
||||
@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));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.muyu.gateway.cache;
|
||||
|
||||
/**
|
||||
* @Author: DongZeLiang
|
||||
* @date: 2024/4/18
|
||||
* @Description: 网关节点分数
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class GatewayNodeScoreCache {
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.muyu.gateway.cache;
|
||||
|
||||
/**
|
||||
* @Author: DongZeLiang
|
||||
* @date: 2024/4/18
|
||||
* @Description: 网关节点存储VIN详情
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class GatewayNodeSetVinCache {
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.muyu.gateway.cache;
|
||||
|
||||
/**
|
||||
* @Author: DongZeLiang
|
||||
* @date: 2024/4/18
|
||||
* @Description: 网关车辆对应网关节点
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class GatewayVehicleLineNodeCache {
|
||||
}
|
|
@ -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<K> {
|
||||
|
||||
@Autowired
|
||||
public RedisService redisService;
|
||||
|
||||
public abstract String getPre();
|
||||
|
||||
public String encode(K key){
|
||||
return getPre() + key;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.muyu.service;
|
||||
|
||||
/**
|
||||
* @Author: DongZeLiang
|
||||
* @date: 2024/4/18
|
||||
* @Description: 网关负载业务
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface GatewayLoadService {
|
||||
|
||||
/**
|
||||
* 负载节点
|
||||
* @return 返回负载节点
|
||||
*/
|
||||
String loadNode ();
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue