xiaoSu 2024-04-20 09:18:21 +08:00
parent b12705e6c5
commit 458b6747dd
16 changed files with 380 additions and 243 deletions

View File

@ -21,11 +21,6 @@ public class GatewayController {
@Autowired
private GatewayLoadService service;
@GetMapping("/locd/node")
public Result<String> loadNode(){
return Result.success(service.loadNode());
}
@PostMapping("/getAssignedServer")
public Result<String> getAssignedServer(){
return service.getAssignedServer();

View File

@ -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<String> {
private final static String gatewayIpLoadCountKey = "ipAndCount";
@Override
public String getPre() {
return "gateway:load:";
}
/**
*
* @param ipAndLoadCounts
*/
public void put(List<NodeLoadNum> ipAndLoadCounts) {
this.remove();
redisService.setCacheList(encode(gatewayIpLoadCountKey), ipAndLoadCounts);
}
/**
*
* @return
*/
public List<NodeLoadNum> get() {
List<Object> cacheList = redisService.getCacheList(encode(gatewayIpLoadCountKey));
return JSON.parseArray(JSON.toJSONString(cacheList), NodeLoadNum.class);
}
/**
*
*/
public void remove() {
redisService.deleteObject(encode(gatewayIpLoadCountKey));
}
}

View File

@ -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<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);
}
}

View File

@ -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<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();
}
}

View File

@ -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<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));
}
}

View File

@ -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<String> {
private final static String gatewayLoadNodeIDKey = "NodeId";
@Override
public String getPre() {
return "gateway:load:";
}
/**
*
* @param gatewayNodeIds
*/
public void put(List<String> gatewayNodeIds) {
this.remove();
redisService.setCacheList(encode(gatewayLoadNodeIDKey), gatewayNodeIds);
}
/**
*
* @return
*/
public List<String> get() {
return redisService.getCacheList(encode(gatewayLoadNodeIDKey));
}
/**
*
*/
public void remove() {
redisService.deleteObject(encode(gatewayLoadNodeIDKey));
}
}

View File

@ -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<String> {
private final static String gatewayLoadNodeIPKey = "NodeIp";
@Override
public String getPre() {
return "gateway:load:";
}
/**
*
* @param gatewayNodeIps
*/
public void put(List<String> gatewayNodeIps) {
this.remove();
redisService.setCacheList(encode(gatewayLoadNodeIPKey), gatewayNodeIps);
}
/**
*
* @return
*/
public List<String> get() {
return redisService.getCacheList(encode(gatewayLoadNodeIPKey));
}
/**
*
*/
public void remove() {
redisService.deleteObject(encode(gatewayLoadNodeIPKey));
}
}

View File

@ -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<String> {
//redis Key
private final static String gatewayLoadInfoKey = "node:";
@Override
public String getPre() {
return "gateway:load:";
}
/**
*
* @param gatewayNodeInfos
*/
public void put(List<GatewayNodeInfo> gatewayNodeInfos) {
this.remove();
redisService.setCacheList(encode(gatewayLoadInfoKey), gatewayNodeInfos);
}
/**
*
* @return
*/
public List<GatewayNodeInfo> get() {
List<Object> cacheList = redisService.getCacheList(encode(gatewayLoadInfoKey));
return JSON.parseArray(JSON.toJSONString(cacheList), GatewayNodeInfo.class);
}
/**
*
*/
public void remove() {
redisService.deleteObject(encode(gatewayLoadInfoKey));
}
}

View File

@ -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<String> {
@Override
public String getPre() {
return null;
}
}

View File

@ -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<String> {
@Override
public String getPre() {
return null;
}
}

View File

@ -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<String> {
private final static String gatewayOrderCache = "order";
@Override
public String getPre() {
return "gateway:load:";
}
/**
*
* @param gatewayNodeOrders
*/
public void put(List<String> gatewayNodeOrders) {
this.remove();
redisService.setCacheList(encode(gatewayOrderCache), gatewayNodeOrders);
}
/**
*
* @return
*/
public List<String> get() {
return redisService.getCacheList(encode(gatewayOrderCache));
}
/**
*
*/
public void remove() {
redisService.deleteObject(encode(gatewayOrderCache));
}
}

View File

@ -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<String> {
@Override
public String getPre() {
return null;
}
}

View File

@ -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<String> {
private final static String gatewayWeightKey = "ipAndWeight";
@Override
public String getPre() {
return "gateway:load:";
}
/**
*
* @param ipAndWeights
*/
public void put(List<NodeLoadWeight> ipAndWeights) {
this.remove();
redisService.setCacheList(encode(gatewayWeightKey), ipAndWeights);
}
/**
*
* @return
*/
public List<NodeLoadWeight> get() {
List<Object> cacheList = redisService.getCacheList(encode(gatewayWeightKey));
return JSON.parseArray(JSON.toJSONString(cacheList), NodeLoadWeight.class);
}
/**
*
*/
public void remove() {
redisService.deleteObject(encode(gatewayWeightKey));
}
}

View File

@ -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();
// }
}

View File

@ -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<String> getAssignedServer();

View File

@ -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<String> getAssignedServer() {