负载中心初版

master
xiaoSu 2024-04-19 14:23:42 +08:00
parent d0ae9efa07
commit d1cae9e922
11 changed files with 335 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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