|
@@ -1,6 +1,15 @@
|
|
|
package com.fdkankan.rubbersheeting;
|
|
|
|
|
|
+import com.aliyun.tea.TeaException;
|
|
|
+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;
|
|
@@ -19,6 +28,7 @@ import java.util.*;
|
|
|
@Slf4j
|
|
|
@Component
|
|
|
@ConditionalOnProperty(name = "scaling.type",havingValue = "oss")
|
|
|
+@Data
|
|
|
public class AliYunScalingService implements ScalingService{
|
|
|
|
|
|
@Value("${scaling.group.id}")
|
|
@@ -33,9 +43,78 @@ public class AliYunScalingService implements ScalingService{
|
|
|
@Value("${scaling.accessKeySecret:#{null}}")
|
|
|
private String accessKeySecret;
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
- Float maxRate = new Float(41.42);
|
|
|
- System.out.println(maxRate > 42);
|
|
|
+ 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{
|
|
@@ -150,6 +229,28 @@ public class AliYunScalingService implements ScalingService{
|
|
|
log.error("启动弹性伸缩异常,url:{}", "http://ess.aliyuncs.com?" + parameterBuffer);
|
|
|
}
|
|
|
|
|
|
+ // 创建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请求并设置参数
|
|
|
+ DescribeInstancesRequest request = new DescribeInstancesRequest();
|
|
|
+ request.setPageSize(10);
|
|
|
+ // 发起请求并处理应答或异常
|
|
|
+ DescribeInstancesResponse response;
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+
|
|
|
return null;
|
|
|
}
|
|
|
|