From 77e9045d6f98e6382d4f697e86e7b7692e69774a Mon Sep 17 00:00:00 2001 From: liuyunhu <3286117488@qq.com> Date: Sat, 20 Apr 2024 09:02:55 +0800 Subject: [PATCH] =?UTF-8?q?16=20=E5=8A=A8=E6=80=81=E6=89=A9=E7=BC=A9?= =?UTF-8?q?=E5=AE=B9=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/lyh/job/Timer.java | 118 ++++++++++++--------------- 1 file changed, 52 insertions(+), 66 deletions(-) diff --git a/src/main/java/com/lyh/job/Timer.java b/src/main/java/com/lyh/job/Timer.java index ab2b161..5984ebe 100644 --- a/src/main/java/com/lyh/job/Timer.java +++ b/src/main/java/com/lyh/job/Timer.java @@ -46,14 +46,14 @@ public class Timer { /** - * 动态扩容 + * 动态扩缩容 * - * @Date: 2024/4/19 9:44 + * @Date: 2024/4/20 8:52 * @Param: [] * @Return: void **/ @Scheduled(cron = "0/10 * * * * ?") - public void dynamicExpansion() { + public void dynamicScaling() { //先获取所有的负载列表 List ipAndLoadCounts = gatewayIpAndLoadCountCache.get(); if (ipAndLoadCounts.isEmpty()) { @@ -64,54 +64,11 @@ public class Timer { //计算所有节点的负载 int connectSize = ipAndLoadCounts.stream().mapToInt(IpAndLoadCount::getLoadCount).sum(); - //求出平均值 - int avg = connectSize / ipAndLoadCounts.size(); + //求出节点数量 + int nodeCount = ipAndLoadCounts.size(); - if (avg >= 80) { - //执行节点扩容 - - //返回实例的ID - String instanceId; - try { - instanceId = aliYunEcsService.createAndRunInstance(); - } catch (Exception e) { - throw new RuntimeException("节点扩容失败!" + e.getMessage()); - } - - if (!instanceId.isEmpty()) { - log.info("扩容 成功!扩容的节点ip为:{}" , instanceId); - } - } else { - log.info("暂时不需要扩容"); - } - } - - - /** - * 动态缩容 - * - * @Date: 2024/4/19 9:44 - * @Param: [] - * @Return: void - **/ - @Scheduled(cron = "0/10 * * * * ?") - public void dynamicReduction() { - //求出所有的节点ID - List nodeIds = gatewayNodeIdCache.get(); - - //如果节点数量小于等于1,则不执行缩容,至少保留一个节点 - if (nodeIds.size() <= 1) { - log.info("暂无节点可删除!"); - return; - } - - - //先获取所有的负载列表 - List ipAndLoadCounts = gatewayIpAndLoadCountCache.get(); - if (ipAndLoadCounts.size() <= 1) { - log.error("负载列表为空!"); - return; - } + //计算平均负载 + int avgLoad = connectSize / nodeCount; //获取节点信息(IP)集合 List gatewayNodeInfoList = gatewayNodeInfoCache.get(); @@ -120,27 +77,56 @@ public class Timer { return; } - //判断哪个节点的负载小于30 + //处理节点扩容或缩容 + boolean scalingPerformed = false; - for (GatewayNodeInfo gatewayNodeInfo : gatewayNodeInfoList) {//获取节点的IP - String ip = gatewayNodeInfo.getPublicIpAddress(); - //获取当前循环节点的负载 - int loadCount = ipAndLoadCounts.stream().filter(ipAndLoadCount -> ipAndLoadCount.getIp().equals(ip)).findFirst().get().getLoadCount(); - //判断节点的负载是否小于30 - if (loadCount < 30) { - //对这个节点进行缩容 - String instanceId = gatewayNodeInfo.getInstanceId(); - try { - aliYunEcsService.releaseInstances(instanceId); - } catch (Exception e) { - throw new RuntimeException("节点缩容失败!" + e.getMessage()); + + if (avgLoad >= 5) { + //执行节点扩容 + try { + String instanceId = aliYunEcsService.createAndRunInstance(); + if (!instanceId.isEmpty()) { + log.info("**********************扩容 成功!**********************扩容的节点ip为:{}", instanceId); + scalingPerformed = true; + } + } catch (Exception e) { + throw new RuntimeException("节点扩容失败!" + e.getMessage()); + } + + + } else { + //检查是否有节点需要缩容 + if (nodeCount > 1) { + for (GatewayNodeInfo gatewayNodeInfo : gatewayNodeInfoList) { + String ip = gatewayNodeInfo.getPublicIpAddress(); + + //获取当前循环IP 的负载量 + Integer loadCount = ipAndLoadCounts.stream() + .filter(ipAndLoadCount -> ipAndLoadCount.getIp().equals(ip)) + .findFirst() + .get() + .getLoadCount(); + //如果负载小于30,则执行缩容 + if (loadCount < 3) { + String instanceId = gatewayNodeInfo.getInstanceId(); + try { + aliYunEcsService.releaseInstances(instanceId); + log.info("**********************节点缩容成功!**********************"); + scalingPerformed = true; + + break;//一次只释放一个节点 + } catch (Exception e) { + throw new RuntimeException("节点缩容失败!" + e.getMessage()); + } + + } } - log.info("节点缩容成功"); - //一次只删除一个节点 - break; } } + if (!scalingPerformed) { + log.info("**********************当前系统负载均衡,无需进行节点扩容或缩容**********************"); + } }