优化代码一次
parent
99be16cde2
commit
3180012c6c
|
@ -0,0 +1,41 @@
|
||||||
|
package com.muyu.cargateway.Aliyun;
|
||||||
|
|
||||||
|
import com.aliyun.ecs20140526.Client;
|
||||||
|
import com.aliyun.teaopenapi.models.Config;
|
||||||
|
import com.muyu.cargateway.config.AliProperties;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Lenovo
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-10-07-18:40
|
||||||
|
* @ Version:1.0
|
||||||
|
* @ Description: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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,199 @@
|
||||||
|
package com.muyu.cargateway.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.cargateway.config.AliProperties;
|
||||||
|
import com.muyu.cargateway.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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Lenovo
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-10-07-18:43
|
||||||
|
* @ Version:1.0
|
||||||
|
* @ Description: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("40");
|
||||||
|
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("10160810@a")
|
||||||
|
// 设置要创建的实例数量
|
||||||
|
.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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package com.muyu.cargateway;
|
package com.muyu.cargateway;
|
||||||
|
|
||||||
import com.muyu.common.security.annotation.EnableCustomConfig;
|
import com.muyu.common.security.annotation.EnableCustomConfig;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
|
@ -13,6 +14,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
* @ Description:故障启动类
|
* @ Description:故障启动类
|
||||||
* @author Lenovo
|
* @author Lenovo
|
||||||
*/
|
*/
|
||||||
|
@Log4j2
|
||||||
@EnableCustomConfig
|
@EnableCustomConfig
|
||||||
@EnableFeignClients
|
@EnableFeignClients
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
package com.muyu.cargateway.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Lenovo
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-10-07-18:34
|
||||||
|
* @ Version:1.0
|
||||||
|
* @ Description:Ali配置类
|
||||||
|
*/
|
||||||
|
@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,24 +1,20 @@
|
||||||
package com.muyu.cargateway.config;
|
package com.muyu.cargateway.config;
|
||||||
|
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
import org.springframework.amqp.core.Binding;
|
import org.slf4j.Logger;
|
||||||
import org.springframework.amqp.core.BindingBuilder;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.amqp.core.Exchange;
|
import org.springframework.amqp.core.*;
|
||||||
import org.springframework.amqp.core.ExchangeBuilder;
|
|
||||||
import org.springframework.amqp.core.Queue;
|
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @author Lenovo
|
||||||
* @ Tool:IntelliJ IDEA
|
* @ Tool:IntelliJ IDEA
|
||||||
* @ Author:CHX
|
* @ Author:CHX
|
||||||
* @ Date:2024-10-04-15:13
|
* @ Date:2024-10-04-15:13
|
||||||
* @ Version:1.0
|
* @ Version:1.0
|
||||||
* @ Description:rabbitmq配置类
|
* @ Description:rabbitmq配置类
|
||||||
* @author Lenovo
|
|
||||||
*/
|
*/
|
||||||
@Log4j2
|
@Log4j2
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@ -91,13 +87,14 @@ public class RabbitmqConfig {
|
||||||
//ROUTINGKEY_EMAIL队列绑定交换机,指定routingKey
|
//ROUTINGKEY_EMAIL队列绑定交换机,指定routingKey
|
||||||
@Bean
|
@Bean
|
||||||
public Binding bindingQueueInformEmail(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue,
|
public Binding bindingQueueInformEmail(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue,
|
||||||
@Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange){
|
@Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
|
||||||
return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL).noargs();
|
return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL).noargs();
|
||||||
}
|
}
|
||||||
|
|
||||||
//ROUTINGKEY_SMS队列绑定交换机,指定routingKey
|
//ROUTINGKEY_SMS队列绑定交换机,指定routingKey
|
||||||
@Bean
|
@Bean
|
||||||
public Binding bindingRoutingKeySms(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
|
public Binding bindingRoutingKeySms(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
|
||||||
@Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange){
|
@Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
|
||||||
return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs();
|
return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
package com.muyu.cargateway.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;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @ Tool:IntelliJ IDEA
|
|
||||||
* @ Author:CHX
|
|
||||||
* @ Date:2024-10-04-15:12
|
|
||||||
* @ Version:1.0
|
|
||||||
* @ Description:Redis配置类
|
|
||||||
* @author Lenovo
|
|
||||||
*/
|
|
||||||
@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,48 +0,0 @@
|
||||||
package com.muyu.cargateway.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;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @ Tool:IntelliJ IDEA
|
|
||||||
* @ Author:CHX
|
|
||||||
* @ Date:2024-10-04-15:08
|
|
||||||
* @ Version:1.0
|
|
||||||
* @ Description:rest配置类
|
|
||||||
* @author Lenovo
|
|
||||||
*/
|
|
||||||
@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,6 +1,7 @@
|
||||||
package com.muyu.cargateway.domain;
|
package com.muyu.cargateway.domain;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@ -13,6 +14,7 @@ import lombok.NoArgsConstructor;
|
||||||
* @author Lenovo
|
* @author Lenovo
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
|
@Builder
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class AliInstance {
|
public class AliInstance {
|
||||||
|
@ -28,4 +30,5 @@ public class AliInstance {
|
||||||
* 实例状态
|
* 实例状态
|
||||||
*/
|
*/
|
||||||
private String status;
|
private String status;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.muyu.cargateway.instance;
|
||||||
|
|
||||||
|
import com.muyu.cargateway.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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-10-07-21:51
|
||||||
|
* @ Version:1.0
|
||||||
|
* @ Description:删除实例方法
|
||||||
|
* @author Lenovo
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
@Component
|
||||||
|
public class DeleteSample implements DisposableBean {
|
||||||
|
@Autowired
|
||||||
|
private AliYunEcsService aliYunEcsService;
|
||||||
|
@Override
|
||||||
|
public void destroy() {
|
||||||
|
try {
|
||||||
|
log.info("=================开始执行删除实例方法");
|
||||||
|
Thread.sleep(10000);
|
||||||
|
aliYunEcsService.deleteInstance();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.info("删除实例失败");
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
log.info("删除实例成功");
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,229 +1,71 @@
|
||||||
package com.muyu.cargateway.instance;
|
package com.muyu.cargateway.instance;
|
||||||
|
|
||||||
import com.aliyun.ecs20140526.Client;
|
import com.muyu.cargateway.Aliyun.service.AliYunEcsService;
|
||||||
import com.aliyun.ecs20140526.models.*;
|
import com.muyu.cargateway.config.AliProperties;
|
||||||
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.domain.AliInstance;
|
import com.muyu.cargateway.domain.AliInstance;
|
||||||
import com.muyu.common.redis.service.RedisService;
|
import com.muyu.common.redis.service.RedisService;
|
||||||
import lombok.extern.log4j.Log4j2;
|
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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.ApplicationArguments;
|
import org.springframework.boot.ApplicationArguments;
|
||||||
import org.springframework.boot.ApplicationRunner;
|
import org.springframework.boot.ApplicationRunner;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @author Lenovo
|
||||||
* @ Tool:IntelliJ IDEA
|
* @ Tool:IntelliJ IDEA
|
||||||
* @ Author:CHX
|
* @ Author:CHX
|
||||||
* @ Date:2024-09-28-21:10
|
* @ Date:2024-09-28-21:10
|
||||||
* @ Version:1.0
|
* @ Version:1.0
|
||||||
* @ Description:调用ali的类
|
* @ Description:调用ali的类
|
||||||
* @author Lenovo
|
|
||||||
*/
|
*/
|
||||||
@Log4j2
|
@Log4j2
|
||||||
@Component
|
@Component
|
||||||
public class Sample implements ApplicationRunner , DisposableBean {
|
public class Sample implements ApplicationRunner{
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AliYunEcsService aliYunEcsService;
|
||||||
|
@Autowired
|
||||||
|
private AliProperties aliProperties;
|
||||||
@Autowired
|
@Autowired
|
||||||
private RedisService redisService;
|
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-uf6ih0vnl5f51pquns11")
|
|
||||||
// 设置实例规格类型
|
|
||||||
.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 的返回值
|
|
||||||
RunInstancesResponse runInstancesResponse = client.runInstancesWithOptions(runInstancesRequest, runtimeOptions);
|
|
||||||
log.info("实例创建成功: {}", runInstancesResponse.getBody().getInstanceIdSets().instanceIdSet);
|
|
||||||
RunInstancesResponseBody body = runInstancesResponse.getBody();
|
|
||||||
RunInstancesResponseBody.RunInstancesResponseBodyInstanceIdSets instanceIdSets = body.getInstanceIdSets();
|
|
||||||
|
|
||||||
List<AliInstance> instanceList = new ArrayList<>();
|
|
||||||
List<String> instanceIds = new ArrayList<>();
|
|
||||||
//遍历实例ID
|
|
||||||
for (String string : instanceIdSets.instanceIdSet) {
|
|
||||||
instanceIds.add(string);
|
|
||||||
}
|
|
||||||
// 在获取实例详细信息时,确保获取所有必要的信息
|
|
||||||
DescribeInstancesResponse describeInstancesResponse = getInstances(client);
|
|
||||||
List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance> instances = describeInstancesResponse.getBody().getInstances().getInstance();
|
|
||||||
List<AliInstance> aliInstances = new ArrayList<>();
|
|
||||||
instanceIds = new ArrayList<>();
|
|
||||||
|
|
||||||
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : instances) {
|
|
||||||
String instanceId = instance.getInstanceId();
|
|
||||||
String ipAddress = instance.getPublicIpAddress().getIpAddress().get(0);
|
|
||||||
String status = instance.getStatus();
|
|
||||||
log.info("获取到的实例ID为{} IP为{} 状态为{}", instanceId, ipAddress, status);
|
|
||||||
aliInstances.add(new AliInstance(instanceId, ipAddress, status));
|
|
||||||
instanceIds.add(instanceId);
|
|
||||||
}
|
|
||||||
//实例信息集合(实例id,ip 状态) 存储在redis中
|
|
||||||
redisService.setCacheList("instanceList", aliInstances);
|
|
||||||
// 实例ID集合 存储在redis中
|
|
||||||
redisService.setCacheList("instanceIds", instanceIds);
|
|
||||||
|
|
||||||
} catch (TeaException error) {
|
|
||||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
|
||||||
// 错误 message
|
|
||||||
log.info(error.getMessage());
|
|
||||||
// 诊断地址
|
|
||||||
log.info(error.getData().get("Recommend"));
|
|
||||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
|
||||||
} catch (Exception _error) {
|
|
||||||
TeaException error = new TeaException(_error.getMessage(), _error);
|
|
||||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
|
||||||
// 错误 message
|
|
||||||
log.info(error.getMessage());
|
|
||||||
// 诊断地址
|
|
||||||
log.info(error.getData().get("Recommend"));
|
|
||||||
Common.assertAsString(error.message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 获取实例ID
|
|
||||||
* @param client
|
|
||||||
* @return string类型的实例ID集合
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
private DescribeInstancesResponse getInstances(Client client) throws Exception {
|
|
||||||
DescribeInstancesRequest request = new DescribeInstancesRequest();
|
|
||||||
request.setInstanceName("server-mqtt");
|
|
||||||
request.setRegionId("cn-shanghai");
|
|
||||||
|
|
||||||
RuntimeOptions runtimeOptions = new RuntimeOptions();
|
|
||||||
return client.describeInstancesWithOptions(request, runtimeOptions);
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 删除创建的实例
|
|
||||||
* @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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(ApplicationArguments args){
|
public void run(ApplicationArguments args) throws Exception {
|
||||||
|
List<String> list;
|
||||||
try {
|
try {
|
||||||
log.info("开始创建实例");
|
log.info("开始创建实例");
|
||||||
generateInstance();
|
list = aliYunEcsService.generateInstance(aliProperties.getAmount());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.info("创建实例失败");
|
log.info("创建实例失败");
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
log.info("创建实例成功");
|
log.info("创建实例成功");
|
||||||
}
|
redisService.setCacheList("instanceIds", list);
|
||||||
|
|
||||||
@Override
|
|
||||||
public void destroy(){
|
|
||||||
try {
|
try {
|
||||||
log.info("删除实例方法");
|
Thread.sleep(6000);
|
||||||
Thread.sleep(10000);
|
} catch (InterruptedException e) {
|
||||||
deleteSample();
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.info("删除实例失败");
|
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
log.info("删除实例成功");
|
List<AliInstance> aliInstances = aliYunEcsService.selectInstance(list);
|
||||||
|
log.info("查询实例信息成功:{}",aliInstances);
|
||||||
|
// 将查询到的实例信息列表存储到Redis中
|
||||||
|
redisService.setCacheList("instanceList", aliInstances);
|
||||||
|
log.info("redis存储成功:{}", aliInstances);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @Override
|
||||||
|
// public void destroy(){
|
||||||
|
// try {
|
||||||
|
// log.info("=================开始执行删除实例方法");
|
||||||
|
// aliYunEcsService.deleteInstance();
|
||||||
|
// redisService.deleteObject("instanceIds");
|
||||||
|
// redisService.deleteObject("instanceList");
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// log.info("删除实例失败");
|
||||||
|
// throw new RuntimeException(e);
|
||||||
|
// }
|
||||||
|
// log.info("删除实例成功");
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,5 @@ import java.util.List;
|
||||||
public interface CarOneClickOperationMapper {
|
public interface CarOneClickOperationMapper {
|
||||||
void addConnect(VehicleConnection vehicleConnection);
|
void addConnect(VehicleConnection vehicleConnection);
|
||||||
|
|
||||||
|
|
||||||
List<String> selectByVehicleVin(String vehicleVin);
|
List<String> selectByVehicleVin(String vehicleVin);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,89 +0,0 @@
|
||||||
//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,120 +0,0 @@
|
||||||
//package com.muyu.cargateway.test;
|
|
||||||
//
|
|
||||||
//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 java.util.Arrays;
|
|
||||||
//import java.util.List;
|
|
||||||
//
|
|
||||||
//
|
|
||||||
///**
|
|
||||||
// * @ Tool:IntelliJ IDEA
|
|
||||||
// * @ Author:CHX
|
|
||||||
// * @ Date:2024-09-27-23:38
|
|
||||||
// * @ Version:1.0
|
|
||||||
// * @ Description:调用ali的测试demo类
|
|
||||||
// * @author Lenovo
|
|
||||||
// */
|
|
||||||
//@Log4j2
|
|
||||||
//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("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);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * 本程序的入口点
|
|
||||||
// * 主要功能是创建阿里云ECS实例
|
|
||||||
// *
|
|
||||||
// * @param args 命令行参数
|
|
||||||
// * @throws Exception 如果执行过程中发生错误则抛出异常
|
|
||||||
// */
|
|
||||||
// public static void main(String[] args) throws Exception {
|
|
||||||
// // 将命令行参数转换为字符串列表
|
|
||||||
// List<String> arg = Arrays.asList(args);
|
|
||||||
//
|
|
||||||
// // 创建阿里云ECS客户端
|
|
||||||
// Client client = Sample.createClient();
|
|
||||||
//
|
|
||||||
// // 配置系统盘参数
|
|
||||||
// RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = new RunInstancesRequest.RunInstancesRequestSystemDisk()
|
|
||||||
// // 设置系统盘大小为20G
|
|
||||||
// .setSize("40")
|
|
||||||
// // 设置系统盘类型为cloud_essd
|
|
||||||
// .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);
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// // 创建运行时选项对象
|
|
||||||
// RuntimeOptions runtime = new RuntimeOptions();
|
|
||||||
//
|
|
||||||
// // 尝试执行创建实例请求
|
|
||||||
// try {
|
|
||||||
// // 复制代码运行请自行打印 API 的返回值
|
|
||||||
// client.runInstancesWithOptions(runInstancesRequest, 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"));
|
|
||||||
// Common.assertAsString(error.message);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//
|
|
|
@ -1,79 +0,0 @@
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -73,3 +73,13 @@ spring:
|
||||||
logging:
|
logging:
|
||||||
level:
|
level:
|
||||||
com.muyu.breakdown.mapper: DEBUG
|
com.muyu.breakdown.mapper: DEBUG
|
||||||
|
aliyun:
|
||||||
|
access-key-id: LTAI5tDH3FyRx4PRr6anx2TL
|
||||||
|
access-key-secret: xdQnX2tDattY50raNkUWmHzE2tondP
|
||||||
|
endpoint: ecs-cn-hangzhou.aliyuncs.com
|
||||||
|
region-id: cn-shanghai
|
||||||
|
image-id: m-uf6ih0vnl5f51pquns11
|
||||||
|
instance-type: ecs.t6-c1m1.large
|
||||||
|
security-group-id: sg-uf642d5u4ja5gsiitx8y
|
||||||
|
switch-id: vsw-uf66lifrkhxqc94xi06v3
|
||||||
|
amount: 1
|
||||||
|
|
Loading…
Reference in New Issue