|
@@ -1,6 +1,12 @@
|
|
package com.fdkankan.rubbersheeting;
|
|
package com.fdkankan.rubbersheeting;
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+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.tea.TeaException;
|
|
|
|
+import com.aliyun.teaopenapi.models.Config;
|
|
import com.aliyuncs.DefaultAcsClient;
|
|
import com.aliyuncs.DefaultAcsClient;
|
|
import com.aliyuncs.IAcsClient;
|
|
import com.aliyuncs.IAcsClient;
|
|
import com.aliyuncs.ecs.model.v20140526.DescribeInstancesRequest;
|
|
import com.aliyuncs.ecs.model.v20140526.DescribeInstancesRequest;
|
|
@@ -43,6 +49,9 @@ public class AliYunScalingService implements ScalingService{
|
|
@Value("${scaling.accessKeySecret:#{null}}")
|
|
@Value("${scaling.accessKeySecret:#{null}}")
|
|
private String accessKeySecret;
|
|
private String accessKeySecret;
|
|
|
|
|
|
|
|
+ @Value("${scaling.endPoint:ess.aliyuncs.com}")
|
|
|
|
+ public String endPoint;
|
|
|
|
+
|
|
public static com.aliyun.ess20220222.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
|
|
public static com.aliyun.ess20220222.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
|
|
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
|
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
|
// 必填,您的 AccessKey ID
|
|
// 必填,您的 AccessKey ID
|
|
@@ -182,76 +191,35 @@ public class AliYunScalingService implements ScalingService{
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public String createEcsByConfig(String ruleAri) {
|
|
public String createEcsByConfig(String ruleAri) {
|
|
- StringBuffer parameterBuffer = new StringBuffer();
|
|
|
|
|
|
+ com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
|
|
|
|
+ com.aliyun.teaopenapi.models.Config config = new Config();
|
|
|
|
+ config.setCredential(credentialClient);
|
|
|
|
+ config.setAccessKeyId(accessKeyId);
|
|
|
|
+ config.setAccessKeySecret(accessKeySecret);
|
|
|
|
+ config.setEndpoint(endPoint);
|
|
|
|
+
|
|
|
|
+// IAcsClient client = new DefaultAcsClient(profile);
|
|
|
|
+ com.aliyun.ess20220222.Client client = null;
|
|
try {
|
|
try {
|
|
- boolean tag = true;
|
|
|
|
- Map<String, String> parameters = null;
|
|
|
|
- while (tag){
|
|
|
|
- parameters = new HashMap();
|
|
|
|
- // 加入请求参数
|
|
|
|
- parameters.put("Action", "ExecuteScalingRule");
|
|
|
|
- parameters.put("ScalingRuleAri", ruleAri);
|
|
|
|
- parameters.put("Version", "2014-08-28");
|
|
|
|
- parameters.put("AccessKeyId", accessKeyId);
|
|
|
|
- parameters.put("Timestamp", formatIso8601Date(new Date()));
|
|
|
|
- parameters.put("SignatureMethod", "HMAC-SHA1");
|
|
|
|
- parameters.put("SignatureVersion", "1.0");
|
|
|
|
- parameters.put("SignatureNonce", UUID.randomUUID().toString());
|
|
|
|
- parameters.put("Format", "JSON");
|
|
|
|
-
|
|
|
|
- String signature = sign("AttachInstances", parameters);
|
|
|
|
- if(!signature.contains("+") && !signature.contains("/")){
|
|
|
|
- tag = false;
|
|
|
|
- }
|
|
|
|
- parameters.put("Signature", signature);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (parameters != null) {
|
|
|
|
- Iterator iterator = parameters.keySet().iterator();
|
|
|
|
- String key = null;
|
|
|
|
- String value = null;
|
|
|
|
- while (iterator.hasNext()) {
|
|
|
|
- key = (String) iterator.next();
|
|
|
|
- if (parameters.get(key) != null) {
|
|
|
|
- value = parameters.get(key);
|
|
|
|
- } else {
|
|
|
|
- value = "";
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- parameterBuffer.append(key).append("=").append(value);
|
|
|
|
- if (iterator.hasNext()) {
|
|
|
|
- parameterBuffer.append("&");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return OkHttpUtils.httpGet("http://ess.aliyuncs.com?" + parameterBuffer);
|
|
|
|
- }catch (Exception e){
|
|
|
|
- log.error("启动弹性伸缩异常,url:{}", "http://ess.aliyuncs.com?" + parameterBuffer);
|
|
|
|
|
|
+ client = new com.aliyun.ess20220222.Client(config);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("创建弹性伸缩客户端异常", e);
|
|
}
|
|
}
|
|
|
|
|
|
- // 创建DefaultAcsClient实例并初始化
|
|
|
|
- DefaultProfile profile = DefaultProfile.getProfile(
|
|
|
|
- "<your-region-id>", // 地域ID
|
|
|
|
- "<your-access-key-id>", // RAM账号的AccessKey ID
|
|
|
|
- "<your-access-key-secret>"); // RAM账号Access Key Secret
|
|
|
|
- IAcsClient client = new DefaultAcsClient(profile);
|
|
|
|
// 创建API请求并设置参数
|
|
// 创建API请求并设置参数
|
|
- DescribeInstancesRequest request = new DescribeInstancesRequest();
|
|
|
|
- request.setPageSize(10);
|
|
|
|
|
|
+ ExecuteScalingRuleRequest executeScalingRuleRequest = new ExecuteScalingRuleRequest();
|
|
|
|
+ executeScalingRuleRequest.setScalingRuleAri(ruleAri);
|
|
|
|
+
|
|
// 发起请求并处理应答或异常
|
|
// 发起请求并处理应答或异常
|
|
- DescribeInstancesResponse response;
|
|
|
|
|
|
+ ExecuteScalingRuleResponse executeScalingRuleResponse = null;
|
|
try {
|
|
try {
|
|
- response = client.getAcsResponse(request);
|
|
|
|
- for (DescribeInstancesResponse.Instance instance:response.getInstances()) {
|
|
|
|
- System.out.println(instance.getPublicIpAddress());
|
|
|
|
- }
|
|
|
|
- } catch (ServerException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- } catch (ClientException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
|
+ executeScalingRuleResponse = client.executeScalingRule(executeScalingRuleRequest);
|
|
|
|
+ return JSON.toJSONString(executeScalingRuleResponse);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("触发弹性伸缩规则异常", e);
|
|
}
|
|
}
|
|
|
|
|
|
- return null;
|
|
|
|
|
|
+ return "启动弹性伸缩失败";
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -261,54 +229,36 @@ public class AliYunScalingService implements ScalingService{
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public String deleteEcs(String groupId, String instanceId){
|
|
public String deleteEcs(String groupId, String instanceId){
|
|
- StringBuffer parameterBuffer = new StringBuffer();
|
|
|
|
|
|
+ com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
|
|
|
|
+ com.aliyun.teaopenapi.models.Config config = new Config();
|
|
|
|
+ config.setCredential(credentialClient);
|
|
|
|
+ config.setAccessKeyId(accessKeyId);
|
|
|
|
+ config.setAccessKeySecret(accessKeySecret);
|
|
|
|
+ config.setEndpoint(endPoint);
|
|
|
|
+
|
|
|
|
+// IAcsClient client = new DefaultAcsClient(profile);
|
|
|
|
+ com.aliyun.ess20220222.Client client = null;
|
|
try {
|
|
try {
|
|
- boolean tag = true;
|
|
|
|
- Map<String, String> parameters = null;
|
|
|
|
- while (tag){
|
|
|
|
- parameters = new HashMap();
|
|
|
|
- // 加入请求参数
|
|
|
|
- parameters.put("Action", "RemoveInstances");
|
|
|
|
- parameters.put("InstanceId.1", instanceId);
|
|
|
|
- parameters.put("ScalingGroupId", groupId);
|
|
|
|
- parameters.put("Version", "2014-08-28");
|
|
|
|
- parameters.put("AccessKeyId", accessKeyId);
|
|
|
|
- parameters.put("Timestamp", formatIso8601Date(new Date()));
|
|
|
|
- parameters.put("SignatureMethod", "HMAC-SHA1");
|
|
|
|
- parameters.put("SignatureVersion", "1.0");
|
|
|
|
- parameters.put("SignatureNonce", UUID.randomUUID().toString());
|
|
|
|
- parameters.put("Format", "JSON");
|
|
|
|
-
|
|
|
|
- String signature = sign("AttachInstances", parameters);
|
|
|
|
- if(!signature.contains("+") && !signature.contains("/")){
|
|
|
|
- tag = false;
|
|
|
|
- }
|
|
|
|
- parameters.put("Signature", signature);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (parameters != null) {
|
|
|
|
- Iterator iterator = parameters.keySet().iterator();
|
|
|
|
- String key = null;
|
|
|
|
- String value = null;
|
|
|
|
- while (iterator.hasNext()) {
|
|
|
|
- key = (String) iterator.next();
|
|
|
|
- if (parameters.get(key) != null) {
|
|
|
|
- value = parameters.get(key);
|
|
|
|
- } else {
|
|
|
|
- value = "";
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- parameterBuffer.append(key).append("=").append(value);
|
|
|
|
- if (iterator.hasNext()) {
|
|
|
|
- parameterBuffer.append("&");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return OkHttpUtils.httpGet("http://ess.aliyuncs.com?" + parameterBuffer);
|
|
|
|
- }catch (Exception e){
|
|
|
|
- log.error("启动弹性伸缩异常,url:{}", "http://ess.aliyuncs.com?" + parameterBuffer);
|
|
|
|
|
|
+ client = new com.aliyun.ess20220222.Client(config);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("创建弹性伸缩客户端异常", e);
|
|
}
|
|
}
|
|
- return null;
|
|
|
|
|
|
+
|
|
|
|
+ // 创建API请求并设置参数
|
|
|
|
+ RemoveInstancesRequest removeInstancesRequest = new RemoveInstancesRequest();
|
|
|
|
+ removeInstancesRequest.setScalingGroupId(groupId);
|
|
|
|
+ removeInstancesRequest.setInstanceIds(Arrays.asList(instanceId));
|
|
|
|
+
|
|
|
|
+ // 发起请求并处理应答或异常
|
|
|
|
+ RemoveInstancesResponse removeInstancesResponse = null;
|
|
|
|
+ try {
|
|
|
|
+ removeInstancesResponse = client.removeInstances(removeInstancesRequest);
|
|
|
|
+ return JSON.toJSONString(removeInstancesResponse);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("触发弹性伸缩规则异常", e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return "移除弹性伸缩实例失败";
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|