|
@@ -0,0 +1,91 @@
|
|
|
|
+package com.fdkankan.mqcontroller.utils;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.aliyun.ess20220222.Client;
|
|
|
|
+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;
|
|
|
|
+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
|
|
|
+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
|
|
|
+import software.amazon.awssdk.http.SdkHttpResponse;
|
|
|
|
+import software.amazon.awssdk.regions.Region;
|
|
|
|
+import software.amazon.awssdk.services.autoscaling.AutoScalingClient;
|
|
|
|
+import software.amazon.awssdk.services.autoscaling.model.*;
|
|
|
|
+
|
|
|
|
+import java.util.Arrays;
|
|
|
|
+import java.util.UUID;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+@Slf4j
|
|
|
|
+public class AwsEcsUtils {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static Boolean setCapacity(String autoScalingGroupName,String accesskey,String secretkey,Integer desiredCapacity) {
|
|
|
|
+
|
|
|
|
+ AwsBasicCredentials awsCreds = AwsBasicCredentials.create(accesskey, secretkey);
|
|
|
|
+
|
|
|
|
+ AutoScalingClient autoScalingClient = AutoScalingClient.builder()
|
|
|
|
+ .region(Region.EU_CENTRAL_1)
|
|
|
|
+ .credentialsProvider(StaticCredentialsProvider.create(awsCreds))
|
|
|
|
+ .build();
|
|
|
|
+ autoScalingClient.describeAutoScalingGroups();
|
|
|
|
+ try {
|
|
|
|
+ SetDesiredCapacityRequest request = SetDesiredCapacityRequest.builder()
|
|
|
|
+ .autoScalingGroupName(autoScalingGroupName)
|
|
|
|
+ .desiredCapacity(desiredCapacity)
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ SetDesiredCapacityResponse setDesiredCapacityResponse = autoScalingClient.setDesiredCapacity(request);
|
|
|
|
+ SdkHttpResponse sdkHttpResponse = setDesiredCapacityResponse.sdkHttpResponse();
|
|
|
|
+ //log.info("resp:{}",sdkHttpResponse);
|
|
|
|
+ return sdkHttpResponse.isSuccessful();
|
|
|
|
+ } catch (AutoScalingException e) {
|
|
|
|
+ log.info(e.awsErrorDetails().errorMessage());
|
|
|
|
+ return false;
|
|
|
|
+ } finally {
|
|
|
|
+ autoScalingClient.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static Boolean stopEcs(String accesskey,String secretkey,String instanceId){
|
|
|
|
+ boolean shouldDecrementDesiredCapacity = false;
|
|
|
|
+ AwsBasicCredentials awsCreds = AwsBasicCredentials.create(accesskey, secretkey);
|
|
|
|
+ AutoScalingClient autoScalingClient = AutoScalingClient.builder()
|
|
|
|
+ .region(Region.EU_CENTRAL_1)
|
|
|
|
+ .credentialsProvider(StaticCredentialsProvider.create(awsCreds))
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ TerminateInstanceInAutoScalingGroupRequest request = TerminateInstanceInAutoScalingGroupRequest.builder()
|
|
|
|
+ .instanceId(instanceId)
|
|
|
|
+ .shouldDecrementDesiredCapacity(shouldDecrementDesiredCapacity)
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ TerminateInstanceInAutoScalingGroupResponse terminateInstanceInAutoScalingGroupResponse = autoScalingClient.terminateInstanceInAutoScalingGroup(request);
|
|
|
|
+ log.info("resp:{}",terminateInstanceInAutoScalingGroupResponse);
|
|
|
|
+ } catch (AutoScalingException e) {
|
|
|
|
+ log.info(e.awsErrorDetails().errorMessage());
|
|
|
|
+ return false;
|
|
|
|
+ } finally {
|
|
|
|
+ autoScalingClient.close();
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ String APPKEY = "AKIAWCV5QFZ3WTRVTJW7";
|
|
|
|
+ String APPSECRET = "bxt1vcjoLb5KuFw4y5UWFB7Z0RxKu12L7Ur+Vrbl";
|
|
|
|
+ String AUTOSCALINGGROUPNAME = "test";
|
|
|
|
+
|
|
|
|
+ //stopEcs(APPKEY,APPSECRET,"i-01849da64bd3919d6");
|
|
|
|
+ System.out.println( setCapacity(AUTOSCALINGGROUPNAME,APPKEY,APPSECRET,0));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|