From 09ff525c26ae7101d2ee6eaabf452345b9f9b614 Mon Sep 17 00:00:00 2001 From: liuyunhu <3286117488@qq.com> Date: Fri, 19 Apr 2024 20:28:39 +0800 Subject: [PATCH] =?UTF-8?q?14=E4=BC=98=E5=8C=96=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/lyh/LoadCenterApplication.java | 10 +- .../lyh/common/aliyun/config/AliConfig.java | 26 +- .../aliyun/model/InstanceSpecification.java | 55 ++-- .../aliyun/service/AliYunEcsService.java | 271 +++++++++--------- .../java/com/lyh/common/domain/MqttInfo.java | 3 +- .../com/lyh/common/utils/mqtt/MqttUtil.java | 8 +- .../com/lyh/common/utils/user/UserUtil.java | 13 +- .../lyh/controller/LoadCenterController.java | 7 +- .../cache/GatewayIpAndLoadCountCache.java | 18 +- .../cache/GatewayIpAndLoadWeightCache.java | 18 +- .../lyh/gateway/cache/GatewayNodeIdCache.java | 18 +- .../gateway/cache/GatewayNodeInfoCache.java | 18 +- .../lyh/gateway/cache/GatewayNodeIpCache.java | 18 +- .../gateway/cache/GatewayNodeOrderCache.java | 18 +- .../gateway/cache/abs/GatewayCacheAbs.java | 3 +- .../com/lyh/gateway/mode/GatewayNodeInfo.java | 35 +-- .../com/lyh/gateway/mode/IpAndLoadCount.java | 9 +- .../com/lyh/gateway/mode/IpAndWeight.java | 9 +- src/main/java/com/lyh/handle/HandleCache.java | 72 +++-- src/main/java/com/lyh/job/Timer.java | 25 +- .../com/lyh/service/LoadCenterService.java | 3 +- .../service/impl/LoadCenterServiceImpl.java | 18 +- src/test/java/Test.java | 2 +- 23 files changed, 375 insertions(+), 302 deletions(-) diff --git a/src/main/java/com/lyh/LoadCenterApplication.java b/src/main/java/com/lyh/LoadCenterApplication.java index b8003a1..fd39399 100644 --- a/src/main/java/com/lyh/LoadCenterApplication.java +++ b/src/main/java/com/lyh/LoadCenterApplication.java @@ -5,15 +5,23 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; /** + * 负载中心模块启动类 + * * @ProjectName: Default (Template) Project * @Author: LiuYunHu * @CreateTime: 2024/4/1 - * @Description: 负载中心模块启动类 */ @SpringBootApplication @EnableScheduling public class LoadCenterApplication { + /** + * 启动类main方法 + * + * @Date: 2024/4/19 20:23 + * @Param: [args] + * @Return: void + **/ public static void main(String[] args) { SpringApplication.run(LoadCenterApplication.class, args); System.out.println("负载中心模块启动成功"); diff --git a/src/main/java/com/lyh/common/aliyun/config/AliConfig.java b/src/main/java/com/lyh/common/aliyun/config/AliConfig.java index c248dd1..999ecea 100644 --- a/src/main/java/com/lyh/common/aliyun/config/AliConfig.java +++ b/src/main/java/com/lyh/common/aliyun/config/AliConfig.java @@ -10,10 +10,11 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** + * 阿里云客户端配置类 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/16 - * @Description: 阿里云配置 */ @Configuration @ConfigurationProperties(prefix = "config.aliyun") @@ -21,27 +22,26 @@ import org.springframework.context.annotation.Configuration; @AllArgsConstructor @NoArgsConstructor public class AliConfig { - /* + /** * access-key-id - * */ + */ private String accessKeyId; - /* + /** * access-key-secret - * */ + */ private String accessKeySecret; - /* + /** * 地域id - * */ + */ private String regionId; - /* - * @Author: LiuYunHu - * @Date: 2024/4/16 21:29 - * @Description: 初始化客户端实例 - * @Param: [aliConfig] - * @Return: com.aliyun.ecs20140526.Client + /** + * 初始化客户端实例 + * Date: 2024/4/19 20:17 + * Param: [aliConfig] + * Return: com.aliyun.ecs20140526.Client **/ @Bean public Client createEcsClient(AliConfig aliConfig) throws Exception { diff --git a/src/main/java/com/lyh/common/aliyun/model/InstanceSpecification.java b/src/main/java/com/lyh/common/aliyun/model/InstanceSpecification.java index 1656546..2bcc25b 100644 --- a/src/main/java/com/lyh/common/aliyun/model/InstanceSpecification.java +++ b/src/main/java/com/lyh/common/aliyun/model/InstanceSpecification.java @@ -8,10 +8,11 @@ import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; /** + * 实例规格 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/16 - * @Description: 实例规格 */ @Configuration @ConfigurationProperties(prefix = "config.instance") @@ -21,54 +22,54 @@ import org.springframework.stereotype.Component; @Component public class InstanceSpecification { - /* + /** * 镜像 ID,启动实例时选择的镜像资源。 - * */ + */ private String imageId; - /* - *实例规格 - * */ + /** + * 实例规格 + */ private String instanceType; - /* - *新创建实例所属于的安全组 ID。 - * */ + /** + * 新创建实例所属于的安全组 ID。 + */ private String securityGroupId; - /* - *虚拟交换机 ID。 - * */ + /** + * 虚拟交换机 ID。 + */ private String vSwitchId; - /* - *公网出带宽最大值,单位为 Mbit/s。取值范围:0~100。 默认值:0。 - * */ + /** + * 公网出带宽最大值,单位为 Mbit/s。取值范围:0~100。 默认值:0。 + */ private String internetMaxBandwidthOut; - /* - *网络计费类型。取值范围: + /** + * 网络计费类型。取值范围: * PayByBandwidth: 按固定带宽计费。 * PayByTraffic: 按使用流量计费。 * 默认值:PayByTraffic。 - * */ + */ private String internetChargeType; - /* - *系统盘大小 - * */ + /** + * 系统盘大小 + */ private String size; - /* - *系统盘的云盘种类 - * */ + /** + * 系统盘的云盘种类 + */ private String category; - /* - *ECS实例的计费方式 + /** + * ECS实例的计费方式 * PrePaid:包年包月 * PostPaid:按量付费 - * */ + */ private String instanceChargeType; } diff --git a/src/main/java/com/lyh/common/aliyun/service/AliYunEcsService.java b/src/main/java/com/lyh/common/aliyun/service/AliYunEcsService.java index 567d70b..4c9186e 100644 --- a/src/main/java/com/lyh/common/aliyun/service/AliYunEcsService.java +++ b/src/main/java/com/lyh/common/aliyun/service/AliYunEcsService.java @@ -18,112 +18,35 @@ import java.util.Date; import java.util.List; /** + * 阿里云ecs服务器openAPI调用 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/16 - * @Description: 阿里云ecs服务器openAPI调用 */ @Component @Slf4j public class AliYunEcsService { - /* - *实例规格,用于创建实例 - * */ - @Autowired - private InstanceSpecification instanceSpecification; - - //应用阿里云客户端配置 private final AliConfig aliConfig; private final Client client; + /** + * 实例规格,用于创建实例 + */ + @Autowired + private InstanceSpecification instanceSpecification; public AliYunEcsService(AliConfig aliConfig, Client client) { this.aliConfig = aliConfig; this.client = client; } - - /* - * @Author: LiuYunHu - * @Date: 2024/4/16 20:01 - * @Description: 获取指定区域的实例ID列表 - * @Param: [] - * @Return: java.util.List - **/ - public List getIDList() throws Exception { - - java.util.List regionIds = com.aliyun.darabonbastring.Client.split(aliConfig.getRegionId(), ",", 50); - - String regionId = regionIds.get(0); - - DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest() - .setPageSize(100) - .setRegionId(regionId); - DescribeInstancesResponse resp = this.client.describeInstances(describeInstancesRequest); - java.util.List instances = resp.body.instances.instance; - - log.info(regionId + " 下 ECS 实例列表:"); - - //存储结果的List - ArrayList result = new ArrayList<>(); - for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : instances) { -// log.info("主机名:" + instance.hostName + " 实例ID:" + instance.instanceId + " CPU:" + instance.cpu + " 内存:" + instance.memory + " MB 规格:" + instance.instanceType + " 系统:" + instance.OSType + "(" + instance.OSName + ") 状态:" + instance.status); - - result.add(instance.instanceId); - } - - //将查询到的ecs服务器id列表 打印控制台 - log.info(JSON.toJSONString(result)); - - //返回结果 - return result; - } - - - /* - * @Author: LiuYunHu - * @Date: 2024/4/16 20:19 - * @Description: 通过实例ID释放实例 多个实例ID,用英文逗号分隔 - * @Param: instanceIds 要删除实例ID - * @Return: - **/ - public void releaseInstances(String instanceIds) throws Exception { - - // 实例名称,支持使用通配符*进行模糊搜索 - String instanceName = "*"; - // 强制删除有删除保护的机器 - String deleteProtected = "true"; - // 强制删除运行中的机器 - String force = "true"; - - //判断是否为强制删除 - if (Common.equalString(deleteProtected, "true")) { - DescribeInstancesResponse describeInstancesResp = DescribeInstances(client, aliConfig.getRegionId(), instanceIds, instanceName); - instanceIds = ""; - for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : describeInstancesResp.body.instances.instance) { - instanceIds = instance.instanceId + "," + instanceIds; - if (instance.deletionProtection) { - ModifyInstanceAttribute(client, instance.instanceId); - } - - } - instanceIds = com.aliyun.darabonbastring.Client.subString(instanceIds, 0, -1); - } - - //如果入参为空 - if (Common.empty(instanceIds)) { - com.aliyun.teaconsole.Client.log("--------------------无有效实例可删除--------------------"); - return; - } - - DeleteInstances(client, aliConfig.getRegionId(), instanceIds, force); - } - - /* + /** + * 查询要删除的实例 + * * @Author: LiuYunHu * @Date: 2024/4/16 20:31 - * @Description: 查询要删除的实例 * @Param: [client, regionId, instanceIds, instanceName] * @Return: DescribeInstancesResponse **/ @@ -140,10 +63,11 @@ public class AliYunEcsService { return resp; } - /* + /** + * 修改实例的属性 取消 ‘释放保护’ + * * @Author: LiuYunHu * @Date: 2024/4/16 20:33 - * @Description: 修改实例的属性 取消 ‘释放保护’ * @Param: [client, instatnceId] * @Return: void **/ @@ -155,10 +79,11 @@ public class AliYunEcsService { com.aliyun.teaconsole.Client.log("--------------------" + instatnceId + "释放保护取消成功--------------------"); } - /* + /** + * 执行释放实例 + * * @Author: LiuYunHu * @Date: 2024/4/16 20:35 - * @Description: 执行释放实例 * @Param: [client, regionId, instanceIds, force] * @Return: void **/ @@ -172,48 +97,6 @@ public class AliYunEcsService { com.aliyun.teaconsole.Client.log(Common.toJSONString(Common.toMap(resp))); } - - /* - * @Author: LiuYunHu - * @Date: 2024/4/16 21:44 - * @Description: 创建并运行实例 - * @Param: [] - * @Return: java.lang.String - **/ - public String createAndRunInstance() throws Exception { - - //获取实例规格 - // 地域Id - String regionId = aliConfig.getRegionId(); - // 镜像 ID,启动实例时选择的镜像资源。 - String imageId = instanceSpecification.getImageId(); - // 实例规格 - String instanceType = instanceSpecification.getInstanceType(); - // 新创建实例所属于的安全组 ID。 - String securityGroupId = instanceSpecification.getSecurityGroupId(); - // 虚拟交换机 ID。 - String vSwitchId = instanceSpecification.getVSwitchId(); - // 公网出带宽最大值,单位为 Mbit/s。取值范围:0~100。 默认值:0。 - Integer internetMaxBandwidthOut = com.aliyun.darabonbanumber.Client.parseInt(instanceSpecification.getInternetMaxBandwidthOut()); - // 网络计费类型。取值范围: - // PayByBandwidth: 按固定带宽计费。 - // PayByTraffic: 按使用流量计费。 - // 默认值:PayByTraffic。 - String internetChargeType = instanceSpecification.getInternetChargeType(); - // 系统盘大小 - String size = instanceSpecification.getSize(); - // 系统盘的云盘种类 - String category = instanceSpecification.getCategory(); - // ECS实例的计费方式 - // PrePaid:包年包月 - // PostPaid:按量付费 - String instanceChargeType = instanceSpecification.getInstanceChargeType(); - - - // 批量创建实例 - return RunInstances(client, regionId, imageId, instanceType, securityGroupId, vSwitchId, internetMaxBandwidthOut, internetChargeType, size, category, instanceChargeType); - } - /** * RunInstances 通过备选实例规格创建ECS实例最佳实践 * 该场景中,在调用RunInstances创建ECS实例时判断是否发生库存不足等错误,如果发生错误,将调用DescribeRecommendInstanceType查询备选实例,然后通过备选实例规格重新创建ECS实例。 @@ -260,11 +143,129 @@ public class AliYunEcsService { return result; } + /** + * 获取指定区域的实例ID列表 + * + * @Date: 2024/4/19 20:02 + * @Param: [] + * @Return: java.util.List + **/ + public List getIDList() throws Exception { - /* + java.util.List regionIds = com.aliyun.darabonbastring.Client.split(aliConfig.getRegionId(), ",", 50); + + String regionId = regionIds.get(0); + + DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest() + .setPageSize(100) + .setRegionId(regionId); + DescribeInstancesResponse resp = this.client.describeInstances(describeInstancesRequest); + java.util.List instances = resp.body.instances.instance; + + log.info(regionId + " 下 ECS 实例列表:"); + + //存储结果的List + ArrayList result = new ArrayList<>(); + for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : instances) { +// log.info("主机名:" + instance.hostName + " 实例ID:" + instance.instanceId + " CPU:" + instance.cpu + " 内存:" + instance.memory + " MB 规格:" + instance.instanceType + " 系统:" + instance.OSType + "(" + instance.OSName + ") 状态:" + instance.status); + + result.add(instance.instanceId); + } + + //将查询到的ecs服务器id列表 打印控制台 + log.info(JSON.toJSONString(result)); + + //返回结果 + return result; + } + + /** + * 通过实例ID释放实例 多个实例ID,用英文逗号分隔 + * + * @Author: LiuYunHu + * @Date: 2024/4/16 20:19 + * @Description: + * @Param: instanceIds 要删除实例ID + **/ + public void releaseInstances(String instanceIds) throws Exception { + + // 实例名称,支持使用通配符*进行模糊搜索 + String instanceName = "*"; + // 强制删除有删除保护的机器 + String deleteProtected = "true"; + // 强制删除运行中的机器 + String force = "true"; + + //判断是否为强制删除 + if (Common.equalString(deleteProtected, "true")) { + DescribeInstancesResponse describeInstancesResp = DescribeInstances(client, aliConfig.getRegionId(), instanceIds, instanceName); + instanceIds = ""; + for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : describeInstancesResp.body.instances.instance) { + instanceIds = instance.instanceId + "," + instanceIds; + if (instance.deletionProtection) { + ModifyInstanceAttribute(client, instance.instanceId); + } + + } + instanceIds = com.aliyun.darabonbastring.Client.subString(instanceIds, 0, -1); + } + + //如果入参为空 + if (Common.empty(instanceIds)) { + com.aliyun.teaconsole.Client.log("--------------------无有效实例可删除--------------------"); + return; + } + + DeleteInstances(client, aliConfig.getRegionId(), instanceIds, force); + } + + /** + * 创建并运行实例 + * + * @Author: LiuYunHu + * @Date: 2024/4/16 21:44 + * @Param: [] + * @Return: java.lang.String + **/ + public String createAndRunInstance() throws Exception { + + //获取实例规格 + // 地域Id + String regionId = aliConfig.getRegionId(); + // 镜像 ID,启动实例时选择的镜像资源。 + String imageId = instanceSpecification.getImageId(); + // 实例规格 + String instanceType = instanceSpecification.getInstanceType(); + // 新创建实例所属于的安全组 ID。 + String securityGroupId = instanceSpecification.getSecurityGroupId(); + // 虚拟交换机 ID。 + String vSwitchId = instanceSpecification.getVSwitchId(); + // 公网出带宽最大值,单位为 Mbit/s。取值范围:0~100。 默认值:0。 + Integer internetMaxBandwidthOut = com.aliyun.darabonbanumber.Client.parseInt(instanceSpecification.getInternetMaxBandwidthOut()); + // 网络计费类型。取值范围: + // PayByBandwidth: 按固定带宽计费。 + // PayByTraffic: 按使用流量计费。 + // 默认值:PayByTraffic。 + String internetChargeType = instanceSpecification.getInternetChargeType(); + // 系统盘大小 + String size = instanceSpecification.getSize(); + // 系统盘的云盘种类 + String category = instanceSpecification.getCategory(); + // ECS实例的计费方式 + // PrePaid:包年包月 + // PostPaid:按量付费 + String instanceChargeType = instanceSpecification.getInstanceChargeType(); + + + // 批量创建实例 + return RunInstances(client, regionId, imageId, instanceType, securityGroupId, vSwitchId, internetMaxBandwidthOut, internetChargeType, size, category, instanceChargeType); + } + + /** + * 查询一台或多台实例的信息 多台实例用英文逗号拼接 + * * @Author: LiuYunHu * @Date: 2024/4/16 22:02 - * @Description: 查询一台或多台实例的信息 多台实例用英文逗号拼接 * @Param: [instanceIds] i-uf6chlqotgoc9h173alu **/ public List queryInstancesInformation(String instanceIds) { diff --git a/src/main/java/com/lyh/common/domain/MqttInfo.java b/src/main/java/com/lyh/common/domain/MqttInfo.java index 571b16d..0330bad 100644 --- a/src/main/java/com/lyh/common/domain/MqttInfo.java +++ b/src/main/java/com/lyh/common/domain/MqttInfo.java @@ -5,10 +5,11 @@ import lombok.Data; import lombok.NoArgsConstructor; /** + * mqtt服务器信息 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/13 - * @Description: mqtt服务器信息 */ @Data @AllArgsConstructor diff --git a/src/main/java/com/lyh/common/utils/mqtt/MqttUtil.java b/src/main/java/com/lyh/common/utils/mqtt/MqttUtil.java index 27d9125..eb5861c 100644 --- a/src/main/java/com/lyh/common/utils/mqtt/MqttUtil.java +++ b/src/main/java/com/lyh/common/utils/mqtt/MqttUtil.java @@ -12,20 +12,22 @@ import org.springframework.stereotype.Component; import java.io.IOException; /** + * mqtt的工具类 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/15 - * @Description: mqtt的工具类 */ @Component @Slf4j public class MqttUtil { - /* + /** + * 通过IP获取指定IP的fluxmq的负载量 + * * @Author: LiuYunHu * @Date: 2024/4/15 17:26 - * @Description: 通过IP获取指定IP的fluxmq的负载量 * @Param: [IP] * @Return: int **/ diff --git a/src/main/java/com/lyh/common/utils/user/UserUtil.java b/src/main/java/com/lyh/common/utils/user/UserUtil.java index f6d95d1..a74faea 100644 --- a/src/main/java/com/lyh/common/utils/user/UserUtil.java +++ b/src/main/java/com/lyh/common/utils/user/UserUtil.java @@ -8,20 +8,22 @@ import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; /** + * 自定义工具类 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/13 - * @Description: 自定义工具类 */ @Component public class UserUtil { @Autowired private RedisService redisService; - /* + /** + * 将字符串前后的[]进行删除 + * * @Author: LiuYunHu * @Date: 2024/4/13 9:25 - * @Description: 将字符串前后的[]进行删除 * @Param: [input] * @Return: java.lang.String **/ @@ -32,10 +34,11 @@ public class UserUtil { } - /* + /** + * 获取实例注册token令牌 + * * @Author: LiuYunHu * @Date: 2024/4/13 11:42 - * @Description: 获取实例注册token令牌 * @Param: * @Return: **/ diff --git a/src/main/java/com/lyh/controller/LoadCenterController.java b/src/main/java/com/lyh/controller/LoadCenterController.java index 6fc90b7..fdba686 100644 --- a/src/main/java/com/lyh/controller/LoadCenterController.java +++ b/src/main/java/com/lyh/controller/LoadCenterController.java @@ -8,10 +8,11 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** + * 负载中心控制层 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/15 - * @Description: 负载中心控制层 */ @RestController @@ -21,10 +22,10 @@ public class LoadCenterController { private LoadCenterService loadCenterService; - /* + /** + * 获取指定的服务器IP * @Author: LiuYunHu * @Date: 2024/4/15 14:57 - * @Description: 获取指定的服务器IP * @Param: [] * @Return: com.lyh.common.domain.resp.Result **/ diff --git a/src/main/java/com/lyh/gateway/cache/GatewayIpAndLoadCountCache.java b/src/main/java/com/lyh/gateway/cache/GatewayIpAndLoadCountCache.java index 9bebfb2..085962d 100644 --- a/src/main/java/com/lyh/gateway/cache/GatewayIpAndLoadCountCache.java +++ b/src/main/java/com/lyh/gateway/cache/GatewayIpAndLoadCountCache.java @@ -8,10 +8,11 @@ import org.springframework.stereotype.Component; import java.util.List; /** + * 缓存实例IP和负载量 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/18 - * @Description: 缓存实例IP和负载量 */ @Component public class GatewayIpAndLoadCountCache extends GatewayCacheAbs { @@ -22,8 +23,9 @@ public class GatewayIpAndLoadCountCache extends GatewayCacheAbs { return "gateway:load:"; } - /* - * @Description: 增加缓存数据 + /** + * 增加缓存数据 + * * @Date: 2024/4/18 21:37 * @Param: [gatewayNodeIps] * @Return: void @@ -33,8 +35,9 @@ public class GatewayIpAndLoadCountCache extends GatewayCacheAbs { redisService.setCacheList(encode(gatewayIpAndLoadCountKey), ipAndLoadCounts); } - /* - * @Description: 获取缓存数据 + /** + * 获取缓存数据 + * * @Date: 2024/4/18 21:37 * @Param: [] * @Return: java.util.List @@ -44,8 +47,9 @@ public class GatewayIpAndLoadCountCache extends GatewayCacheAbs { return JSON.parseArray(JSON.toJSONString(cacheList), IpAndLoadCount.class); } - /* - * @Description: 删除缓存数据 + /** + * 删除缓存数据 + * * @Date: 2024/4/18 21:37 * @Param: [] * @Return: void diff --git a/src/main/java/com/lyh/gateway/cache/GatewayIpAndLoadWeightCache.java b/src/main/java/com/lyh/gateway/cache/GatewayIpAndLoadWeightCache.java index 6160fc7..f2f7ca6 100644 --- a/src/main/java/com/lyh/gateway/cache/GatewayIpAndLoadWeightCache.java +++ b/src/main/java/com/lyh/gateway/cache/GatewayIpAndLoadWeightCache.java @@ -8,10 +8,11 @@ import org.springframework.stereotype.Component; import java.util.List; /** + * 缓存IP和权重 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/18 - * @Description: 缓存IP和权重 */ @Component public class GatewayIpAndLoadWeightCache extends GatewayCacheAbs { @@ -22,8 +23,9 @@ public class GatewayIpAndLoadWeightCache extends GatewayCacheAbs { return "gateway:load:"; } - /* - * @Description: 增加缓存数据 + /** + * 增加缓存数据 + * * @Date: 2024/4/18 21:37 * @Param: [gatewayNodeIps] * @Return: void @@ -33,8 +35,9 @@ public class GatewayIpAndLoadWeightCache extends GatewayCacheAbs { redisService.setCacheList(encode(gatewayIpAndWeightKey), ipAndWeights); } - /* - * @Description: 获取缓存数据 + /** + * 获取缓存数据 + * * @Date: 2024/4/18 21:37 * @Param: [] * @Return: java.util.List @@ -44,8 +47,9 @@ public class GatewayIpAndLoadWeightCache extends GatewayCacheAbs { return JSON.parseArray(JSON.toJSONString(cacheList), IpAndWeight.class); } - /* - * @Description: 删除缓存数据 + /** + * 删除缓存数据 + * * @Date: 2024/4/18 21:37 * @Param: [] * @Return: void diff --git a/src/main/java/com/lyh/gateway/cache/GatewayNodeIdCache.java b/src/main/java/com/lyh/gateway/cache/GatewayNodeIdCache.java index 7e6f397..081f0cb 100644 --- a/src/main/java/com/lyh/gateway/cache/GatewayNodeIdCache.java +++ b/src/main/java/com/lyh/gateway/cache/GatewayNodeIdCache.java @@ -6,10 +6,11 @@ import org.springframework.stereotype.Component; import java.util.List; /** + * 节点id缓存 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/19 - * @Description: 节点id缓存 */ @Component public class GatewayNodeIdCache extends GatewayCacheAbs { @@ -20,8 +21,9 @@ public class GatewayNodeIdCache extends GatewayCacheAbs { return "gateway:load:"; } - /* - * @Description: 增加缓存数据 + /** + * 增加缓存数据 + * * @Date: 2024/4/18 21:37 * @Param: [gatewayNodeIps] * @Return: void @@ -31,8 +33,9 @@ public class GatewayNodeIdCache extends GatewayCacheAbs { redisService.setCacheList(encode(gatewayLoadNodeIdKey), gatewayNodeIds); } - /* - * @Description: 获取缓存数据 + /** + * 获取缓存数据 + * * @Date: 2024/4/18 21:37 * @Param: [] * @Return: java.util.List @@ -41,8 +44,9 @@ public class GatewayNodeIdCache extends GatewayCacheAbs { return redisService.getCacheList(encode(gatewayLoadNodeIdKey)); } - /* - * @Description: 删除缓存数据 + /** + * 删除缓存数据 + * * @Date: 2024/4/18 21:37 * @Param: [] * @Return: void diff --git a/src/main/java/com/lyh/gateway/cache/GatewayNodeInfoCache.java b/src/main/java/com/lyh/gateway/cache/GatewayNodeInfoCache.java index 18f305d..370a53e 100644 --- a/src/main/java/com/lyh/gateway/cache/GatewayNodeInfoCache.java +++ b/src/main/java/com/lyh/gateway/cache/GatewayNodeInfoCache.java @@ -8,10 +8,11 @@ import org.springframework.stereotype.Component; import java.util.List; /** + * 网关节点缓存 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/18 - * @Description: 网关节点缓存 */ @Component @@ -24,10 +25,11 @@ public class GatewayNodeInfoCache extends GatewayCacheAbs { return "gateway:load:"; } - /* + /** + * 增加缓存数据 + * * @Author: LiuYunHu * @Date: 2024/4/18 15:16 - * @Description: 增加缓存数据 * @Param: 节点信息 * @Return: void **/ @@ -36,10 +38,11 @@ public class GatewayNodeInfoCache extends GatewayCacheAbs { redisService.setCacheList(encode(gatewayLoadInfoKey), gatewayNodeInfos); } - /* + /** + * 获取缓存数据 + * * @Author: LiuYunHu * @Date: 2024/4/18 15:19 - * @Description: 获取缓存数据 * @Param: 节点id * @Return: 节点信息 **/ @@ -49,10 +52,11 @@ public class GatewayNodeInfoCache extends GatewayCacheAbs { return JSON.parseArray(JSON.toJSONString(cacheList), GatewayNodeInfo.class); } - /* + /** + * 删除缓存数据 + * * @Author: LiuYunHu * @Date: 2024/4/18 15:19 - * @Description: 删除缓存数据 * @Param: 节点id * @Return: void **/ diff --git a/src/main/java/com/lyh/gateway/cache/GatewayNodeIpCache.java b/src/main/java/com/lyh/gateway/cache/GatewayNodeIpCache.java index ff90080..c8ddb15 100644 --- a/src/main/java/com/lyh/gateway/cache/GatewayNodeIpCache.java +++ b/src/main/java/com/lyh/gateway/cache/GatewayNodeIpCache.java @@ -6,10 +6,11 @@ import org.springframework.stereotype.Component; import java.util.List; /** + * 负载节点IP缓存 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/18 - * @Description: 负载节点IP缓存 */ @Component public class GatewayNodeIpCache extends GatewayCacheAbs { @@ -20,8 +21,9 @@ public class GatewayNodeIpCache extends GatewayCacheAbs { return "gateway:load:"; } - /* - * @Description: 增加缓存数据 + /** + * 增加缓存数据 + * * @Date: 2024/4/18 21:37 * @Param: [gatewayNodeIps] * @Return: void @@ -31,8 +33,9 @@ public class GatewayNodeIpCache extends GatewayCacheAbs { redisService.setCacheList(encode(gatewayLoadNodeIpKey), gatewayNodeIps); } - /* - * @Description: 获取缓存数据 + /** + * 获取缓存数据 + * * @Date: 2024/4/18 21:37 * @Param: [] * @Return: java.util.List @@ -41,8 +44,9 @@ public class GatewayNodeIpCache extends GatewayCacheAbs { return redisService.getCacheList(encode(gatewayLoadNodeIpKey)); } - /* - * @Description: 删除缓存数据 + /** + * 删除缓存数据 + * * @Date: 2024/4/18 21:37 * @Param: [] * @Return: void diff --git a/src/main/java/com/lyh/gateway/cache/GatewayNodeOrderCache.java b/src/main/java/com/lyh/gateway/cache/GatewayNodeOrderCache.java index 7300ac3..ebf9960 100644 --- a/src/main/java/com/lyh/gateway/cache/GatewayNodeOrderCache.java +++ b/src/main/java/com/lyh/gateway/cache/GatewayNodeOrderCache.java @@ -6,10 +6,11 @@ import org.springframework.stereotype.Component; import java.util.List; /** + * 节点IP序列缓存 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/18 - * @Description: 节点IP序列缓存 */ @Component public class GatewayNodeOrderCache extends GatewayCacheAbs { @@ -20,8 +21,9 @@ public class GatewayNodeOrderCache extends GatewayCacheAbs { return "gateway:load:"; } - /* - * @Description: 增加缓存数据 + /** + * 增加缓存数据 + * * @Date: 2024/4/18 21:37 * @Param: [gatewayNodeIps] * @Return: void @@ -32,8 +34,9 @@ public class GatewayNodeOrderCache extends GatewayCacheAbs { } - /* - * @Description: 获取全部缓存数据 + /** + * 获取全部缓存数据 + * * @Date: 2024/4/18 21:37 * @Param: [] * @Return: java.util.List @@ -42,8 +45,9 @@ public class GatewayNodeOrderCache extends GatewayCacheAbs { return redisService.getCacheList(encode(gatewayNodeOrderKey)); } - /* - * @Description: 删除缓存数据 + /** + * 删除缓存数据 + * * @Date: 2024/4/18 21:37 * @Param: [] * @Return: void diff --git a/src/main/java/com/lyh/gateway/cache/abs/GatewayCacheAbs.java b/src/main/java/com/lyh/gateway/cache/abs/GatewayCacheAbs.java index 1777a30..f610d07 100644 --- a/src/main/java/com/lyh/gateway/cache/abs/GatewayCacheAbs.java +++ b/src/main/java/com/lyh/gateway/cache/abs/GatewayCacheAbs.java @@ -4,10 +4,11 @@ import com.lyh.common.redis.service.RedisService; import org.springframework.beans.factory.annotation.Autowired; /** + * 缓存抽象类 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/18 - * @Description: 缓存抽象类 */ public abstract class GatewayCacheAbs { diff --git a/src/main/java/com/lyh/gateway/mode/GatewayNodeInfo.java b/src/main/java/com/lyh/gateway/mode/GatewayNodeInfo.java index 891bc0a..2e83d59 100644 --- a/src/main/java/com/lyh/gateway/mode/GatewayNodeInfo.java +++ b/src/main/java/com/lyh/gateway/mode/GatewayNodeInfo.java @@ -6,10 +6,11 @@ import lombok.Data; import lombok.NoArgsConstructor; /** + * 网关节点信息 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/18 - * @Description: 网关节点信息 */ @Data @NoArgsConstructor @@ -28,43 +29,43 @@ public class GatewayNodeInfo { * */ - /* + /** * 实例的ID - * */ + */ private String instanceId; - /* + /** * 名称 - * */ + */ private String instanceName; - /* + /** * 状态 - * */ + */ private String status; - /* + /** * 公网IP - * */ + */ private String publicIpAddress; - /* + /** * 私网IP - * */ + */ private String privateIpAddress; - /* + /** * 创建时间 - * */ + */ private String creationTime; - /* + /** * 到期时间 - * */ + */ private String expiredTime; - /* + /** * 是否可以回收 - * */ + */ private Boolean recyclable; } diff --git a/src/main/java/com/lyh/gateway/mode/IpAndLoadCount.java b/src/main/java/com/lyh/gateway/mode/IpAndLoadCount.java index 91ce0da..a6ab326 100644 --- a/src/main/java/com/lyh/gateway/mode/IpAndLoadCount.java +++ b/src/main/java/com/lyh/gateway/mode/IpAndLoadCount.java @@ -5,16 +5,23 @@ import lombok.Data; import lombok.NoArgsConstructor; /** + * IP和对应IP的负载量 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/15 - * @Description: IP和对应IP的负载量 */ @Data @AllArgsConstructor @NoArgsConstructor public class IpAndLoadCount { + /** + * IP + */ private String ip; + /** + * 负载量 + */ private Integer loadCount; } diff --git a/src/main/java/com/lyh/gateway/mode/IpAndWeight.java b/src/main/java/com/lyh/gateway/mode/IpAndWeight.java index 56acb14..a390d14 100644 --- a/src/main/java/com/lyh/gateway/mode/IpAndWeight.java +++ b/src/main/java/com/lyh/gateway/mode/IpAndWeight.java @@ -5,16 +5,23 @@ import lombok.Data; import lombok.NoArgsConstructor; /** + * 节点IP和对应的权重 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/15 - * @Description: //节点IP和对应的权重 */ @Data @AllArgsConstructor @NoArgsConstructor public class IpAndWeight { + /** + * 节点IP + */ private String nodeIp; + /** + * 权重值 + */ private Integer weight; } diff --git a/src/main/java/com/lyh/handle/HandleCache.java b/src/main/java/com/lyh/handle/HandleCache.java index 4f22380..63e49f4 100644 --- a/src/main/java/com/lyh/handle/HandleCache.java +++ b/src/main/java/com/lyh/handle/HandleCache.java @@ -18,10 +18,11 @@ import java.util.ArrayList; import java.util.List; /** + * 操作缓存类 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/19 - * @Description: 操作缓存类 */ @Component @Slf4j @@ -32,43 +33,48 @@ public class HandleCache { @Autowired private AliYunEcsService aliYunEcsService; - /* + /** * 操作网关节点缓存 - * */ + */ @Autowired private GatewayNodeInfoCache gatewayNodeInfoCache; - /* + /** * 操作负载节点IP缓存 - * */ + */ @Autowired private GatewayNodeIpCache gatewayNodeIpCache; - /* + /** * 操作负载节点ID缓存 - * */ + */ @Autowired private GatewayNodeIdCache gatewayNodeIdCache; - /* + /** * 操作实例IP和负载量缓存 - * */ + */ @Autowired private GatewayIpAndLoadCountCache gatewayIpAndLoadCountCache; - /* + /** * 操作实例IP和权重缓存 - * */ + */ @Autowired private GatewayIpAndLoadWeightCache gatewayIpAndLoadWeightCache; - /* + /** * 操作实例IP序列缓存 - * */ + */ @Autowired private GatewayNodeOrderCache gatewayNodeOrderCache; - //刷新所有的缓存 + /** + * 刷新所有的缓存 + * Date: 2024/4/19 20:19 + * Param: [] + * Return: void + **/ @Scheduled(cron = "0/2 * * * * ?") public void refreshAllCache() { this.getNodeIDList(); @@ -80,8 +86,9 @@ public class HandleCache { } - /* - * @Description: 获取节点id列表 + /** + * 获取节点id列表 + * * @Date: 2024/4/19 9:09 * @Param: [] * @Return: void @@ -102,8 +109,9 @@ public class HandleCache { } } - /* - * @Description: 获取实例信息列表 + /** + * 获取实例信息列表 + * * @Date: 2024/4/18 21:18 * @Param: [] * @Return: void @@ -185,10 +193,11 @@ public class HandleCache { } - /* + /** + * 获取所有实例公网的IP列表 + * * @Author: LiuYunHu * @Date: 2024/4/17 17:14 - * @Description: 获取所有实例公网的IP列表 * @Param: [] * @Return: List **/ @@ -220,10 +229,11 @@ public class HandleCache { } - /* + /** + * 通过IP获取各个IP的负载量 + * * @Author: LiuYunHu * @Date: 2024/4/17 19:41 - * @Description: 通过IP获取各个IP的负载量 * @Param: ecsIPList * @Return: List **/ @@ -239,9 +249,9 @@ public class HandleCache { ArrayList ipAndLoadCounts = new ArrayList<>(); //拿到IP后,获取各个IP的负载量 - nodeIPList.forEach(ip -> { - ipAndLoadCounts.add(mqttUtil.getFetchLoad(ip)); - }); + nodeIPList.forEach( + ip -> ipAndLoadCounts.add(mqttUtil.getFetchLoad(ip)) + ); log.info("各个IP的负载量:{}", ipAndLoadCounts); @@ -249,10 +259,11 @@ public class HandleCache { } - /* + /** + * 通过IP和对应的负载量,计算出IP对应的权重 + * * @Author: LiuYunHu * @Date: 2024/4/17 19:49 - * @Description: 通过IP和对应的负载量,计算出IP对应的权重 * @Param: ipAndLoadCountList * @Return: List **/ @@ -268,7 +279,7 @@ public class HandleCache { //求出空负载的总量 int emptyLoadCount = 0; for (IpAndLoadCount ipAndLoadCount : ipAndLoadCounts) { - //假设使用2/8原则 一个节点最多能有100个连接 + //假设使用2/8 原则 一个节点最多能有100个连接 emptyLoadCount += (80 - ipAndLoadCount.getLoadCount()); } @@ -289,10 +300,11 @@ public class HandleCache { gatewayIpAndLoadWeightCache.put(ipAndWeights); } - /* + /** + * 通过IP和权重,计算节点IP序列 + * * @Author: LiuYunHu * @Date: 2024/4/17 20:02 - * @Description: 通过IP和权重,计算节点IP序列 * @Param: [] * @Return: **/ diff --git a/src/main/java/com/lyh/job/Timer.java b/src/main/java/com/lyh/job/Timer.java index 458de5a..df6ef6a 100644 --- a/src/main/java/com/lyh/job/Timer.java +++ b/src/main/java/com/lyh/job/Timer.java @@ -13,23 +13,24 @@ import org.springframework.stereotype.Component; import java.util.List; /** + * 定时任务类 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/13 - * @Description: */ @Component @Slf4j public class Timer { - /* + /** * 阿里云api接口类 - * */ + */ private final AliYunEcsService aliYunEcsService; - /* + /** * 操作缓存 - * */ + */ private final GatewayIpAndLoadCountCache gatewayIpAndLoadCountCache; private final GatewayNodeIdCache gatewayNodeIdCache; @@ -44,13 +45,14 @@ public class Timer { } - @Scheduled(cron = "0/10 * * * * ?") - /* - * @Description: 动态扩容 + /** + * 动态扩容 + * * @Date: 2024/4/19 9:44 * @Param: [] * @Return: void **/ + @Scheduled(cron = "0/10 * * * * ?") public void dynamicExpansion() { //先获取所有的负载列表 List ipAndLoadCounts = gatewayIpAndLoadCountCache.get(); @@ -85,13 +87,14 @@ public class Timer { } - @Scheduled(cron = "0/10 * * * * ?") - /* TODO 缩容有问题 - * @Description: 动态缩容 + /** + * 动态缩容 + * * @Date: 2024/4/19 9:44 * @Param: [] * @Return: void **/ + @Scheduled(cron = "0/10 * * * * ?") public void dynamicReduction() { //求出所有的节点ID List nodeIds = gatewayNodeIdCache.get(); diff --git a/src/main/java/com/lyh/service/LoadCenterService.java b/src/main/java/com/lyh/service/LoadCenterService.java index 1baa94b..3160741 100644 --- a/src/main/java/com/lyh/service/LoadCenterService.java +++ b/src/main/java/com/lyh/service/LoadCenterService.java @@ -3,10 +3,11 @@ package com.lyh.service; import com.lyh.common.domain.resp.Result; /** + * 业务接口 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/15 - * @Description: */ public interface LoadCenterService { diff --git a/src/main/java/com/lyh/service/impl/LoadCenterServiceImpl.java b/src/main/java/com/lyh/service/impl/LoadCenterServiceImpl.java index 18662be..cb66dc9 100644 --- a/src/main/java/com/lyh/service/impl/LoadCenterServiceImpl.java +++ b/src/main/java/com/lyh/service/impl/LoadCenterServiceImpl.java @@ -11,31 +11,33 @@ import org.springframework.stereotype.Service; import java.util.List; /** + * 负载中心业务实现类 + * * @ProjectName: LoadCenter * @Author: LiuYunHu * @CreateTime: 2024/4/15 - * @Description: */ @Service @Slf4j public class LoadCenterServiceImpl implements LoadCenterService { - /* + /** * 操作缓存 - * */ + */ @Autowired private HandleCache handleCache; - /* + /** * 操作实例IP序列缓存 - * */ + */ @Autowired private GatewayNodeOrderCache gatewayNodeOrderCache; - /* + /** + * 获取 分配的一个服务器IP + * * @Author: LiuYunHu * @Date: 2024/4/15 21:49 - * @Description: 获取 分配的一个服务器IP * @Param: [] * @Return: com.lyh.common.domain.resp.Result **/ @@ -52,8 +54,6 @@ public class LoadCenterServiceImpl implements LoadCenterService { return Result.success(result); } - - } diff --git a/src/test/java/Test.java b/src/test/java/Test.java index 8dc7875..5ca2811 100644 --- a/src/test/java/Test.java +++ b/src/test/java/Test.java @@ -72,7 +72,7 @@ public class Test { **/ @org.junit.jupiter.api.Test public void releaseInstances() throws Exception { - aliYunEcsService.releaseInstances("i-uf6h4s0jtpvrtgx574s1,"); + aliYunEcsService.releaseInstances("i-uf6dsbjd5l364ey5wnkd"); } /*