lyhzzz пре 1 година
родитељ
комит
cf6203159a

+ 0 - 34
src/main/java/com/fdkankan/mqcontroller/task/TaskController.java

@@ -27,42 +27,8 @@ public class TaskController {
     }
 
 
-    /**
-     * 判断是否关闭弹性伸缩
-     */
-    @Scheduled(initialDelay = 3000,fixedDelay = 1000 * 70 )
-    public void checkDelEcs(){
-        try {
-            taskService.checkDelEcs();
-        }catch (Exception e){
-            log.info("checkDelEcs执行失败:",e);
-        }
-    }
 
 
-    /**
-     * 开启缓存弹性伸缩
-     */
-    @Scheduled(initialDelay = 4000,fixedDelay = 1000 * 41 )
-    public void openEcsList(){
-        try {
-            taskService.openEcsList();
-        }catch (Exception e){
-            log.info("openEcsList执行失败:",e);
-        }
-    }
-
-    /**
-     * 关闭缓存弹性伸缩
-     */
-    @Scheduled(initialDelay = 5000,fixedDelay = 1000 * 16 )
-    public void delEcsList(){
-        try {
-            taskService.delEcsList();
-        }catch (Exception e){
-            log.info("delEcsList执行失败:",e);
-        }
-    }
 
 
 

+ 15 - 0
src/main/java/com/fdkankan/mqcontroller/task/TaskService.java

@@ -122,6 +122,10 @@ public class TaskService {
             sendRabbitMq(msgList,mqMsg.getConsumers() - mqMsg.getMessages_unacknowledged() - mqMsg.getMessages_ready(),mqQueueConfig.getQueueName());
         }
 
+        checkDelEcs();
+        openEcsList();
+        delEcsList();
+
     }
 
     /**
@@ -211,6 +215,9 @@ public class TaskService {
         }
     }
 
+    /**
+     * 开启弹性伸缩
+     */
     public void openEcsList() {
         try {
             HashMap<String, MqQueueConfig> queueMap = queueConfigService.getQueueMap();
@@ -247,6 +254,9 @@ public class TaskService {
         }
     }
 
+    /**
+     * 判断是否关闭弹性伸缩
+     */
     public void checkDelEcs() {
         List<MqEcs> mqEcsList = mqEcsService.getScalingNotStopList();
         if(mqEcsList.isEmpty()){
@@ -277,6 +287,9 @@ public class TaskService {
 
 
 
+    /**
+     * 关闭弹性伸缩
+     */
     public void delEcsList() {
         try {
             if(delList.isEmpty()){
@@ -300,6 +313,8 @@ public class TaskService {
             if(delFlag){
                 log.info("checkDelEcs--关闭弹性伸缩实例成功:{}", take.getMqEcs().getEcsName());
                 mqEcsService.updateMqEcs(take.getMqEcs());
+            }else {
+                delList.offer(take);
             }
         }catch (Exception e){
             log.info("delEcsList--关闭弹性伸缩失败:",e);

+ 19 - 8
src/main/java/com/fdkankan/mqcontroller/utils/ECSUtils.java

@@ -7,6 +7,7 @@ import com.aliyun.ess20220222.models.ExecuteScalingRuleRequest;
 import com.aliyun.ess20220222.models.ExecuteScalingRuleResponse;
 import com.aliyun.ess20220222.models.RemoveInstancesRequest;
 import com.aliyun.ess20220222.models.RemoveInstancesResponse;
+import com.aliyun.tea.TeaException;
 import com.aliyun.teaopenapi.models.Config;
 import lombok.extern.slf4j.Slf4j;
 
@@ -36,15 +37,25 @@ public class ECSUtils {
         }
         return false;
     }
-    public static Boolean executeScalingStop(Client client, String groupId,String instanceId)  throws Exception{
-        RemoveInstancesRequest removeInstancesRequest = new RemoveInstancesRequest();
-        removeInstancesRequest.setScalingGroupId(groupId);
-        removeInstancesRequest.setInstanceIds(Arrays.asList(instanceId));
-        RemoveInstancesResponse  removeInstancesResponse = client.removeInstances(removeInstancesRequest);
-        log.info("executeScalingStop-resp:{}",JSONObject.toJSONString(removeInstancesResponse));
-        if(removeInstancesResponse.getStatusCode() == 200){
-            return true;
+    public static Boolean executeScalingStop(Client client, String groupId,String instanceId) {
+        try {
+            RemoveInstancesRequest removeInstancesRequest = new RemoveInstancesRequest();
+            removeInstancesRequest.setScalingGroupId(groupId);
+            removeInstancesRequest.setInstanceIds(Arrays.asList(instanceId));
+            RemoveInstancesResponse  removeInstancesResponse = client.removeInstances(removeInstancesRequest);
+            log.info("executeScalingStop-resp:{}",JSONObject.toJSONString(removeInstancesResponse));
+            if(removeInstancesResponse.getStatusCode() == 200){
+                return true;
+            }
+        }catch (TeaException e){
+            //实例不存在视作关闭成功
+            if(e.getStatusCode().equals(400) && e.getMessage().contains("The specified instance does not exist")){
+                return true;
+            }
+        }catch (Exception e){
+            log.info("delEcs--error:",e);
         }
+
         return false;
     }
 

+ 2 - 2
src/main/java/com/fdkankan/mqcontroller/utils/RabbitMqUtils.java

@@ -23,7 +23,7 @@ public class RabbitMqUtils {
      */
     public static MqMsg getRabbitMqMsg(String rabbitMgmtUrl,String vhost, String userName,String password,String queueName) {
         String url = rabbitMgmtUrl + "/api/queues/" + vhost + "/" + queueName;
-        log.info("getRabbitMqMsg-url:{}",url);
+        log.warn("getRabbitMqMsg-url:{}",url);
         // 添加请求头
         // 设置认证信息
         try {
@@ -32,7 +32,7 @@ public class RabbitMqUtils {
                             Base64.encode((userName + ":" + password).getBytes(CharsetUtil.UTF_8)));
             // 发送请求,并获取响应字符串
             String response = request.execute().body();
-            log.info("getRabbitMqMsg-resp:{}",response);
+            log.warn("getRabbitMqMsg-resp:{}",response);
             JSONObject jsonObject = JSONObject.parseObject(response);
             return JSONObject.toJavaObject(jsonObject,MqMsg.class);
         }catch (Exception e){