feat():查询ESC信息
parent
7d7950e925
commit
ddf4ede829
|
@ -125,6 +125,11 @@
|
||||||
<artifactId>tea-util</artifactId>
|
<artifactId>tea-util</artifactId>
|
||||||
<version>0.2.21</version>
|
<version>0.2.21</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun</groupId>
|
||||||
|
<artifactId>aliyun-java-sdk-ecs</artifactId>
|
||||||
|
<version>4.2.0</version><!-- 请根据实际情况使用最新的版本 -->
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
|
@ -0,0 +1,173 @@
|
||||||
|
package com.muyu.vehicle;
|
||||||
|
|
||||||
|
|
||||||
|
import com.aliyun.ecs20140526.Client;
|
||||||
|
import com.aliyun.ecs20140526.models.*;
|
||||||
|
import com.aliyun.tea.TeaException;
|
||||||
|
import com.aliyun.teaopenapi.models.Config;
|
||||||
|
import com.aliyun.teautil.Common;
|
||||||
|
import com.aliyun.teautil.models.RuntimeOptions;
|
||||||
|
import com.muyu.vehicle.domain.InstanceInfo;
|
||||||
|
import com.muyu.vehicle.service.OpenInstance;
|
||||||
|
import com.muyu.vehicle.service.SelectInstance;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.ApplicationArguments;
|
||||||
|
import org.springframework.boot.ApplicationRunner;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Log4j2
|
||||||
|
public class ManageInstance implements ApplicationRunner {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ACCESS_KEY_ID
|
||||||
|
*/
|
||||||
|
public static final String ALIBABA_CLOUD_ACCESS_KEY_ID="LTAI5tGabdxedjfCh2uXHNrw";
|
||||||
|
|
||||||
|
/**
|
||||||
|
*ACCESS_KEY_SECRET
|
||||||
|
*/
|
||||||
|
public static final String ACCESS_KEY_SECRET="NHb7wHVpesLW6Axc0bFBs6ThhuNR10";
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 镜像ID
|
||||||
|
*/
|
||||||
|
public static final String IMAGE_ID="m-uf6agr9i6g27gj23om34";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 实例类型
|
||||||
|
*/
|
||||||
|
public static final String INSTANCE_TYPE="ecs.e-c1m1.large";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 安全组ID
|
||||||
|
*/
|
||||||
|
public static final String SECURITY_GROUP_ID="sg-uf6glo8c4k17szhxu7sk";
|
||||||
|
|
||||||
|
/**
|
||||||
|
*交换机ID
|
||||||
|
*/
|
||||||
|
public static final String V_SWITCH_ID="vsw-uf6xy4rbt9ggcz93t6oib";
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 实例付费类型
|
||||||
|
*/
|
||||||
|
public static final String INSTANCE_CHARGE_TY="PostPaid";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用AK&SK初始化账号Client
|
||||||
|
* @return Client
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static Client createClient() throws Exception {
|
||||||
|
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||||
|
Config config = new Config()
|
||||||
|
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||||
|
.setAccessKeyId(ALIBABA_CLOUD_ACCESS_KEY_ID)
|
||||||
|
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||||
|
.setAccessKeySecret(ACCESS_KEY_SECRET);
|
||||||
|
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||||
|
config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
|
||||||
|
return new com.aliyun.ecs20140526.Client(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void generateInstance() throws Exception {
|
||||||
|
// 创建阿里云ECS客户端
|
||||||
|
Client client = ManageInstance.createClient();
|
||||||
|
// 配置系统盘参数
|
||||||
|
RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk=
|
||||||
|
new RunInstancesRequest.RunInstancesRequestSystemDisk()
|
||||||
|
.setSize("40")
|
||||||
|
.setCategory("cloud_essd");
|
||||||
|
|
||||||
|
// 创建创建实例请求对象并设置参数
|
||||||
|
|
||||||
|
RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
|
||||||
|
.setRegionId("cn-shanghai") // 设置地域ID
|
||||||
|
.setImageId(IMAGE_ID) // 设置镜像ID
|
||||||
|
.setInstanceType(INSTANCE_TYPE) // 设置实例类型
|
||||||
|
.setSecurityGroupId(SECURITY_GROUP_ID) // 设置安全组ID
|
||||||
|
.setVSwitchId(V_SWITCH_ID) // 设置虚拟交换机ID
|
||||||
|
.setInstanceName("cloud-MQTT") // 设置实例名称
|
||||||
|
.setInstanceChargeType(INSTANCE_CHARGE_TY) // 设置实例付费类型为后付费按量付费
|
||||||
|
.setSystemDisk(systemDisk) // 设置系统盘配置
|
||||||
|
.setHostName("root") // 设置主机名
|
||||||
|
.setPassword("2112A-four") // 设置实例密码
|
||||||
|
.setAmount(2) // 设置创建实例的数量
|
||||||
|
.setInternetChargeType("PayByTraffic")
|
||||||
|
.setInternetMaxBandwidthOut(1);
|
||||||
|
|
||||||
|
|
||||||
|
//创建运行时选择对象
|
||||||
|
RuntimeOptions runTime=
|
||||||
|
new RuntimeOptions();
|
||||||
|
// 尝试执行创建实例请求
|
||||||
|
try {
|
||||||
|
ArrayList<String> list = new ArrayList<>();
|
||||||
|
// 复制代码运行请自行打印 API 的返回值
|
||||||
|
RunInstancesResponse runInstancesResponse = client.runInstancesWithOptions(runInstancesRequest, runTime);
|
||||||
|
RunInstancesResponseBody body = runInstancesResponse.getBody();
|
||||||
|
for (String instance : body.getInstanceIdSets().getInstanceIdSet()) {
|
||||||
|
list.add(instance);
|
||||||
|
}
|
||||||
|
log.info("ESC创建成功,实例ID为:" + list);
|
||||||
|
} catch (TeaException error) {
|
||||||
|
// 错误 message
|
||||||
|
log.info(error.getMessage());
|
||||||
|
// 诊断地址
|
||||||
|
log.info(error.getData().get("Recommend"));
|
||||||
|
Common.assertAsString(error.message);
|
||||||
|
} catch (Exception _error) {
|
||||||
|
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||||
|
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||||
|
// 错误 message
|
||||||
|
log.info("实例创建失败:"+error.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<InstanceInfo> selectInstance() throws Exception {
|
||||||
|
Client client = ManageInstance.createClient();
|
||||||
|
ArrayList<InstanceInfo> instanceInfos = new ArrayList<>();// 实例基础信息
|
||||||
|
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
|
||||||
|
.setRegionId("cn-shanghai")
|
||||||
|
.setInternetChargeType("PayByTraffic")
|
||||||
|
.setInstanceChargeType("PostPaid")
|
||||||
|
.setInstanceName("cloud-MQTT") // 设置实例名称
|
||||||
|
;
|
||||||
|
// 创建运行时选项对象
|
||||||
|
RuntimeOptions runtime = new RuntimeOptions();
|
||||||
|
//实例ID Instances.Instance.InstanceId
|
||||||
|
//实例IP Instances.Instance.PublicIpAddress.IpAddress
|
||||||
|
//状态 Instances.Instance.Status
|
||||||
|
DescribeInstancesResponse resp =client.describeInstancesWithOptions(describeInstancesRequest, runtime);
|
||||||
|
DescribeInstancesResponseBody body = resp.getBody();
|
||||||
|
|
||||||
|
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : body.getInstances().getInstance()){
|
||||||
|
InstanceInfo instanceInfo = new InstanceInfo();
|
||||||
|
instanceInfo.setInstanceId(instance.getInstanceId());
|
||||||
|
instanceInfo.setIpAddress(String.valueOf(instance.getPublicIpAddress().getIpAddress()));
|
||||||
|
instanceInfo.setStatus(instance.getStatus());
|
||||||
|
instanceInfos.add(instanceInfo);
|
||||||
|
|
||||||
|
}
|
||||||
|
log.info("实例信息为:"+Common.toJSONString(instanceInfos));
|
||||||
|
return instanceInfos;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(ApplicationArguments args) throws Exception {
|
||||||
|
generateInstance();
|
||||||
|
selectInstance();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.muyu.vehicle.domain;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class InstanceInfo {
|
||||||
|
|
||||||
|
//实例ID
|
||||||
|
private String InstanceId;
|
||||||
|
|
||||||
|
private String IpAddress;
|
||||||
|
|
||||||
|
private String status;
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.muyu.vehicle;
|
package com.muyu.vehicle.service;
|
||||||
|
|
||||||
|
|
||||||
import com.aliyun.ecs20140526.Client;
|
import com.aliyun.ecs20140526.Client;
|
|
@ -1,20 +1,20 @@
|
||||||
package com.muyu.vehicle;
|
package com.muyu.vehicle.service;
|
||||||
|
|
||||||
import com.aliyun.ecs20140526.Client;
|
import com.aliyun.ecs20140526.Client;
|
||||||
import com.aliyun.ecs20140526.models.RunInstancesRequest;
|
import com.aliyun.ecs20140526.models.RunInstancesRequest;
|
||||||
|
import com.aliyun.ecs20140526.models.RunInstancesResponse;
|
||||||
|
import com.aliyun.ecs20140526.models.RunInstancesResponseBody;
|
||||||
import com.aliyun.tea.TeaException;
|
import com.aliyun.tea.TeaException;
|
||||||
import com.aliyun.teaopenapi.models.Config;
|
import com.aliyun.teaopenapi.models.Config;
|
||||||
import com.aliyun.teautil.Common;
|
import com.aliyun.teautil.Common;
|
||||||
import com.aliyun.teautil.models.RuntimeOptions;
|
import com.aliyun.teautil.models.RuntimeOptions;
|
||||||
|
import com.muyu.vehicle.ManageInstance;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
import org.springframework.boot.ApplicationArguments;
|
|
||||||
import org.springframework.boot.ApplicationRunner;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Component
|
import java.util.ArrayList;
|
||||||
|
|
||||||
@Log4j2
|
@Log4j2
|
||||||
public class OpenInstance implements ApplicationRunner {
|
public class OpenInstance {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ACCESS_KEY_ID
|
* ACCESS_KEY_ID
|
||||||
*/
|
*/
|
||||||
|
@ -95,7 +95,7 @@ public class OpenInstance implements ApplicationRunner {
|
||||||
.setSystemDisk(systemDisk) // 设置系统盘配置
|
.setSystemDisk(systemDisk) // 设置系统盘配置
|
||||||
.setHostName("root") // 设置主机名
|
.setHostName("root") // 设置主机名
|
||||||
.setPassword("2112A-four") // 设置实例密码
|
.setPassword("2112A-four") // 设置实例密码
|
||||||
.setAmount(1) // 设置创建实例的数量
|
.setAmount(2) // 设置创建实例的数量
|
||||||
.setInternetChargeType("PayByTraffic")
|
.setInternetChargeType("PayByTraffic")
|
||||||
.setInternetMaxBandwidthOut(1);
|
.setInternetMaxBandwidthOut(1);
|
||||||
|
|
||||||
|
@ -105,9 +105,14 @@ public class OpenInstance implements ApplicationRunner {
|
||||||
new RuntimeOptions();
|
new RuntimeOptions();
|
||||||
// 尝试执行创建实例请求
|
// 尝试执行创建实例请求
|
||||||
try {
|
try {
|
||||||
|
ArrayList<String> list = new ArrayList<>();
|
||||||
// 复制代码运行请自行打印 API 的返回值
|
// 复制代码运行请自行打印 API 的返回值
|
||||||
client.runInstancesWithOptions(runInstancesRequest, runTime);
|
RunInstancesResponse runInstancesResponse = client.runInstancesWithOptions(runInstancesRequest, runTime);
|
||||||
log.info("实例创建成功");
|
RunInstancesResponseBody body = runInstancesResponse.getBody();
|
||||||
|
for (String instance : body.getInstanceIdSets().getInstanceIdSet()) {
|
||||||
|
list.add(instance);
|
||||||
|
log.info("ESC创建成功,实例ID为:" + list);
|
||||||
|
}
|
||||||
} catch (TeaException error) {
|
} catch (TeaException error) {
|
||||||
// 错误 message
|
// 错误 message
|
||||||
log.info(error.getMessage());
|
log.info(error.getMessage());
|
||||||
|
@ -121,8 +126,4 @@ public class OpenInstance implements ApplicationRunner {
|
||||||
log.info("实例创建失败:"+error.getMessage());
|
log.info("实例创建失败:"+error.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@Override
|
|
||||||
public void run(ApplicationArguments args) throws Exception {
|
|
||||||
generateInstance();
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -0,0 +1,66 @@
|
||||||
|
package com.muyu.vehicle.service;
|
||||||
|
|
||||||
|
import com.aliyun.ecs20140526.Client;
|
||||||
|
import com.aliyun.ecs20140526.models.DescribeInstancesRequest;
|
||||||
|
import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
|
||||||
|
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
|
||||||
|
import com.aliyun.teaopenapi.models.Config;
|
||||||
|
import com.aliyun.teautil.Common;
|
||||||
|
import com.aliyun.teautil.models.RuntimeOptions;
|
||||||
|
import com.muyu.vehicle.domain.InstanceInfo;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
@Log4j2
|
||||||
|
public class SelectInstance {
|
||||||
|
/**
|
||||||
|
* ACCESS_KEY_ID
|
||||||
|
*/
|
||||||
|
public static final String ALIBABA_CLOUD_ACCESS_KEY_ID="LTAI5tGabdxedjfCh2uXHNrw";
|
||||||
|
|
||||||
|
/**
|
||||||
|
*ACCESS_KEY_SECRET
|
||||||
|
*/
|
||||||
|
public static final String ACCESS_KEY_SECRET="NHb7wHVpesLW6Axc0bFBs6ThhuNR10";
|
||||||
|
|
||||||
|
public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
|
||||||
|
Config config = new com.aliyun.teaopenapi.models.Config()
|
||||||
|
// 必填,您的 AccessKey ID
|
||||||
|
.setAccessKeyId(ALIBABA_CLOUD_ACCESS_KEY_ID)
|
||||||
|
// 必填,您的 AccessKey Secret
|
||||||
|
.setAccessKeySecret(ACCESS_KEY_SECRET);
|
||||||
|
// 访问的域名
|
||||||
|
config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
|
||||||
|
return new Client(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args_) throws Exception {
|
||||||
|
java.util.List<String> args = java.util.Arrays.asList(args_);
|
||||||
|
// 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||||
|
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的 STS 方式
|
||||||
|
Client client = SelectInstance.createClient(ALIBABA_CLOUD_ACCESS_KEY_ID, ACCESS_KEY_SECRET);
|
||||||
|
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
|
||||||
|
.setRegionId("cn-shanghai")
|
||||||
|
.setInternetChargeType("PayByTraffic")
|
||||||
|
.setInstanceChargeType("PostPaid")
|
||||||
|
.setInstanceName("cloud-MQTT") // 设置实例名称
|
||||||
|
;
|
||||||
|
//实例ID Instances.Instance.InstanceId
|
||||||
|
//实例IP Instances.Instance.PublicIpAddress.IpAddress
|
||||||
|
//状态 Instances.Instance.Status
|
||||||
|
RuntimeOptions runtime = new RuntimeOptions();
|
||||||
|
DescribeInstancesResponse resp = client.describeInstancesWithOptions(describeInstancesRequest, runtime);
|
||||||
|
DescribeInstancesResponseBody body = resp.getBody();
|
||||||
|
ArrayList<InstanceInfo> instanceInfos = new ArrayList<>();// 实例基础信息
|
||||||
|
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : body.getInstances().getInstance()){
|
||||||
|
|
||||||
|
InstanceInfo instanceInfo = new InstanceInfo();
|
||||||
|
instanceInfo.setInstanceId(instance.getInstanceId());
|
||||||
|
instanceInfo.setIpAddress(String.valueOf(instance.getPublicIpAddress().getIpAddress()));
|
||||||
|
instanceInfo.setStatus(instance.getStatus());
|
||||||
|
instanceInfos.add(instanceInfo);
|
||||||
|
}
|
||||||
|
log.info(Common.toJSONString(instanceInfos));
|
||||||
|
}
|
||||||
|
}
|
|
@ -54,3 +54,5 @@ logging:
|
||||||
level:
|
level:
|
||||||
com.muyu.system.mapper: DEBUG
|
com.muyu.system.mapper: DEBUG
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue