feat():程序启动时可以通过新创的服务器id查询到ip和状态
parent
cb79600bc0
commit
add31c49fa
|
@ -84,16 +84,19 @@
|
|||
<artifactId>ecs20140526</artifactId>
|
||||
<version>5.1.8</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>tea-openapi</artifactId>
|
||||
<version>0.3.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>tea-console</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>tea-util</artifactId>
|
||||
|
@ -107,6 +110,14 @@
|
|||
|
||||
|
||||
</dependencies>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>aliyun-repo</id>
|
||||
<url>https://maven.aliyun.com/repository/public</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
package com.muyu.vehiclegateway.instance;
|
||||
|
||||
import com.aliyun.cloudapi20160714.models.DescribeInstancesRequest;
|
||||
import com.aliyun.cloudapi20160714.models.DescribeInstancesResponse;
|
||||
import com.aliyun.ecs20140526.Client;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @ClassName acquireInstance
|
||||
* @Description 描述
|
||||
* @Author YiBo.Liu
|
||||
* @Date 2024/9/29 17:24
|
||||
*/
|
||||
@Component
|
||||
public class AcquireInstance {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,16 +1,25 @@
|
|||
package com.muyu.vehiclegateway.instance;
|
||||
|
||||
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.ecs20140526.models.RunInstancesRequest;
|
||||
import com.aliyun.tea.TeaException;
|
||||
import com.aliyun.teautil.Common;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
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.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @ClassName GenerateInstance
|
||||
* @Description 描述
|
||||
|
@ -22,11 +31,14 @@ import org.springframework.stereotype.Component;
|
|||
@Tag(name = "程序启动创建ECS服务器实例")
|
||||
public class GenerateInstance implements ApplicationRunner {
|
||||
|
||||
@Autowired
|
||||
private static StringRedisTemplate redisTemplate;
|
||||
|
||||
/**
|
||||
* 启动自动创建实例
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void generateInstance() throws Exception {
|
||||
public static List<String> generateInstance() throws Exception {
|
||||
|
||||
// 创建ECS客户端对象,用于后续调用ECS相关API
|
||||
Client client = CreateClient.createClient();
|
||||
|
@ -58,7 +70,7 @@ public class GenerateInstance implements ApplicationRunner {
|
|||
// 设置实例密码
|
||||
.setPassword("Six@211206")
|
||||
// 设置创建实例的数量
|
||||
.setAmount(1);
|
||||
.setAmount(2);
|
||||
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
try {
|
||||
|
@ -80,8 +92,43 @@ public class GenerateInstance implements ApplicationRunner {
|
|||
System.out.println(error.getData().get("Recommend"));
|
||||
Common.assertAsString(error.message);
|
||||
}
|
||||
|
||||
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
|
||||
.setRegionId("cn-shanghai");
|
||||
|
||||
//创建运行时选择对象,用于配置运行时的选项参数
|
||||
RuntimeOptions runtimeOptions = new RuntimeOptions();
|
||||
|
||||
//获取实例列表
|
||||
DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(describeInstancesRequest, runtimeOptions);
|
||||
|
||||
//提取实例ID集合
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
|
||||
DescribeInstancesResponseBody body = describeInstancesResponse.getBody();
|
||||
|
||||
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : body.getInstances().getInstance()) {
|
||||
list.add(instance.getInstanceId());
|
||||
list.add(instance.getPublicIpAddress().ipAddress.get(0));
|
||||
list.add(instance.getStatus());
|
||||
|
||||
log.info("实例id为:"+instance.getInstanceId());
|
||||
log.info("实例ip为:"+instance.getPublicIpAddress().ipAddress.get(0));
|
||||
log.info("实例状态为:"+instance.getStatus());
|
||||
}
|
||||
|
||||
// redisTemplate.opsForList().rightPushAll("shili",list.toString());
|
||||
//
|
||||
// List<String> shili = redisTemplate.opsForList().range("shili", 0, -1);
|
||||
// for (String string : shili) {
|
||||
// log.info("redis实例:"+string);
|
||||
// }
|
||||
|
||||
return list;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
generateInstance();
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration scan="true" scanPeriod="60 seconds" debug="false">
|
||||
<!-- 日志存放路径 -->
|
||||
<property name="log.path" value="logs/cloud-vehicleGateway"/>
|
||||
<property name="log.path" value="logs/cloud-vehicle-gateway"/>
|
||||
<!-- 日志输出格式 -->
|
||||
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n"/>
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration scan="true" scanPeriod="60 seconds" debug="false">
|
||||
<!-- 日志存放路径 -->
|
||||
<property name="log.path" value="logs/cloud-vehicleGateway"/>
|
||||
<property name="log.path" value="logs/cloud-vehicle-gateway"/>
|
||||
<!-- 日志输出格式 -->
|
||||
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n"/>
|
||||
<property name="log.sky.pattern" value="%d{HH:mm:ss.SSS} %yellow([%tid]) [%thread] %-5level %logger{20} - [%method,%line] - %msg%n"/>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration scan="true" scanPeriod="60 seconds" debug="false">
|
||||
<!-- 日志存放路径 -->
|
||||
<property name="log.path" value="logs/cloud-vehicleGateway"/>
|
||||
<property name="log.path" value="logs/cloud-vehicle-gateway"/>
|
||||
<!-- 日志输出格式 -->
|
||||
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n"/>
|
||||
<property name="log.sky.pattern" value="%d{HH:mm:ss.SSS} %yellow([%tid]) [%thread] %-5level %logger{20} - [%method,%line] - %msg%n"/>
|
||||
|
|
Loading…
Reference in New Issue