获取到实例的ID和实例的公网ip以及实例状态
parent
723cb6556d
commit
b318c0e30b
|
@ -1,107 +0,0 @@
|
||||||
package com.muyu.cargateway.cargatewaytest;
|
|
||||||
|
|
||||||
import com.aliyun.ecs20140526.Client;
|
|
||||||
import com.aliyun.ecs20140526.models.RunInstancesRequest;
|
|
||||||
import com.aliyun.tea.TeaException;
|
|
||||||
import com.aliyun.teaopenapi.models.Config;
|
|
||||||
import com.aliyun.teautil.Common;
|
|
||||||
import com.aliyun.teautil.models.RuntimeOptions;
|
|
||||||
import lombok.extern.log4j.Log4j2;
|
|
||||||
import org.springframework.boot.ApplicationArguments;
|
|
||||||
import org.springframework.boot.ApplicationRunner;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @ Tool:IntelliJ IDEA
|
|
||||||
* @ Author:CHX
|
|
||||||
* @ Date:2024-09-28-21:10
|
|
||||||
* @ Version:1.0
|
|
||||||
* @ Description:调用ali的类
|
|
||||||
* @author Lenovo
|
|
||||||
*/
|
|
||||||
@Log4j2
|
|
||||||
@Component
|
|
||||||
public class Sample implements ApplicationRunner {
|
|
||||||
|
|
||||||
public static Client createClient() throws Exception {
|
|
||||||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
|
||||||
Config config = new Config()
|
|
||||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
|
||||||
.setAccessKeyId("LTAI5tDH3FyRx4PRr6anx2TL")
|
|
||||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
|
||||||
.setAccessKeySecret("xdQnX2tDattY50raNkUWmHzE2tondP");
|
|
||||||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
|
||||||
config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
|
|
||||||
return new Client(config);
|
|
||||||
}
|
|
||||||
public static void generateInstance() throws Exception {
|
|
||||||
Client client = Sample.createClient();
|
|
||||||
RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = new RunInstancesRequest.RunInstancesRequestSystemDisk();
|
|
||||||
//设置系统盘的大小为40GiB
|
|
||||||
systemDisk.setSize("40");
|
|
||||||
//设置系统盘类型为cloud_essd
|
|
||||||
systemDisk.setCategory("cloud_essd");
|
|
||||||
|
|
||||||
// 创建创建实例请求对象并设置参数
|
|
||||||
|
|
||||||
RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
|
|
||||||
// 设置地域ID
|
|
||||||
.setRegionId("cn-shanghai")
|
|
||||||
// 设置镜像ID
|
|
||||||
.setImageId("m-uf65fvle9qseu9jo5jl3")
|
|
||||||
// 设置实例规格类型
|
|
||||||
.setInstanceType("ecs.t6-c1m1.large")
|
|
||||||
// 设置安全组ID
|
|
||||||
.setSecurityGroupId("sg-uf642d5u4ja5gsiitx8y")
|
|
||||||
// 设置虚拟交换机ID
|
|
||||||
.setVSwitchId("vsw-uf66lifrkhxqc94xi06v3")
|
|
||||||
// 设置实例名称
|
|
||||||
.setInstanceName("server-mqtt")
|
|
||||||
// 设置付费类型 按量付费
|
|
||||||
.setInstanceChargeType("PostPaid")
|
|
||||||
// 设置系统盘配置
|
|
||||||
.setSystemDisk(systemDisk)
|
|
||||||
// 设置用户名
|
|
||||||
.setHostName("root")
|
|
||||||
// 设置密码
|
|
||||||
.setPassword("10160810@a")
|
|
||||||
// 设置要创创建的实例数量
|
|
||||||
.setAmount(1)
|
|
||||||
.setInternetChargeType("PayByTraffic")
|
|
||||||
.setInternetMaxBandwidthOut(1);
|
|
||||||
|
|
||||||
RuntimeOptions runtimeOptions = new RuntimeOptions();
|
|
||||||
// 尝试执行创建实例请求
|
|
||||||
try {
|
|
||||||
// 复制代码运行请自行打印 API 的返回值
|
|
||||||
client.runInstancesWithOptions(runInstancesRequest, runtimeOptions);
|
|
||||||
} 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"));
|
|
||||||
Common.assertAsString(error.message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run(ApplicationArguments args){
|
|
||||||
try {
|
|
||||||
log.info("开始创建实例");
|
|
||||||
generateInstance();
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.info("创建实例失败");
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
log.info("创建实例成功");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.muyu.cargateway.domain;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-09-30-9:33
|
||||||
|
* @ Version:1.0
|
||||||
|
* @ Description:
|
||||||
|
* @author Lenovo
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class Instance {
|
||||||
|
private String instanceId;
|
||||||
|
private String ipAddress;
|
||||||
|
private String status;
|
||||||
|
}
|
|
@ -0,0 +1,246 @@
|
||||||
|
package com.muyu.cargateway.instance;
|
||||||
|
|
||||||
|
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.common.redis.service.RedisService;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.springframework.beans.factory.DisposableBean;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.ApplicationArguments;
|
||||||
|
import org.springframework.boot.ApplicationRunner;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-09-28-21:10
|
||||||
|
* @ Version:1.0
|
||||||
|
* @ Description:调用ali的类
|
||||||
|
* @author Lenovo
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
@Component
|
||||||
|
public class Sample implements ApplicationRunner , DisposableBean {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private StringRedisTemplate redisTemplate;
|
||||||
|
@Autowired
|
||||||
|
private RedisService redisService;
|
||||||
|
|
||||||
|
public static final String ACCESS_KEY_ID = "LTAI5tDH3FyRx4PRr6anx2TL";
|
||||||
|
public static final String ACCESS_KEY_SECRET = "xdQnX2tDattY50raNkUWmHzE2tondP";
|
||||||
|
|
||||||
|
public static Client createClient() throws Exception {
|
||||||
|
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||||
|
Config config = new Config()
|
||||||
|
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||||
|
.setAccessKeyId(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 Client(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建实例
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public void generateInstance() throws Exception {
|
||||||
|
Client client = Sample.createClient();
|
||||||
|
RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = new RunInstancesRequest.RunInstancesRequestSystemDisk();
|
||||||
|
//设置系统盘的大小为40GiB
|
||||||
|
systemDisk.setSize("40");
|
||||||
|
//设置系统盘类型为cloud_essd
|
||||||
|
systemDisk.setCategory("cloud_essd");
|
||||||
|
|
||||||
|
// 创建创建实例请求对象并设置参数
|
||||||
|
|
||||||
|
RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
|
||||||
|
// 设置地域ID
|
||||||
|
.setRegionId("cn-shanghai")
|
||||||
|
// 设置镜像ID
|
||||||
|
.setImageId("m-uf63y73rquw4ub9znymf")
|
||||||
|
// 设置实例规格类型
|
||||||
|
.setInstanceType("ecs.t6-c1m1.large")
|
||||||
|
// 设置安全组ID
|
||||||
|
.setSecurityGroupId("sg-uf642d5u4ja5gsiitx8y")
|
||||||
|
// 设置虚拟交换机ID
|
||||||
|
.setVSwitchId("vsw-uf66lifrkhxqc94xi06v3")
|
||||||
|
// 设置实例名称
|
||||||
|
.setInstanceName("server-mqtt")
|
||||||
|
// 设置付费类型 按量付费
|
||||||
|
.setInstanceChargeType("PostPaid")
|
||||||
|
// 设置系统盘配置
|
||||||
|
.setSystemDisk(systemDisk)
|
||||||
|
// 设置用户名
|
||||||
|
.setHostName("root")
|
||||||
|
// 设置密码
|
||||||
|
.setPassword("10160810@a")
|
||||||
|
// 设置要创建的实例数量
|
||||||
|
.setAmount(2)
|
||||||
|
.setInternetChargeType("PayByTraffic")
|
||||||
|
.setInternetMaxBandwidthOut(1);
|
||||||
|
|
||||||
|
RuntimeOptions runtimeOptions = new RuntimeOptions();
|
||||||
|
// 尝试执行创建实例请求
|
||||||
|
try {
|
||||||
|
|
||||||
|
|
||||||
|
// 复制代码运行请自行打印 API 的返回值
|
||||||
|
RunInstancesResponse runInstancesResponse = client.runInstancesWithOptions(runInstancesRequest, runtimeOptions);
|
||||||
|
log.info("==================实例创建成功");
|
||||||
|
RunInstancesResponseBody body = runInstancesResponse.getBody();
|
||||||
|
RunInstancesResponseBody.RunInstancesResponseBodyInstanceIdSets instanceIdSets = body.getInstanceIdSets();
|
||||||
|
|
||||||
|
List<String> instanceIds = new ArrayList<>();
|
||||||
|
for (String string : instanceIdSets.instanceIdSet) {
|
||||||
|
instanceIds.add(string);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
DescribeInstancesResponse describeInstancesResponse = getStrings(client);
|
||||||
|
log.info("====>实例ID"+instanceIds);
|
||||||
|
List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance> instancesInstances = describeInstancesResponse.getBody().getInstances().getInstance();
|
||||||
|
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : instancesInstances) {
|
||||||
|
String instanceId = instance.getInstanceId();
|
||||||
|
String ipAddress = instance.getPublicIpAddress().getIpAddress().get(0);
|
||||||
|
String status = instance.getStatus();
|
||||||
|
log.info("==================获取到的实例ID为"+instanceId+" IP为"+ipAddress+" 状态为"+status);
|
||||||
|
instanceIds.add(instanceId);
|
||||||
|
instanceIds.add(ipAddress);
|
||||||
|
instanceIds.add(status);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// for (String string : stringArrayList) {
|
||||||
|
// log.info("==================获取到的实例ID为"+string);
|
||||||
|
// }
|
||||||
|
// redisTemplate.opsForList().rightPush("instanceId",String.valueOf(stringArrayList));
|
||||||
|
// log.info("==================redis存储成功");
|
||||||
|
// List<String> instanceId = redisTemplate.opsForList().range("instanceId", 0, -1);
|
||||||
|
// for (String string : instanceId) {
|
||||||
|
// log.info("==================redis获取实例ID成功"+string);
|
||||||
|
// }
|
||||||
|
|
||||||
|
} 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"));
|
||||||
|
Common.assertAsString(error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询实例的详细信息
|
||||||
|
*/
|
||||||
|
public void selectInstance(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除创建的实例
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public void deleteSample() throws Exception {
|
||||||
|
Client client = Sample.createClient();
|
||||||
|
DescribeInstancesRequest attributeRequest = new DescribeInstancesRequest();
|
||||||
|
attributeRequest.setRegionId("cn-shanghai");
|
||||||
|
|
||||||
|
RuntimeOptions runtimeOptions = new RuntimeOptions();
|
||||||
|
|
||||||
|
DescribeInstancesResponse instancesWithOptions = client.describeInstancesWithOptions(attributeRequest, runtimeOptions);
|
||||||
|
ArrayList<String> list = new ArrayList<>();
|
||||||
|
DescribeInstancesResponseBody body = instancesWithOptions.getBody();
|
||||||
|
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : body.instances.instance) {
|
||||||
|
list.add(instance.getInstanceId());
|
||||||
|
}
|
||||||
|
log.info("list:"+list);
|
||||||
|
DeleteInstancesRequest deleteInstancesRequest = new DeleteInstancesRequest();
|
||||||
|
deleteInstancesRequest.setRegionId("cn-shanghai")
|
||||||
|
.setDryRun(false)
|
||||||
|
.setForce(true)
|
||||||
|
.setTerminateSubscription(true)
|
||||||
|
.setInstanceId(list);
|
||||||
|
RuntimeOptions runtime = new RuntimeOptions();
|
||||||
|
try{
|
||||||
|
client.deleteInstancesWithOptions(deleteInstancesRequest, runtime);
|
||||||
|
}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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取实例ID
|
||||||
|
* @param client
|
||||||
|
* @return string类型的实例ID集合
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
private DescribeInstancesResponse getStrings(Client client) throws Exception {
|
||||||
|
DescribeInstancesRequest attributeRequest = new DescribeInstancesRequest();
|
||||||
|
attributeRequest.setInstanceName("server-mqtt");
|
||||||
|
attributeRequest.setRegionId("cn-shanghai");
|
||||||
|
|
||||||
|
RuntimeOptions runtimeOptions = new RuntimeOptions();
|
||||||
|
|
||||||
|
DescribeInstancesResponse instancesWithOptions = client.describeInstancesWithOptions(attributeRequest, runtimeOptions);
|
||||||
|
return instancesWithOptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(ApplicationArguments args){
|
||||||
|
try {
|
||||||
|
log.info("开始创建实例");
|
||||||
|
generateInstance();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.info("创建实例失败");
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
log.info("创建实例成功");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroy(){
|
||||||
|
try {
|
||||||
|
log.info("删除实例方法");
|
||||||
|
deleteSample();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.info("删除实例失败");
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
log.info("删除实例成功");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,89 @@
|
||||||
|
//package com.muyu.cargateway.cargatewaytest;
|
||||||
|
//
|
||||||
|
//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.models.RuntimeOptions;
|
||||||
|
//import lombok.extern.log4j.Log4j2;
|
||||||
|
//import org.springframework.beans.factory.DisposableBean;
|
||||||
|
//import org.springframework.stereotype.Component;
|
||||||
|
//
|
||||||
|
//import java.util.ArrayList;
|
||||||
|
//
|
||||||
|
///**
|
||||||
|
// * @ Tool:IntelliJ IDEA
|
||||||
|
// * @ Author:CHX
|
||||||
|
// * @ Date:2024-09-28-21:20
|
||||||
|
// * @ Version:1.0
|
||||||
|
// * @ Description:删除实例
|
||||||
|
// * @author Lenovo
|
||||||
|
// */
|
||||||
|
//@Log4j2
|
||||||
|
//@Component
|
||||||
|
//public class DeleteSample implements DisposableBean {
|
||||||
|
//
|
||||||
|
// public static Client createClient() throws Exception {
|
||||||
|
// // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||||
|
// Config config = new Config()
|
||||||
|
// // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||||
|
// .setAccessKeyId("LTAI5tDH3FyRx4PRr6anx2TL")
|
||||||
|
// // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||||
|
// .setAccessKeySecret("xdQnX2tDattY50raNkUWmHzE2tondP");
|
||||||
|
// // Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||||
|
// config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
|
||||||
|
// return new Client(config);
|
||||||
|
// }
|
||||||
|
// public static void deleteSample() throws Exception {
|
||||||
|
// Client client = DeleteSample.createClient();
|
||||||
|
// DescribeInstancesRequest attributeRequest = new DescribeInstancesRequest();
|
||||||
|
// attributeRequest.setRegionId("cn-shanghai");
|
||||||
|
//
|
||||||
|
// RuntimeOptions runtimeOptions = new RuntimeOptions();
|
||||||
|
//
|
||||||
|
// DescribeInstancesResponse instancesWithOptions = client.describeInstancesWithOptions(attributeRequest, runtimeOptions);
|
||||||
|
// ArrayList<String> list = new ArrayList<>();
|
||||||
|
// DescribeInstancesResponseBody body = instancesWithOptions.getBody();
|
||||||
|
// for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : body.instances.instance) {
|
||||||
|
// list.add(instance.getInstanceId());
|
||||||
|
// }
|
||||||
|
// log.info("list:"+list);
|
||||||
|
// DeleteInstancesRequest deleteInstancesRequest = new DeleteInstancesRequest();
|
||||||
|
// deleteInstancesRequest.setRegionId("cn-shanghai")
|
||||||
|
// .setDryRun(false)
|
||||||
|
// .setForce(true)
|
||||||
|
// .setTerminateSubscription(true)
|
||||||
|
// .setInstanceId(list);
|
||||||
|
//
|
||||||
|
// RuntimeOptions runtime = new RuntimeOptions();
|
||||||
|
// try{
|
||||||
|
// client.deleteInstancesWithOptions(deleteInstancesRequest, runtime);
|
||||||
|
// }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);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// @Override
|
||||||
|
// public void destroy(){
|
||||||
|
// try {
|
||||||
|
// log.info("删除实例方法");
|
||||||
|
// deleteSample();
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// log.info("删除实例失败");
|
||||||
|
// throw new RuntimeException(e);
|
||||||
|
// }
|
||||||
|
// log.info("删除实例成功");
|
||||||
|
// }
|
||||||
|
//}
|
|
@ -1,60 +0,0 @@
|
||||||
//package com.muyu.cargateway.test;
|
|
||||||
//
|
|
||||||
//import lombok.extern.log4j.Log4j2;
|
|
||||||
//import org.eclipse.paho.client.mqttv3.MqttClient;
|
|
||||||
//import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
|
||||||
//import org.eclipse.paho.client.mqttv3.MqttException;
|
|
||||||
//import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|
||||||
//
|
|
||||||
///**
|
|
||||||
// * @ Tool:IntelliJ IDEA
|
|
||||||
// * @ Author:CHX
|
|
||||||
// * @ Date:2024-09-27-22:32
|
|
||||||
// * @ Version:1.0
|
|
||||||
// * @ Description:mqtt
|
|
||||||
// * @author Lenovo
|
|
||||||
// */
|
|
||||||
//@Log4j2
|
|
||||||
//public class MqttPublisher {
|
|
||||||
// //MQTT代理地址
|
|
||||||
// String broker="tcp://106.54.193.225:1883";
|
|
||||||
// //客户端ID
|
|
||||||
// String clientId="car-gateway";
|
|
||||||
// //主题
|
|
||||||
// String topic="car-gateway";
|
|
||||||
// //要发送的内容
|
|
||||||
// String content="连接成功";
|
|
||||||
// //服务质量 0:最多一次 1:至少一次 2:
|
|
||||||
// int qos=0;
|
|
||||||
// MqttClient mqttClient;
|
|
||||||
//
|
|
||||||
// {
|
|
||||||
// try {
|
|
||||||
// mqttClient = new MqttClient(broker, clientId);
|
|
||||||
//
|
|
||||||
// //连接到MQtt代理
|
|
||||||
// MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
|
|
||||||
// //清空会话
|
|
||||||
// mqttConnectOptions.setCleanSession(true);
|
|
||||||
// //连接
|
|
||||||
// mqttClient.connect(mqttConnectOptions);
|
|
||||||
// log.info("MQTT客户端连接成功"+broker);
|
|
||||||
//
|
|
||||||
// //发送消息
|
|
||||||
// MqttMessage message = new MqttMessage(content.getBytes());
|
|
||||||
// //设置服务的质量
|
|
||||||
// message.setQos(qos);
|
|
||||||
// //开始发送消息
|
|
||||||
// mqttClient.publish(topic, message);
|
|
||||||
// log.info("消息发送成功,消息是:"+content);
|
|
||||||
//
|
|
||||||
// //断开连接
|
|
||||||
// mqttClient.disconnect();
|
|
||||||
// log.info("MQTT断开连接成功");
|
|
||||||
// } catch (MqttException e) {
|
|
||||||
// //输出异常信息
|
|
||||||
// e.getStackTrace();
|
|
||||||
// throw new RuntimeException(e);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//}
|
|
|
@ -0,0 +1,79 @@
|
||||||
|
package com.muyu.cargateway.test;
|
||||||
|
|
||||||
|
import com.aliyun.ecs20140526.Client;
|
||||||
|
import com.aliyun.ecs20140526.models.DescribeInstanceAttributeRequest;
|
||||||
|
import com.aliyun.ecs20140526.models.DescribeInstancesRequest;
|
||||||
|
import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
|
||||||
|
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
|
||||||
|
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.cargateway.instance.Sample;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-09-29-20:16
|
||||||
|
* @ Version:1.0
|
||||||
|
* @ Description:查询实例所有信息
|
||||||
|
* @author Lenovo
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
public class SelectSample {
|
||||||
|
public static Client createClient() throws Exception {
|
||||||
|
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||||
|
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
|
||||||
|
Config config = new Config()
|
||||||
|
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||||
|
.setAccessKeyId("LTAI5tDH3FyRx4PRr6anx2TL")
|
||||||
|
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||||
|
.setAccessKeySecret("xdQnX2tDattY50raNkUWmHzE2tondP");
|
||||||
|
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||||
|
config.endpoint = "ecs.cn-shanghai.aliyuncs.com";
|
||||||
|
return new Client(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args_) throws Exception {
|
||||||
|
List<String> args = Arrays.asList(args_);
|
||||||
|
Client client = Sample.createClient();
|
||||||
|
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
|
||||||
|
.setRegionId("cn-shanghai");
|
||||||
|
RuntimeOptions runtime = new RuntimeOptions();
|
||||||
|
try {
|
||||||
|
// 复制代码运行请自行打印 API 的返回值
|
||||||
|
DescribeInstancesResponse instancesWithOptions = client.describeInstancesWithOptions(describeInstancesRequest, runtime);
|
||||||
|
ArrayList<String> list = new ArrayList<>();
|
||||||
|
DescribeInstancesResponseBody body = instancesWithOptions.getBody();
|
||||||
|
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : body.instances.instance) {
|
||||||
|
list.add(instance.getInstanceId());
|
||||||
|
list.add(instance.getPublicIpAddress().ipAddress.get(0));
|
||||||
|
list.add(instance.getStatus());
|
||||||
|
|
||||||
|
}
|
||||||
|
log.info(Common.toJSONString(list));
|
||||||
|
} catch (TeaException error) {
|
||||||
|
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||||
|
// 错误 message
|
||||||
|
System.out.println(error.getMessage());
|
||||||
|
// 诊断地址
|
||||||
|
System.out.println(error.getData().get("Recommend"));
|
||||||
|
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"));
|
||||||
|
Common.assertAsString(error.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue