|
@@ -1,6 +1,21 @@
|
|
|
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.teaopenapi.models.Config;
|
|
|
+import com.aliyuncs.DefaultAcsClient;
|
|
|
+import com.aliyuncs.IAcsClient;
|
|
|
+import com.aliyuncs.ecs.model.v20140526.DescribeInstancesRequest;
|
|
|
+import com.aliyuncs.ecs.model.v20140526.DescribeInstancesResponse;
|
|
|
+import com.aliyuncs.exceptions.ClientException;
|
|
|
+import com.aliyuncs.exceptions.ServerException;
|
|
|
+import com.aliyuncs.profile.DefaultProfile;
|
|
|
import com.fdkankan.common.util.OkHttpUtils;
|
|
|
+import lombok.Data;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
@@ -8,7 +23,6 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.crypto.Mac;
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
-import java.beans.ConstructorProperties;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.text.SimpleDateFormat;
|
|
@@ -20,23 +34,98 @@ import java.util.*;
|
|
|
@Slf4j
|
|
|
@Component
|
|
|
@ConditionalOnProperty(name = "scaling.type",havingValue = "oss")
|
|
|
+@Data
|
|
|
public class AliYunScalingService implements ScalingService{
|
|
|
|
|
|
@Value("${scaling.group.id}")
|
|
|
- private String ScalingGroupId;
|
|
|
+ private String scalingGroupId;
|
|
|
|
|
|
@Value("${scaling.rule.ari}")
|
|
|
- private String ScalingRuleAri;
|
|
|
+ private String scalingRuleAri;
|
|
|
|
|
|
- @Value("${scaling.url}")
|
|
|
- private String url;
|
|
|
-
|
|
|
- @Value("${accessKey.id}")
|
|
|
+ @Value("${scaling.accessKeyId:#{null}}")
|
|
|
private String accessKeyId;
|
|
|
|
|
|
- @Value("${accessKey.secret}")
|
|
|
+ @Value("${scaling.accessKeySecret:#{null}}")
|
|
|
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 {
|
|
|
+ com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
|
|
+ // 必填,您的 AccessKey ID
|
|
|
+ .setAccessKeyId(accessKeyId)
|
|
|
+ // 必填,您的 AccessKey Secret
|
|
|
+ .setAccessKeySecret(accessKeySecret);
|
|
|
+ // Endpoint 请参考 https://api.aliyun.com/product/Ess
|
|
|
+ config.endpoint = "ess.aliyuncs.com";
|
|
|
+ return new com.aliyun.ess20220222.Client(config);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args_) throws Exception {
|
|
|
+
|
|
|
+// AliYunScalingService aliYunScalingService = new AliYunScalingService();
|
|
|
+//// aliYunScalingService.setScalingGroupId("asg-wz918td5gc2qcdx2de7q");
|
|
|
+// aliYunScalingService.setScalingRuleAri("ari:acs:ess:cn-shenzhen:1899912233141089:scalingrule/asr-wz97gv4s2nod1cxznynx");
|
|
|
+// aliYunScalingService.setAccessKeyId("LTAI4GKZQBM1zZZZBJK7nGjR");
|
|
|
+// aliYunScalingService.setAccessKeySecret("bo1ura8KODXASVyZ5fofy0fWFILumz&");
|
|
|
+// final String ecs = aliYunScalingService.createEcs(aliYunScalingService.getScalingRuleAri());
|
|
|
+// System.out.println(ecs);
|
|
|
+
|
|
|
+// // 初始化阿里云客户端
|
|
|
+// DefaultProfile profile = DefaultProfile.getProfile(
|
|
|
+// "oss-cn-shenzhen", // 如 cn-hangzhou
|
|
|
+// "LTAI4GKZQBM1zZZZBJK7nGjR", // AccessKeyId
|
|
|
+// "bo1ura8KODXASVyZ5fofy0fWFILumz&"); // AccessKeySecret
|
|
|
+// IAcsClient client = new DefaultAcsClient(profile);
|
|
|
+//
|
|
|
+// // 指定伸缩组ID
|
|
|
+// String scalingGroupId = "<您的伸缩组ID>";
|
|
|
+//
|
|
|
+// try {
|
|
|
+// // 创建扩容请求
|
|
|
+// ExecuteScalingActionRequest request = new ExecuteScalingActionRequest();
|
|
|
+// request.setScalingGroupId(scalingGroupId);
|
|
|
+// request.setScalingAction("SCALE_OUT"); // 执行扩容动作
|
|
|
+//
|
|
|
+// // 根据伸缩组配置自动创建实例
|
|
|
+// ExecuteScalingActionResponse response = client.getAcsResponse(request);
|
|
|
+//
|
|
|
+// // 输出响应信息(可根据需求处理)
|
|
|
+// System.out.println(response.getRequestId());
|
|
|
+//
|
|
|
+// } catch (ClientException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+
|
|
|
+ java.util.List<String> args = java.util.Arrays.asList(args_);
|
|
|
+ // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
|
|
+ // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html
|
|
|
+ com.aliyun.ess20220222.Client client = AliYunScalingService.createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
|
|
|
+ com.aliyun.ess20220222.models.AttachInstancesRequest tagResourcesRequest = new com.aliyun.ess20220222.models.AttachInstancesRequest()
|
|
|
+ .setRegionId("your_value")
|
|
|
+ .setScalingGroupId("your_value");
|
|
|
+ try {
|
|
|
+ // 复制代码运行请自行打印 API 的返回值
|
|
|
+ client.attachInstances(tagResourcesRequest);
|
|
|
+ } catch (TeaException error) {
|
|
|
+ // 错误 message
|
|
|
+ System.out.println(error.getMessage());
|
|
|
+ // 诊断地址
|
|
|
+ System.out.println(error.getData().get("Recommend"));
|
|
|
+ com.aliyun.teautil.Common.assertAsString(error.message);
|
|
|
+ } catch (Exception _error) {
|
|
|
+ TeaException error = new TeaException(_error.getMessage(), _error);
|
|
|
+ // 错误 message
|
|
|
+ System.out.println(error.getMessage());
|
|
|
+ // 诊断地址
|
|
|
+ System.out.println(error.getData().get("Recommend"));
|
|
|
+ com.aliyun.teautil.Common.assertAsString(error.message);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
private String sign(String action, Map<String, String> parameters) throws Exception{
|
|
|
final String HTTP_METHOD = "GET";
|
|
|
|
|
@@ -86,119 +175,87 @@ public class AliYunScalingService implements ScalingService{
|
|
|
|
|
|
@Override
|
|
|
public String createEcs(){
|
|
|
- Map<String,String> configs = new HashMap<>(2);
|
|
|
- configs.put("ruleAri",ScalingRuleAri);
|
|
|
- return createEcsByConfig(configs);
|
|
|
+ return this.createEcs(scalingRuleAri);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String createEcs(String ruleAri) {
|
|
|
+ return this.createEcsByConfig(ruleAri);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String createEcsByConfig(Map<String, String> configs) {
|
|
|
String ruleAri = configs.get("ruleAri");
|
|
|
+ return this.createEcsByConfig(ruleAri);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String createEcsByConfig(String ruleAri) {
|
|
|
+
|
|
|
+ com.aliyun.teaopenapi.models.Config config = new Config();
|
|
|
+ config.setAccessKeyId(accessKeyId);
|
|
|
+ config.setAccessKeySecret(accessKeySecret);
|
|
|
+ config.setEndpoint(endPoint);
|
|
|
+
|
|
|
+// IAcsClient client = new DefaultAcsClient(profile);
|
|
|
+ com.aliyun.ess20220222.Client client = null;
|
|
|
try {
|
|
|
+ client = new com.aliyun.ess20220222.Client(config);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("创建弹性伸缩客户端异常", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建API请求并设置参数
|
|
|
+ ExecuteScalingRuleRequest executeScalingRuleRequest = new ExecuteScalingRuleRequest();
|
|
|
+ executeScalingRuleRequest.setScalingRuleAri(ruleAri);
|
|
|
|
|
|
- boolean tag = true;
|
|
|
- Map<String, String> parameters = null;
|
|
|
- while (tag){
|
|
|
- parameters = new HashMap<String, String>();
|
|
|
- // 加入请求参数
|
|
|
- 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);
|
|
|
- System.out.println(signature);
|
|
|
- if(!signature.contains("+") && !signature.contains("/")){
|
|
|
- tag = false;
|
|
|
- }
|
|
|
- parameters.put("Signature", signature);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- StringBuffer parameterBuffer = new StringBuffer();
|
|
|
- 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 = (String) parameters.get(key);
|
|
|
- } else {
|
|
|
- value = "";
|
|
|
- }
|
|
|
-
|
|
|
- parameterBuffer.append(key).append("=").append(value);
|
|
|
- if (iterator.hasNext()) {
|
|
|
- parameterBuffer.append("&");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return OkHttpUtils.httpGet(url + "?" + parameterBuffer.toString());
|
|
|
- }catch (Exception e){
|
|
|
- log.error("启动弹性伸缩失败");
|
|
|
+ // 发起请求并处理应答或异常
|
|
|
+ ExecuteScalingRuleResponse executeScalingRuleResponse = null;
|
|
|
+ try {
|
|
|
+ executeScalingRuleResponse = client.executeScalingRule(executeScalingRuleRequest);
|
|
|
+ return JSON.toJSONString(executeScalingRuleResponse);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("触发弹性伸缩规则异常", e);
|
|
|
}
|
|
|
|
|
|
- return null;
|
|
|
+ return "启动弹性伸缩失败";
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String deleteEcs(String id){
|
|
|
+ return this.deleteEcs(scalingGroupId, id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String deleteEcs(String groupId, String instanceId){
|
|
|
+ com.aliyun.teaopenapi.models.Config config = new Config();
|
|
|
+ config.setAccessKeyId(accessKeyId);
|
|
|
+ config.setAccessKeySecret(accessKeySecret);
|
|
|
+ config.setEndpoint(endPoint);
|
|
|
+
|
|
|
+// IAcsClient client = new DefaultAcsClient(profile);
|
|
|
+ com.aliyun.ess20220222.Client client = null;
|
|
|
+ try {
|
|
|
+ client = new com.aliyun.ess20220222.Client(config);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("创建弹性伸缩客户端异常", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建API请求并设置参数
|
|
|
+ RemoveInstancesRequest removeInstancesRequest = new RemoveInstancesRequest();
|
|
|
+ removeInstancesRequest.setScalingGroupId(groupId);
|
|
|
+ removeInstancesRequest.setInstanceIds(Arrays.asList(instanceId));
|
|
|
+
|
|
|
+ // 发起请求并处理应答或异常
|
|
|
+ RemoveInstancesResponse removeInstancesResponse = null;
|
|
|
try {
|
|
|
- boolean tag = true;
|
|
|
- Map<String, String> parameters = null;
|
|
|
- while (tag){
|
|
|
- parameters = new HashMap<String, String>();
|
|
|
- // 加入请求参数
|
|
|
- parameters.put("Action", "RemoveInstances");
|
|
|
- parameters.put("InstanceId.1", id);
|
|
|
- parameters.put("ScalingGroupId", ScalingGroupId);
|
|
|
- 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);
|
|
|
-// System.out.println(signature);
|
|
|
- if(!signature.contains("+") && !signature.contains("/")){
|
|
|
- tag = false;
|
|
|
- }
|
|
|
- parameters.put("Signature", signature);
|
|
|
- }
|
|
|
-
|
|
|
- StringBuffer parameterBuffer = new StringBuffer();
|
|
|
- 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 = (String) parameters.get(key);
|
|
|
- } else {
|
|
|
- value = "";
|
|
|
- }
|
|
|
-
|
|
|
- parameterBuffer.append(key).append("=").append(value);
|
|
|
- if (iterator.hasNext()) {
|
|
|
- parameterBuffer.append("&");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-// System.out.println("POST parameter : " + parameterBuffer.toString());
|
|
|
- return OkHttpUtils.httpGet(url + "?" + parameterBuffer.toString());
|
|
|
- }catch (Exception e){
|
|
|
- e.printStackTrace();
|
|
|
+ removeInstancesResponse = client.removeInstances(removeInstancesRequest);
|
|
|
+ return JSON.toJSONString(removeInstancesResponse);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("触发弹性伸缩规则异常", e);
|
|
|
}
|
|
|
- return null;
|
|
|
+
|
|
|
+ return "移除弹性伸缩实例失败";
|
|
|
}
|
|
|
|
|
|
}
|