数据类型结构及模型
master
JangCan 2024-04-18 16:28:56 +08:00
parent f35e44a18b
commit edaffed736
19 changed files with 322 additions and 12 deletions

View File

@ -7,7 +7,7 @@ import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;
import com.loadcenter.common.aliyun.model.InstanceSpecification;
import com.loadcenter.common.aliyun.config.AliConfig;
import com.loadcenter.domain.InstancesInformation;
import com.loadcenter.common.domain.InstancesInformation;
import com.loadcenter.common.redis.service.RedisService;
import com.loadcenter.common.utils.user.UserUtil;
import lombok.extern.slf4j.Slf4j;

View File

@ -1,4 +1,4 @@
package com.loadcenter.domain;
package com.loadcenter.common.domain;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package com.loadcenter.domain;
package com.loadcenter.common.domain;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package com.loadcenter.domain;
package com.loadcenter.common.domain;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package com.loadcenter.domain;
package com.loadcenter.common.domain;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package com.loadcenter.domain.resp;
package com.loadcenter.common.domain.resp;
/**
*

View File

@ -1,4 +1,4 @@
package com.loadcenter.domain.resp;
package com.loadcenter.common.domain.resp;
import lombok.Data;

View File

@ -143,6 +143,10 @@ public class RedisService {
return count == null ? 0 : count;
}
public <T> T getCacheListValue(final String key,long index){
return (T) redisTemplate.opsForList().index(key,index);
}
/**
* list
*

View File

@ -0,0 +1,29 @@
package com.loadcenter.controller;
import com.loadcenter.common.domain.resp.Result;
import com.loadcenter.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 Can.J
* @Date 2024/4/18 16:09
*/
@RestController
@RequestMapping("/gateway")
public class GatewayController {
@Autowired
private GatewayLoadService gatewayLoadService;
@GetMapping("/load/node")
public Result<String> loadNode(){
return Result.success(gatewayLoadService.loadNode());
}
}

View File

@ -1,6 +1,6 @@
package com.loadcenter.controller;
import com.loadcenter.domain.resp.Result;
import com.loadcenter.common.domain.resp.Result;
import com.loadcenter.service.LoadCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;

View File

@ -0,0 +1,52 @@
package com.loadcenter.gateway.cache;
import com.loadcenter.gateway.cache.abs.GatewayCacheAbs;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @ClassName GatewayLoadNodeCache
* @Description
* @Author Can.J
* @Date 2024/4/18 15:31
*/
@Component
public class GatewayLoadNodeCache extends GatewayCacheAbs<String> {
// Redis中存储序列值的键名
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 getByIndex(Long index){
if(index ==null || index>100){
throw new RuntimeException("下表违法,0-100");
}
return redisService.getCacheListValue(encode(gatewayLoadNodeKey),index);
}
}

View File

@ -0,0 +1,57 @@
package com.loadcenter.gateway.cache;
import com.loadcenter.common.redis.service.RedisService;
import com.loadcenter.gateway.cache.abs.GatewayCacheAbs;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @ClassName GatewayLoadSeriesCache
* @Description
* @Author Can.J
* @Date 2024/4/18 15:04
*/
@Component
public class GatewayLoadSeriesCache extends GatewayCacheAbs<String> {
// Redis中存储序列值的键名
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);
}
/**
* 0
*/
public void reset(){
this.init();
}
}

View File

@ -0,0 +1,50 @@
package com.loadcenter.gateway.cache;
import com.loadcenter.gateway.cache.abs.GatewayCacheAbs;
import com.loadcenter.gateway.model.GatewayNodeInfo;
import com.loadcenter.common.redis.service.RedisService;
import org.springframework.stereotype.Component;
/**
* @ClassName GatewayNodeCache
* @Description
* @Author Can.J
* @Date 2024/4/18 14:38
*/
@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));
}
}

View File

@ -0,0 +1,21 @@
package com.loadcenter.gateway.cache.abs;
import com.loadcenter.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @ClassName GatewayCacheAbs
* @Description
* @Author Can.J
* @Date 2024/4/18 15:35
*/
public abstract class GatewayCacheAbs<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.gateway.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @ClassName GatewayNodeInfo
* @Description
* @Author Can.J
* @Date 2024/4/18 14:32
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class GatewayNodeInfo {
/**
* id
*/
private String nodeId;
/**
* ip
*/
private String publicAddress;
/**
* ip
*/
private String privateAddress;
}

View File

@ -0,0 +1,14 @@
package com.loadcenter.service;
/**
* @ClassName GatewayLoadService
* @Description
* @Author Can.J
* @Date 2024/4/18 16:11
*/
public interface GatewayLoadService {
/**
*
* @return
*/
String loadNode();
}

View File

@ -1,6 +1,6 @@
package com.loadcenter.service;
import com.loadcenter.domain.resp.Result;
import com.loadcenter.common.domain.resp.Result;
/**
* @ClassName LoadCenterService

View File

@ -0,0 +1,50 @@
package com.loadcenter.service.impl;
import com.loadcenter.gateway.cache.GatewayLoadNodeCache;
import com.loadcenter.gateway.cache.GatewayLoadSeriesCache;
import com.loadcenter.gateway.cache.GatewayNodeCache;
import com.loadcenter.gateway.model.GatewayNodeInfo;
import com.loadcenter.service.GatewayLoadService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @ClassName GatewayLoadServiceImpl
* @Description
* @Author Can.J
* @Date 2024/4/18 16:10
*/
@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.getByIndex(seriesLoadIndex);
GatewayNodeInfo gatewayNodeInfo = gatewayNodeCache.get(loadNodeId);
return gatewayNodeInfo.getPublicAddress();
}
}

View File

@ -3,9 +3,9 @@ package com.loadcenter.service.impl;
import com.alibaba.fastjson2.JSON;
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
import com.loadcenter.common.aliyun.service.AliYunEcsService;
import com.loadcenter.domain.IpAndLoadCount;
import com.loadcenter.domain.IpAndWeight;
import com.loadcenter.domain.resp.Result;
import com.loadcenter.common.domain.IpAndLoadCount;
import com.loadcenter.common.domain.IpAndWeight;
import com.loadcenter.common.domain.resp.Result;
import com.loadcenter.common.redis.service.RedisService;
import com.loadcenter.common.utils.user.UserUtil;
import com.loadcenter.common.utils.mqtt.MqttUtil;