feat(): 重构创建实例代码以及删除
parent
51ca5ef6f4
commit
27479e2b49
|
@ -6,6 +6,7 @@ import org.springframework.data.redis.core.HashOperations;
|
|||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
@ -17,6 +18,7 @@ import java.util.concurrent.TimeUnit;
|
|||
**/
|
||||
@SuppressWarnings(value = {"unchecked", "rawtypes"})
|
||||
@Component
|
||||
//@Service
|
||||
public class RedisService {
|
||||
@Autowired
|
||||
public RedisTemplate redisTemplate;
|
||||
|
|
|
@ -83,6 +83,10 @@
|
|||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-xxl</artifactId>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.muyu</groupId>-->
|
||||
<!-- <artifactId>cloud-common-redis</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
|
|
|
@ -4,13 +4,16 @@ import com.muyu.common.security.annotation.EnableCustomConfig;
|
|||
import com.muyu.common.security.annotation.EnableMyFeignClients;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
/**
|
||||
* 车辆网关负载中心
|
||||
*/
|
||||
@EnableCustomConfig
|
||||
@EnableMyFeignClients
|
||||
@EnableFeignClients
|
||||
@SpringBootApplication
|
||||
//@ComponentScan(basePackages = "com.muyu")
|
||||
public class VehicleGatewayApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(VehicleGatewayApplication.class, args);
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun;
|
||||
|
||||
import com.aliyun.ecs20140526.Client;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.muyu.cloud.vehicle.gateway.config.AliProperties;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* :Ali客户端
|
||||
*/
|
||||
@Configuration
|
||||
public class AliYunConfig {
|
||||
|
||||
@Autowired
|
||||
private AliProperties aliProperties;
|
||||
|
||||
@Bean
|
||||
public Client createClient() {
|
||||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||
Config config = new Config()
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||
.setAccessKeyId(aliProperties.getAccessKeyId())
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||
.setAccessKeySecret(aliProperties.getAccessKeySecret());
|
||||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||
config.endpoint = aliProperties.getEndpoint();
|
||||
try {
|
||||
return new Client(config);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,135 +0,0 @@
|
|||
// This file is auto-generated, don't edit it. Thanks.
|
||||
package com.muyu.cloud.vehicle.gateway.aliyun.ecs;
|
||||
|
||||
import com.aliyun.ecs20140526.Client;
|
||||
import com.aliyun.ecs20140526.models.DeleteInstancesRequest;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesRequest;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
|
||||
import com.aliyun.tea.*;
|
||||
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.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.event.ContextClosedEvent;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 删除实例
|
||||
*/
|
||||
@Log4j2
|
||||
@Component
|
||||
public class DelInstance implements ApplicationListener<ContextClosedEvent> {
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate redisTemplate;
|
||||
|
||||
|
||||
/**
|
||||
* <b>description</b> :
|
||||
* <p>使用AK&SK初始化账号Client</p>
|
||||
* @return Client
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
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("LTAI5t7Fnx2QLTYLSu9357wP")
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||
.setAccessKeySecret("3LOnydNZ25ytsTGczuSygElx0HJ6nN");
|
||||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||
config.endpoint = "ecs.cn-shanghai.aliyuncs.com";
|
||||
return new Client(config);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void delInstance() throws Exception{
|
||||
//创建ECS客户端对象,用于后续调用ECS相关API
|
||||
Client client = DelInstance.createClient();
|
||||
|
||||
List<String> list = redisTemplate.opsForList().range("instanceIds",0,-1);
|
||||
|
||||
// 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());
|
||||
// }
|
||||
//
|
||||
// System.out.println("Instance IDs"+list);
|
||||
|
||||
// 创建删除实例请求对象,并设置请求参数
|
||||
DeleteInstancesRequest deleteInstancesRequest = new DeleteInstancesRequest()
|
||||
// 设置地域ID,指定要删除的实例所属的地域ID。
|
||||
.setRegionId("cn-shanghai")
|
||||
//设置DryRun为True,用于验证请求是否可以成功,但不实际执行删除操作
|
||||
.setDryRun(false)
|
||||
// 设置Force为true,表示即使实例有正在运行的任务,也强制删除实例
|
||||
.setForce(true)
|
||||
// 设置TerminateSubscription为true,表示删除按订阅付费的实例时终止订阅
|
||||
.setTerminateSubscription(true)
|
||||
// 设置实例ID列表,使用参数名称 instanceId,参数类型为数组。
|
||||
.setInstanceId(list);
|
||||
// 创建运行时选项对象,用于配置运行时的选项参数
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
try{
|
||||
//复制代码运行请自行打印 API 的返回值
|
||||
client.deleteInstancesWithOptions(deleteInstancesRequest,runtime);
|
||||
} catch (TeaException error) {
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
log.error(error.getMessage());
|
||||
// 诊断地址
|
||||
log.error(error.getData().get("Recommend"));
|
||||
Common.assertAsString(error.message);
|
||||
} catch (Exception _error){
|
||||
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
log.error(error.getMessage());
|
||||
// 诊断地址
|
||||
log.error(error.getData().get("Recommend"));
|
||||
Common.assertAsString(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// @Override
|
||||
// public void destroy() throws Exception {
|
||||
// log.info("===============>开始执行删除实例方法");
|
||||
// delInstance();
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ContextClosedEvent event) {
|
||||
try{
|
||||
log.info("=======>删除实例");
|
||||
delInstance();
|
||||
// redisTemplate.delete("instanceIds");
|
||||
// redisTemplate.delete("instanceList");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.ecs;
|
||||
|
||||
import com.muyu.cloud.vehicle.gateway.aliyun.service.AliYunEcsService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
*删除实例方法
|
||||
*/
|
||||
@Log4j2
|
||||
@Component
|
||||
public class DeleteSample implements DisposableBean {
|
||||
|
||||
@Autowired
|
||||
private AliYunEcsService aliYunEcsService;
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
try{
|
||||
log.info("==========开始执行删除实例方法");
|
||||
Thread.sleep(10000);
|
||||
aliYunEcsService.deleteInstance();
|
||||
} catch (Exception e) {
|
||||
log.info("删除实例失败");
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
log.info("删除实例成功");
|
||||
}
|
||||
}
|
|
@ -1,59 +0,0 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.ecs;
|
||||
|
||||
import com.aliyun.ecs20140526.Client;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesRequest;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.aliyun.darabonba.env.EnvClient;
|
||||
import com.aliyun.teautil.Common;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 查询一台或多台实例的详细信息
|
||||
*/
|
||||
public class QueryInstance {
|
||||
|
||||
/**
|
||||
* <b>description</b> :
|
||||
* <p>使用AK&SK初始化账号Client</p>
|
||||
* @return Client
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public static Client createClient(String alibabaCloudAccessKeyId, String alibabaCloudAccessKeySecret) throws Exception{
|
||||
Config config = new Config()
|
||||
.setAccessKeyId("LTAI5t7Fnx2QLTYLSu9357wP")
|
||||
.setAccessKeySecret("3LOnydNZ25ytsTGczuSygElx0HJ6nN");
|
||||
config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
|
||||
return new Client(config);
|
||||
}
|
||||
|
||||
public static Client createClientWithSTS() throws Exception {
|
||||
Config config = new Config()
|
||||
.setAccessKeyId("LTAI5t7Fnx2QLTYLSu9357wP")
|
||||
.setAccessKeySecret("3LOnydNZ25ytsTGczuSygElx0HJ6nN")
|
||||
.setSecurityToken("token")
|
||||
.setType("sts");
|
||||
config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
|
||||
return new Client(config);
|
||||
}
|
||||
|
||||
public static void main(String[] args_) throws Exception{
|
||||
List<String> list = Arrays.asList(args_);
|
||||
|
||||
Client client = QueryInstance.createClient(EnvClient.getEnv("LTAI5t7Fnx2QLTYLSu9357wP"), EnvClient.getEnv("3LOnydNZ25ytsTGczuSygElx0HJ6nN"));
|
||||
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
|
||||
.setRegionId("cn-shanghai")
|
||||
.setInternetChargeType("PayByTraffic")
|
||||
.setInstanceChargeType("PostPaid");
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
DescribeInstancesResponse resp = client.describeInstancesWithOptions(describeInstancesRequest, runtime);
|
||||
com.aliyun.teaconsole.Client.log(Common.toJSONString(resp));
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,114 +1,57 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.ecs;
|
||||
|
||||
|
||||
import com.aliyun.ecs20140526.Client;
|
||||
import com.aliyun.ecs20140526.models.*;
|
||||
import com.aliyun.tea.*;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import com.muyu.cloud.vehicle.gateway.aliyun.domain.Instance;
|
||||
import com.muyu.cloud.vehicle.gateway.aliyun.service.AliYunEcsService;
|
||||
import com.muyu.cloud.vehicle.gateway.config.AliProperties;
|
||||
import com.muyu.cloud.vehicle.gateway.domain.AliInstance;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.apache.logging.log4j.CloseableThreadContext;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.data.redis.core.ListOperations;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 批量创建ECS实例
|
||||
* 创建实例
|
||||
*/
|
||||
|
||||
@Log4j2
|
||||
@Component
|
||||
public class Sample implements ApplicationRunner{
|
||||
|
||||
@Autowired
|
||||
private AliYunEcsService aliYunEcsService;
|
||||
|
||||
@Autowired
|
||||
private AliProperties aliProperties;
|
||||
|
||||
/**
|
||||
* <b>description</b> :
|
||||
* <p>使用AK&SK初始化账号Client</p>
|
||||
* @return Client
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public static Client createClient() throws Exception {
|
||||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||
Config config = new Config()
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||
.setAccessKeyId("LTAI5t7Fnx2QLTYLSu9357wP")
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||
.setAccessKeySecret("3LOnydNZ25ytsTGczuSygElx0HJ6nN");
|
||||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||
config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
|
||||
return new Client(config);
|
||||
}
|
||||
|
||||
public void creatInstance() throws Exception {
|
||||
|
||||
// 将命令行参数转换为字符串列表
|
||||
List<String> args = Arrays.asList();
|
||||
// 创建阿里云ECS客户端
|
||||
Client client = Sample.createClient();
|
||||
// 配置系统盘参数
|
||||
RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = new RunInstancesRequest.RunInstancesRequestSystemDisk()
|
||||
.setSize("20")// 设置系统盘大小为40GB
|
||||
.setCategory("cloud_essd");// 设置系统盘类型为cloud_essd
|
||||
// 创建创建实例请求对象并设置参数
|
||||
RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
|
||||
.setRegionId("cn-shanghai") // 设置地域ID
|
||||
.setImageId("m-uf66taa8r57ky0pg3e7s")// 设置镜像ID
|
||||
.setInstanceType("ecs.e-c1m1.large")// 设置实例类型
|
||||
.setSecurityGroupId("sg-uf6hyictocodexptlgiv")// 设置安全组ID
|
||||
.setVSwitchId("vsw-uf6ags5luz17qd6ckn2tb")// 设置虚拟交换机ID
|
||||
.setInstanceName("launch-advisor-20240929")// 设置实例名称
|
||||
.setInstanceChargeType("PostPaid")// 设置实例付费类型为后付费按量付费
|
||||
.setSystemDisk(systemDisk)// 设置系统盘配置
|
||||
.setHostName("root")// 设置主机名
|
||||
.setPassword("@ywt021900")// 设置实例密码
|
||||
.setAmount(1)// 设置创建实例的数量
|
||||
.setInternetChargeType("PayByTraffic")// 设置互联网计费类型为按流量计费
|
||||
.setInternetMaxBandwidthOut(1);// 设置互联网最大出带宽为1 Mbps
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
try {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
RunInstancesResponse runInstancesResponse = client.runInstancesWithOptions(runInstancesRequest, runtime);
|
||||
RunInstancesResponseBody body = runInstancesResponse.getBody();
|
||||
ArrayList<String> list = new ArrayList<>(body.getInstanceIdSets().getInstanceIdSet());
|
||||
log.info("实例ID:{}", list);
|
||||
|
||||
} 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 static DescribeInstancesResponse queryInstanceDetails(Client client) {
|
||||
//
|
||||
// //创建查询实例请求对象并设置参数
|
||||
// DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
|
||||
// return null;
|
||||
// }
|
||||
// @Autowired
|
||||
// private RedisService redisService;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
log.info("===============>开始批量创建实例");
|
||||
creatInstance();
|
||||
List<String> list;
|
||||
try{
|
||||
log.info("开始创建实例");
|
||||
list =aliYunEcsService.generateInstance(aliProperties.getAmount());
|
||||
} catch (Exception e) {
|
||||
log.info("创建实例失败");
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
log.info("创建实例成功");
|
||||
// redisService.setCacheList("instanceIds",list);
|
||||
try{
|
||||
Thread.sleep(6000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
List<AliInstance> aliInstances = aliYunEcsService.selectInstance(list);
|
||||
log.info("查询实例信息成功:{}",aliInstances);
|
||||
//将查询到的实例信息列表存储到redis中
|
||||
// redisService.setCacheList("instanceList",aliInstances);
|
||||
log.info("redis存储成功:{}",aliInstances);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,56 +0,0 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.ecs;//// This file is auto-generated, don't edit it. Thanks.
|
||||
//package com.muyu.cloud.vehicle.gateway.aliyun.ecs;
|
||||
//
|
||||
//import com.aliyun.tea.*;
|
||||
//
|
||||
///**
|
||||
// * 启动一台实例
|
||||
// */
|
||||
//public class StartInstance {
|
||||
//
|
||||
// /**
|
||||
// * <b>description</b> :
|
||||
// * <p>使用AK&SK初始化账号Client</p>
|
||||
// * @return Client
|
||||
// *
|
||||
// * @throws Exception
|
||||
// */
|
||||
// public static com.aliyun.ecs20140526.Client createClient() throws Exception {
|
||||
// // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||
// // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
|
||||
// com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
||||
// // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||
// .setAccessKeyId("LTAI5t7Fnx2QLTYLSu9357wP")
|
||||
// // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||
// .setAccessKeySecret("3LOnydNZ25ytsTGczuSygElx0HJ6nN");
|
||||
// // Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||
// config.endpoint = "ecs.cn-shanghai.aliyuncs.com";
|
||||
// return new com.aliyun.ecs20140526.Client(config);
|
||||
// }
|
||||
//
|
||||
// public static void main(String[] args_) throws Exception {
|
||||
// java.util.List<String> args = java.util.Arrays.asList(args_);
|
||||
// com.aliyun.ecs20140526.Client client = StartInstance.createClient();
|
||||
// com.aliyun.ecs20140526.models.StartInstanceRequest startInstanceRequest = new com.aliyun.ecs20140526.models.StartInstanceRequest();
|
||||
// com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||
// try {
|
||||
// // 复制代码运行请自行打印 API 的返回值
|
||||
// client.startInstanceWithOptions(startInstanceRequest, 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);
|
||||
// }
|
||||
// }
|
||||
//}
|
|
@ -1,10 +0,0 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.mapper;
|
||||
|
||||
import com.muyu.cloud.vehicle.gateway.aliyun.domain.VehicleConnection;
|
||||
import com.muyu.cloud.vehicle.gateway.aliyun.domain.req.VehicleConnectionReq;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface VehicleConnectionMapper {
|
||||
void addConnect(VehicleConnection vehicleConnection);
|
||||
}
|
|
@ -0,0 +1,194 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.service;
|
||||
|
||||
import com.aliyun.ecs20140526.Client;
|
||||
import com.aliyun.ecs20140526.models.*;
|
||||
import com.aliyun.tea.TeaException;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import com.muyu.cloud.vehicle.gateway.config.AliProperties;
|
||||
import com.muyu.cloud.vehicle.gateway.domain.AliInstance;
|
||||
import com.muyu.common.core.exception.ServiceException;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*ali业务层
|
||||
*/
|
||||
@Log4j2
|
||||
@Service
|
||||
public class AliYunEcsService {
|
||||
/**
|
||||
* 阿里云配置
|
||||
*/
|
||||
@Autowired
|
||||
private AliProperties aliProperties;
|
||||
/**
|
||||
* 阿里云客户端
|
||||
*/
|
||||
@Autowired
|
||||
private Client client;
|
||||
/**
|
||||
* redis缓存
|
||||
*/
|
||||
// @Autowired
|
||||
// private RedisService redisService;
|
||||
|
||||
/**
|
||||
* 生成实例
|
||||
*
|
||||
* @param amount 生成数量
|
||||
* @return 实例id集合
|
||||
*/
|
||||
public List<String> generateInstance(Integer amount) {
|
||||
// redisService.deleteObject("instanceIds");
|
||||
// redisService.deleteObject("instanceList");
|
||||
// 检查生成实例的数量是否有效
|
||||
if (amount == null || amount <= 0) {
|
||||
throw new ServiceException("生成数量不能小于1");
|
||||
}
|
||||
|
||||
// 初始化系统盘配置
|
||||
RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = new RunInstancesRequest.RunInstancesRequestSystemDisk();
|
||||
systemDisk.setSize("20");
|
||||
systemDisk.setCategory("cloud_essd");
|
||||
|
||||
// 创建创建实例请求对象并设置参数
|
||||
RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
|
||||
// 设置地域ID
|
||||
.setRegionId(aliProperties.getRegionId())
|
||||
// 设置镜像ID
|
||||
.setImageId(aliProperties.getImageId())
|
||||
// 设置实例规格类型
|
||||
.setInstanceType(aliProperties.getInstanceType())
|
||||
// 设置安全组ID
|
||||
.setSecurityGroupId(aliProperties.getSecurityGroupId())
|
||||
// 设置虚拟交换机ID
|
||||
.setVSwitchId(aliProperties.getSwitchId())
|
||||
// 设置实例名称
|
||||
.setInstanceName("server-mqtt")
|
||||
// 设置付费类型 按量付费
|
||||
.setInstanceChargeType("PostPaid")
|
||||
// 设置系统盘配置
|
||||
.setSystemDisk(systemDisk)
|
||||
// 设置用户名
|
||||
.setHostName("root")
|
||||
// 设置密码
|
||||
.setPassword("@ywt0219")
|
||||
// 设置要创建的实例数量
|
||||
.setAmount(amount)
|
||||
.setInternetChargeType("PayByTraffic")
|
||||
.setInternetMaxBandwidthOut(1);
|
||||
|
||||
// 创建运行时选项对象
|
||||
RuntimeOptions runtimeOptions = new RuntimeOptions();
|
||||
|
||||
// 尝试执行创建实例请求
|
||||
try {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
RunInstancesResponse runInstancesResponse = client.runInstancesWithOptions(runInstancesRequest, runtimeOptions);
|
||||
if (runInstancesResponse.getStatusCode() != 200) {
|
||||
throw new ServiceException("查询实例状态失败");
|
||||
}
|
||||
log.info("实例创建成功: {}", runInstancesResponse.getBody().getInstanceIdSets().instanceIdSet);
|
||||
RunInstancesResponseBody body = runInstancesResponse.getBody();
|
||||
RunInstancesResponseBody.RunInstancesResponseBodyInstanceIdSets instanceIdSets = body.getInstanceIdSets();
|
||||
return new ArrayList<>(instanceIdSets.instanceIdSet);
|
||||
} catch (Exception error) {
|
||||
log.error("创建阿里云实例报错:[{}]", error.getMessage());
|
||||
throw new ServiceException(error.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据实例id查询实例信息
|
||||
*
|
||||
* @param instanceIds 实例id集合
|
||||
*/
|
||||
public List<AliInstance> selectInstance(List<String> instanceIds) throws Exception {
|
||||
// 检查实例ID列表是否为空,如果为空则抛出异常
|
||||
if (instanceIds == null || instanceIds.isEmpty()) {
|
||||
throw new ServiceException("实例id不能为空");
|
||||
}
|
||||
// 创建查询实例的请求对象
|
||||
DescribeInstancesRequest request = new DescribeInstancesRequest()
|
||||
.setRegionId(aliProperties.getRegionId());
|
||||
|
||||
// 创建运行时选项对象,用于配置请求的额外参数
|
||||
RuntimeOptions runtimeOptions = new RuntimeOptions();
|
||||
List<AliInstance> aliInstances = new ArrayList<>();
|
||||
try {
|
||||
// 发送请求并获取响应对象
|
||||
DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(request, runtimeOptions);
|
||||
// 检查响应状态码,如果为200则表示查询失败,抛出异常
|
||||
if (describeInstancesResponse.getStatusCode() != 200) {
|
||||
throw new ServiceException("查询实例状态失败");
|
||||
}
|
||||
List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance> instance = describeInstancesResponse.getBody().getInstances().getInstance();
|
||||
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance bodyInstance : instance) {
|
||||
// 实例id
|
||||
String instanceId = bodyInstance.getInstanceId();
|
||||
log.info("实例id为:{}", instanceId);
|
||||
// ip地址
|
||||
String ipAddress = bodyInstance.getPublicIpAddress().getIpAddress().get(0);
|
||||
log.info("实例ip为:{}", ipAddress);
|
||||
// 实例状态
|
||||
String status = bodyInstance.getStatus();
|
||||
log.info("实例状态为:{}", status);
|
||||
AliInstance aliInstance = new AliInstance(instanceId, ipAddress, status);
|
||||
aliInstances.add(aliInstance);
|
||||
}
|
||||
log.info("查询成功");
|
||||
} catch (Exception e) {
|
||||
log.error("查询服务器实例错误:[{}]", e.getMessage(), e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return aliInstances;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除实例
|
||||
*/
|
||||
public void deleteInstance() throws Exception {
|
||||
DescribeInstancesRequest attributeRequest = new DescribeInstancesRequest();
|
||||
attributeRequest.setRegionId(aliProperties.getRegionId());
|
||||
|
||||
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(aliProperties.getRegionId())
|
||||
.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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.muyu.cloud.vehicle.gateway.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 阿里云配置
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "aliyun")
|
||||
public class AliProperties {
|
||||
|
||||
/**
|
||||
* Key
|
||||
*/
|
||||
private String accessKeyId;
|
||||
/**
|
||||
* Secret
|
||||
*/
|
||||
private String accessKeySecret;
|
||||
/**
|
||||
* 地域
|
||||
*/
|
||||
private String endpoint;
|
||||
/**
|
||||
* 地域ID
|
||||
*/
|
||||
private String regionId;
|
||||
/**
|
||||
* 镜像ID
|
||||
*/
|
||||
private String imageId;
|
||||
/**
|
||||
* 实例规格类型
|
||||
*/
|
||||
private String instanceType;
|
||||
/**
|
||||
* 安全组ID
|
||||
*/
|
||||
private String securityGroupId;
|
||||
/**
|
||||
* 虚拟交换机ID
|
||||
*/
|
||||
private String switchId;
|
||||
/**
|
||||
* 实例数量
|
||||
*/
|
||||
private Integer amount;
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
package com.muyu.cloud.vehicle.gateway.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
/**
|
||||
* Redis配置类
|
||||
*/
|
||||
@Configuration
|
||||
public class RedisConfig {
|
||||
|
||||
@Bean
|
||||
public RedisTemplate<String,String> redisTemplate(RedisConnectionFactory redisConnectionFactory){
|
||||
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
|
||||
redisTemplate.setConnectionFactory(redisConnectionFactory);
|
||||
|
||||
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
|
||||
|
||||
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
|
||||
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
|
||||
return redisTemplate;
|
||||
}
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
package com.muyu.cloud.vehicle.gateway.config;
|
||||
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* rest配置类
|
||||
*/
|
||||
@Log4j2
|
||||
@Configuration
|
||||
public class RestTemplateConfig {
|
||||
@Bean
|
||||
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
|
||||
return new RestTemplate(factory);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建并配置客户端HTTP请求工厂实例。
|
||||
* 注意:这里设置了连接和读取数据的超时时间,分别设为15秒和5秒。
|
||||
* 这些值可以根据实际网络状况和应用需求进行调整。
|
||||
*
|
||||
* @return 配置好的客户端请求工厂对象
|
||||
*/
|
||||
@Bean
|
||||
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
|
||||
try{
|
||||
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
|
||||
//设置读取超时时间为5秒
|
||||
factory.setReadTimeout(5000);
|
||||
//设置连接超时时间为15秒
|
||||
factory.setConnectTimeout(15000);
|
||||
return factory;
|
||||
} catch (Exception e) {
|
||||
//处理创建工厂或设置超时时间可能出现的异常
|
||||
log.info("创建工厂失败:" + e.getMessage());
|
||||
throw new RuntimeException("初始化HTTP请求失败",e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.controller;
|
||||
package com.muyu.cloud.vehicle.gateway.controller;
|
||||
|
||||
|
||||
import com.alibaba.nacos.api.model.v2.Result;
|
||||
import com.muyu.cloud.vehicle.gateway.aliyun.domain.req.VehicleConnectionReq;
|
||||
import com.muyu.cloud.vehicle.gateway.aliyun.service.VehicleConnectionService;
|
||||
import com.muyu.cloud.vehicle.gateway.domain.req.VehicleConnectionReq;
|
||||
import com.muyu.cloud.vehicle.gateway.service.VehicleConnectionService;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.domain;
|
||||
package com.muyu.cloud.vehicle.gateway.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.domain;
|
||||
package com.muyu.cloud.vehicle.gateway.domain;
|
||||
|
||||
|
||||
import lombok.AllArgsConstructor;
|
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.domain;
|
||||
package com.muyu.cloud.vehicle.gateway.domain;
|
||||
|
||||
|
||||
import lombok.AllArgsConstructor;
|
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.domain;
|
||||
package com.muyu.cloud.vehicle.gateway.domain;
|
||||
|
||||
/**
|
||||
* 返回状态码
|
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.domain;
|
||||
package com.muyu.cloud.vehicle.gateway.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.domain;
|
||||
package com.muyu.cloud.vehicle.gateway.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.domain;
|
||||
package com.muyu.cloud.vehicle.gateway.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.domain;
|
||||
package com.muyu.cloud.vehicle.gateway.domain;
|
||||
|
||||
|
||||
import lombok.AllArgsConstructor;
|
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.domain.req;
|
||||
package com.muyu.cloud.vehicle.gateway.domain.req;
|
||||
|
||||
|
||||
import lombok.AllArgsConstructor;
|
|
@ -0,0 +1,9 @@
|
|||
package com.muyu.cloud.vehicle.gateway.mapper;
|
||||
|
||||
import com.muyu.cloud.vehicle.gateway.domain.VehicleConnection;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface VehicleConnectionMapper {
|
||||
void addConnect(VehicleConnection vehicleConnection);
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.service;
|
||||
package com.muyu.cloud.vehicle.gateway.service;
|
||||
|
||||
import com.muyu.cloud.vehicle.gateway.aliyun.domain.req.VehicleConnectionReq;
|
||||
import com.muyu.cloud.vehicle.gateway.domain.req.VehicleConnectionReq;
|
||||
|
||||
public interface VehicleConnectionService {
|
||||
/**
|
|
@ -1,9 +1,9 @@
|
|||
package com.muyu.cloud.vehicle.gateway.aliyun.service.impl;
|
||||
package com.muyu.cloud.vehicle.gateway.service.impl;
|
||||
|
||||
import com.muyu.cloud.vehicle.gateway.aliyun.domain.VehicleConnection;
|
||||
import com.muyu.cloud.vehicle.gateway.aliyun.domain.req.VehicleConnectionReq;
|
||||
import com.muyu.cloud.vehicle.gateway.aliyun.mapper.VehicleConnectionMapper;
|
||||
import com.muyu.cloud.vehicle.gateway.aliyun.service.VehicleConnectionService;
|
||||
import com.muyu.cloud.vehicle.gateway.domain.VehicleConnection;
|
||||
import com.muyu.cloud.vehicle.gateway.domain.req.VehicleConnectionReq;
|
||||
import com.muyu.cloud.vehicle.gateway.mapper.VehicleConnectionMapper;
|
||||
import com.muyu.cloud.vehicle.gateway.service.VehicleConnectionService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
@ -0,0 +1,130 @@
|
|||
//// This file is auto-generated, don't edit it. Thanks.
|
||||
//package com.muyu.cloud.vehicle.gateway.utils;
|
||||
//
|
||||
//import com.aliyun.ecs20140526.Client;
|
||||
//import com.aliyun.ecs20140526.models.DeleteInstancesRequest;
|
||||
//import com.aliyun.tea.*;
|
||||
//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.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.context.ApplicationListener;
|
||||
//import org.springframework.context.event.ContextClosedEvent;
|
||||
//import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//
|
||||
//import java.util.List;
|
||||
//
|
||||
///**
|
||||
// * 删除实例
|
||||
// */
|
||||
//@Log4j2
|
||||
//@Component
|
||||
//public class DelInstance{
|
||||
//
|
||||
// @Autowired
|
||||
// private StringRedisTemplate redisTemplate;
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * <b>description</b> :
|
||||
// * <p>使用AK&SK初始化账号Client</p>
|
||||
// * @return Client
|
||||
// *
|
||||
// * @throws Exception
|
||||
// */
|
||||
// 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("LTAI5t7Fnx2QLTYLSu9357wP")
|
||||
// // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||
// .setAccessKeySecret("3LOnydNZ25ytsTGczuSygElx0HJ6nN");
|
||||
// // Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||
// config.endpoint = "ecs.cn-shanghai.aliyuncs.com";
|
||||
// return new Client(config);
|
||||
// }
|
||||
//
|
||||
//
|
||||
//
|
||||
// public void delInstance() throws Exception{
|
||||
// //创建ECS客户端对象,用于后续调用ECS相关API
|
||||
// Client client = DelInstance.createClient();
|
||||
//
|
||||
// List<String> list = redisTemplate.opsForList().range("instanceIds",0,-1);
|
||||
//
|
||||
//// 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());
|
||||
//// }
|
||||
////
|
||||
//// System.out.println("Instance IDs"+list);
|
||||
//
|
||||
// // 创建删除实例请求对象,并设置请求参数
|
||||
// DeleteInstancesRequest deleteInstancesRequest = new DeleteInstancesRequest()
|
||||
// // 设置地域ID,指定要删除的实例所属的地域ID。
|
||||
// .setRegionId("cn-shanghai")
|
||||
// //设置DryRun为True,用于验证请求是否可以成功,但不实际执行删除操作
|
||||
// .setDryRun(false)
|
||||
// // 设置Force为true,表示即使实例有正在运行的任务,也强制删除实例
|
||||
// .setForce(true)
|
||||
// // 设置TerminateSubscription为true,表示删除按订阅付费的实例时终止订阅
|
||||
// .setTerminateSubscription(true)
|
||||
// // 设置实例ID列表,使用参数名称 instanceId,参数类型为数组。
|
||||
// .setInstanceId(list);
|
||||
// // 创建运行时选项对象,用于配置运行时的选项参数
|
||||
// RuntimeOptions runtime = new RuntimeOptions();
|
||||
// try{
|
||||
// //复制代码运行请自行打印 API 的返回值
|
||||
// client.deleteInstancesWithOptions(deleteInstancesRequest,runtime);
|
||||
// } catch (TeaException error) {
|
||||
// // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// // 错误 message
|
||||
// log.error(error.getMessage());
|
||||
// // 诊断地址
|
||||
// log.error(error.getData().get("Recommend"));
|
||||
// Common.assertAsString(error.message);
|
||||
// } catch (Exception _error){
|
||||
// TeaException error = new TeaException(_error.getMessage(), _error);
|
||||
// // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// // 错误 message
|
||||
// log.error(error.getMessage());
|
||||
// // 诊断地址
|
||||
// log.error(error.getData().get("Recommend"));
|
||||
// Common.assertAsString(error.message);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
//// @Override
|
||||
//// public void destroy() throws Exception {
|
||||
//// log.info("===============>开始执行删除实例方法");
|
||||
//// delInstance();
|
||||
//// }
|
||||
//
|
||||
//// @Override
|
||||
//// public void onApplicationEvent(ContextClosedEvent event) {
|
||||
//// try{
|
||||
//// log.info("=======>删除实例");
|
||||
//// delInstance();
|
||||
////// redisTemplate.delete("instanceIds");
|
||||
////// redisTemplate.delete("instanceList");
|
||||
//// } catch (Exception e) {
|
||||
//// throw new RuntimeException(e);
|
||||
//// }
|
||||
//// }
|
||||
//}
|
|
@ -0,0 +1,109 @@
|
|||
//package com.muyu.cloud.vehicle.gateway.utils;
|
||||
//
|
||||
//
|
||||
//import com.aliyun.ecs20140526.Client;
|
||||
//import com.aliyun.ecs20140526.models.*;
|
||||
//import com.aliyun.tea.*;
|
||||
//import com.aliyun.teaopenapi.models.Config;
|
||||
//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;
|
||||
//
|
||||
//import java.util.ArrayList;
|
||||
//import java.util.Arrays;
|
||||
//import java.util.List;
|
||||
//
|
||||
///**
|
||||
// * 批量创建ECS实例
|
||||
// */
|
||||
//@Log4j2
|
||||
//@Component
|
||||
//public class Sample {
|
||||
//
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * <b>description</b> :
|
||||
// * <p>使用AK&SK初始化账号Client</p>
|
||||
// * @return Client
|
||||
// *
|
||||
// * @throws Exception
|
||||
// */
|
||||
// public static Client createClient() throws Exception {
|
||||
// // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||
// Config config = new Config()
|
||||
// // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||
// .setAccessKeyId("LTAI5t7Fnx2QLTYLSu9357wP")
|
||||
// // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||
// .setAccessKeySecret("3LOnydNZ25ytsTGczuSygElx0HJ6nN");
|
||||
// // Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||
// config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
|
||||
// return new Client(config);
|
||||
// }
|
||||
//
|
||||
// public void creatInstance() throws Exception {
|
||||
//
|
||||
// // 将命令行参数转换为字符串列表
|
||||
// List<String> args = Arrays.asList();
|
||||
// // 创建阿里云ECS客户端
|
||||
// Client client = Sample.createClient();
|
||||
// // 配置系统盘参数
|
||||
// RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = new RunInstancesRequest.RunInstancesRequestSystemDisk()
|
||||
// .setSize("20")// 设置系统盘大小为40GB
|
||||
// .setCategory("cloud_essd");// 设置系统盘类型为cloud_essd
|
||||
// // 创建创建实例请求对象并设置参数
|
||||
// RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
|
||||
// .setRegionId("cn-shanghai") // 设置地域ID
|
||||
// .setImageId("m-uf66taa8r57ky0pg3e7s")// 设置镜像ID
|
||||
// .setInstanceType("ecs.e-c1m1.large")// 设置实例类型
|
||||
// .setSecurityGroupId("sg-uf6hyictocodexptlgiv")// 设置安全组ID
|
||||
// .setVSwitchId("vsw-uf6ags5luz17qd6ckn2tb")// 设置虚拟交换机ID
|
||||
// .setInstanceName("launch-advisor-20240929")// 设置实例名称
|
||||
// .setInstanceChargeType("PostPaid")// 设置实例付费类型为后付费按量付费
|
||||
// .setSystemDisk(systemDisk)// 设置系统盘配置
|
||||
// .setHostName("root")// 设置主机名
|
||||
// .setPassword("@ywt021900")// 设置实例密码
|
||||
// .setAmount(1)// 设置创建实例的数量
|
||||
// .setInternetChargeType("PayByTraffic")// 设置互联网计费类型为按流量计费
|
||||
// .setInternetMaxBandwidthOut(1);// 设置互联网最大出带宽为1 Mbps
|
||||
// RuntimeOptions runtime = new RuntimeOptions();
|
||||
// try {
|
||||
// // 复制代码运行请自行打印 API 的返回值
|
||||
// RunInstancesResponse runInstancesResponse = client.runInstancesWithOptions(runInstancesRequest, runtime);
|
||||
// RunInstancesResponseBody body = runInstancesResponse.getBody();
|
||||
// ArrayList<String> list = new ArrayList<>(body.getInstanceIdSets().getInstanceIdSet());
|
||||
// log.info("实例ID:{}", list);
|
||||
//
|
||||
// } 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 static DescribeInstancesResponse queryInstanceDetails(Client client) {
|
||||
////
|
||||
//// //创建查询实例请求对象并设置参数
|
||||
//// DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
|
||||
//// return null;
|
||||
//// }
|
||||
////
|
||||
//// @Override
|
||||
//// public void run(ApplicationArguments args) throws Exception {
|
||||
//// log.info("===============>开始批量创建实例");
|
||||
//// creatInstance();
|
||||
//// }
|
||||
//}
|
|
@ -7,7 +7,7 @@ nacos:
|
|||
addr: 47.101.49.53:8848
|
||||
user-name: nacos
|
||||
password: nacos
|
||||
namespace: seven
|
||||
namespace: warn
|
||||
# SPRING_AMQP_DESERIALIZATION_TRUST_ALL=true spring.amqp.deserialization.trust.all
|
||||
# Spring
|
||||
spring:
|
||||
|
@ -58,3 +58,13 @@ spring:
|
|||
logging:
|
||||
level:
|
||||
com.muyu.system.mapper: DEBUG
|
||||
aliyun:
|
||||
access-key-id: LTAI5t7Fnx2QLTYLSu9357wP
|
||||
access-key-secret: 3LOnydNZ25ytsTGczuSygElx0HJ6nN
|
||||
endpoint: ecs-cn-hangzhou.aliyuncs.com
|
||||
region-id: cn-shanghai
|
||||
image-id: m-uf66taa8r57ky0pg3e7s
|
||||
instance-type: ecs.t6-c1m1.large
|
||||
security-group-id: sg-uf6hyictocodexptlgiv
|
||||
switch-id: vsw-uf6ags5luz17qd6ckn2tb
|
||||
amount: 1
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.muyu.cloud.vehicle.gateway.aliyun.mapper.VehicleConnectionMapper">
|
||||
<mapper namespace="com.muyu.cloud.vehicle.gateway.mapper.VehicleConnectionMapper">
|
||||
|
||||
|
||||
<insert id="addConnect">
|
||||
|
|
Loading…
Reference in New Issue