feat():增加实例信息查询
parent
27a4900926
commit
d88c10e453
|
@ -7,6 +7,9 @@
|
|||
<version>3.6.3</version>
|
||||
</parent>
|
||||
|
||||
<description>
|
||||
cloud-modules-car-gateway车辆网关模块
|
||||
</description>
|
||||
<artifactId>cloud-modules-car-gateway</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package com.muyu.car.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 实例基础信息
|
||||
* @Author:蓬叁
|
||||
* @Package:com.muyu.car.domain
|
||||
* @Project:cloud-server-8
|
||||
* @name:ExampleInformation
|
||||
* @Date:2024/9/29 下午10:01
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ExampleInformation {
|
||||
|
||||
/**
|
||||
* 实例ID
|
||||
*/
|
||||
private String InstanceId;
|
||||
/**
|
||||
* 实例IP
|
||||
*/
|
||||
private String IpAddress;
|
||||
/**
|
||||
* 实例状态
|
||||
*/
|
||||
private String status;
|
||||
|
||||
}
|
|
@ -3,6 +3,7 @@ package com.muyu.car.instance;
|
|||
import com.aliyun.ecs20140526.models.DescribeInstancesRequest;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
|
||||
import com.aliyun.ecs20140526.models.RunInstancesResponseBody;
|
||||
import com.aliyun.tea.*;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
|
|
|
@ -1,11 +1,17 @@
|
|||
package com.muyu.car.instance;
|
||||
|
||||
import com.aliyun.ecs20140526.models.*;
|
||||
import com.aliyun.tea.TeaException;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author:蓬叁
|
||||
* @Package:com.muyu.car.domain
|
||||
|
@ -22,7 +28,7 @@ public class GenerateInstance implements ApplicationRunner {
|
|||
* 启动自动创建实例
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void generateInstance() throws Exception {
|
||||
public static List<String> generateInstance() throws Exception {
|
||||
|
||||
// 创建ECS客户端对象,用于后续调用ECS相关API
|
||||
com.aliyun.ecs20140526.Client client = CreateClient.createClient();
|
||||
|
@ -34,7 +40,7 @@ public class GenerateInstance implements ApplicationRunner {
|
|||
// 设置地域ID
|
||||
.setRegionId("cn-hangzhou")
|
||||
// 设置镜像ID
|
||||
.setImageId("m-bp154fkbdlkjc1i3ku01")
|
||||
.setImageId("m-bp10rcc4nsihqfoz0w7s")
|
||||
// 设置实例类型
|
||||
.setInstanceType("ecs.t6-c1m1.large")
|
||||
// 设置安全组ID
|
||||
|
@ -54,14 +60,21 @@ public class GenerateInstance implements ApplicationRunner {
|
|||
// 设置实例密码
|
||||
.setPassword("EightGroup123.")
|
||||
// 设置创建实例的数量
|
||||
.setAmount(1);
|
||||
|
||||
|
||||
.setAmount(2);
|
||||
|
||||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||
try {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
client.runInstancesWithOptions(runInstancesRequest, runtime);
|
||||
RunInstancesResponse runInstancesResponse = client.runInstancesWithOptions(runInstancesRequest, runtime);
|
||||
// 获取body返回值对象
|
||||
RunInstancesResponseBody body = runInstancesResponse.getBody();
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
// 得到实例ID数组
|
||||
for (String instance : body.getInstanceIdSets().getInstanceIdSet()) {
|
||||
list.add(instance);
|
||||
}
|
||||
log.info("实例ID:{}",list);
|
||||
return list;
|
||||
} catch (TeaException error) {
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
|
@ -78,6 +91,7 @@ public class GenerateInstance implements ApplicationRunner {
|
|||
log.info(error.getData().get("Recommend"));
|
||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
package com.muyu.car.instance;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
|
||||
import com.aliyun.tea.TeaException;
|
||||
import com.muyu.car.domain.ExampleInformation;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author:蓬叁
|
||||
* @Package:com.muyu.car.instance
|
||||
* @Project:cloud-server-8
|
||||
* @name:QueryInstance
|
||||
* @Date:2024/9/29 下午8:58
|
||||
*/
|
||||
@Log4j2
|
||||
public class QueryInstance {
|
||||
|
||||
public static void queryInstance(List<String> instanceIds) throws Exception {
|
||||
com.aliyun.ecs20140526.Client client = CreateClient.createClient();
|
||||
|
||||
com.aliyun.ecs20140526.models.DescribeInstancesRequest describeInstancesRequest = new com.aliyun.ecs20140526.models.DescribeInstancesRequest()
|
||||
// .setInstanceName("server-mqtt")
|
||||
.setInstanceIds(JSON.toJSONString(instanceIds))
|
||||
.setRegionId("cn-hangzhou");
|
||||
|
||||
|
||||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||
|
||||
try {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(describeInstancesRequest, runtime);
|
||||
DescribeInstancesResponseBody body = describeInstancesResponse.getBody();
|
||||
ArrayList<ExampleInformation> exampleInformations = new ArrayList<>();
|
||||
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : body.getInstances().getInstance()){
|
||||
ExampleInformation exampleInformation = new ExampleInformation();
|
||||
exampleInformation.setInstanceId(instance.getInstanceId());
|
||||
log.info("实例ID:{}",exampleInformation.getInstanceId());
|
||||
exampleInformation.setStatus(instance.getStatus());
|
||||
log.info("实例状态:{}",exampleInformation.getStatus());
|
||||
exampleInformation.setIpAddress(String.valueOf(instance.getPublicIpAddress().getIpAddress()));
|
||||
log.info("实例IP:{}",exampleInformation.getIpAddress());
|
||||
exampleInformations.add(exampleInformation);
|
||||
}
|
||||
log.info("实例信息:{}",exampleInformations);
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue