parent
f35e44a18b
commit
edaffed736
|
@ -7,7 +7,7 @@ import com.aliyun.teautil.Common;
|
||||||
import com.aliyun.teautil.models.RuntimeOptions;
|
import com.aliyun.teautil.models.RuntimeOptions;
|
||||||
import com.loadcenter.common.aliyun.model.InstanceSpecification;
|
import com.loadcenter.common.aliyun.model.InstanceSpecification;
|
||||||
import com.loadcenter.common.aliyun.config.AliConfig;
|
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.redis.service.RedisService;
|
||||||
import com.loadcenter.common.utils.user.UserUtil;
|
import com.loadcenter.common.utils.user.UserUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.loadcenter.domain;
|
package com.loadcenter.common.domain;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.loadcenter.domain;
|
package com.loadcenter.common.domain;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.loadcenter.domain;
|
package com.loadcenter.common.domain;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.loadcenter.domain;
|
package com.loadcenter.common.domain;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.loadcenter.domain.resp;
|
package com.loadcenter.common.domain.resp;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回状态码
|
* 返回状态码
|
|
@ -1,4 +1,4 @@
|
||||||
package com.loadcenter.domain.resp;
|
package com.loadcenter.common.domain.resp;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
|
@ -143,6 +143,10 @@ public class RedisService {
|
||||||
return count == null ? 0 : count;
|
return count == null ? 0 : count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <T> T getCacheListValue(final String key,long index){
|
||||||
|
return (T) redisTemplate.opsForList().index(key,index);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得缓存的list对象
|
* 获得缓存的list对象
|
||||||
*
|
*
|
||||||
|
|
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
package com.loadcenter.controller;
|
package com.loadcenter.controller;
|
||||||
|
|
||||||
import com.loadcenter.domain.resp.Result;
|
import com.loadcenter.common.domain.resp.Result;
|
||||||
import com.loadcenter.service.LoadCenterService;
|
import com.loadcenter.service.LoadCenterService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
package com.loadcenter.service;
|
package com.loadcenter.service;
|
||||||
|
|
||||||
import com.loadcenter.domain.resp.Result;
|
import com.loadcenter.common.domain.resp.Result;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ClassName LoadCenterService
|
* @ClassName LoadCenterService
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,9 +3,9 @@ package com.loadcenter.service.impl;
|
||||||
import com.alibaba.fastjson2.JSON;
|
import com.alibaba.fastjson2.JSON;
|
||||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
|
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
|
||||||
import com.loadcenter.common.aliyun.service.AliYunEcsService;
|
import com.loadcenter.common.aliyun.service.AliYunEcsService;
|
||||||
import com.loadcenter.domain.IpAndLoadCount;
|
import com.loadcenter.common.domain.IpAndLoadCount;
|
||||||
import com.loadcenter.domain.IpAndWeight;
|
import com.loadcenter.common.domain.IpAndWeight;
|
||||||
import com.loadcenter.domain.resp.Result;
|
import com.loadcenter.common.domain.resp.Result;
|
||||||
import com.loadcenter.common.redis.service.RedisService;
|
import com.loadcenter.common.redis.service.RedisService;
|
||||||
import com.loadcenter.common.utils.user.UserUtil;
|
import com.loadcenter.common.utils.user.UserUtil;
|
||||||
import com.loadcenter.common.utils.mqtt.MqttUtil;
|
import com.loadcenter.common.utils.mqtt.MqttUtil;
|
||||||
|
|
Loading…
Reference in New Issue