负载中心初版
parent
d0ae9efa07
commit
d1cae9e922
|
@ -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<String> loadNode(){
|
||||
return Result.success(service.loadNode());
|
||||
}
|
||||
}
|
|
@ -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<String> nodeList){
|
||||
redisService.deleteObject(encode(GATEWAY_LOAD_NODE_KEY));
|
||||
redisService.setCacheList(encode(GATEWAY_LOAD_NODE_KEY),nodeList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有负载节点
|
||||
* @return 负载节点集合
|
||||
*/
|
||||
public List<String> 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);
|
||||
}
|
||||
}
|
|
@ -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<String> {
|
||||
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();
|
||||
}
|
||||
}
|
|
@ -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<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,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<String> {
|
||||
@Override
|
||||
public String getPre() {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -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<String> {
|
||||
@Override
|
||||
public String getPre() {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -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<String> {
|
||||
@Override
|
||||
public String getPre() {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -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<K> {
|
||||
|
||||
@Autowired
|
||||
public RedisService redisService;
|
||||
|
||||
public abstract String getPre();
|
||||
|
||||
public String encode(K key){
|
||||
return getPre()+key;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
||||
}
|
|
@ -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();
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue