fix():优化代码
parent
6eb46f53ae
commit
9952d8b24d
|
@ -5,9 +5,8 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.muyu</groupId>
|
<groupId>com.muyu</groupId>
|
||||||
<artifactId>cloud-server</artifactId>
|
<artifactId>cloud-modules</artifactId>
|
||||||
<version>3.6.3</version>
|
<version>3.6.3</version>
|
||||||
<relativePath>../../../pom.xml</relativePath>
|
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>enterpise-client</artifactId>
|
<artifactId>enterpise-client</artifactId>
|
||||||
|
|
|
@ -5,9 +5,8 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.muyu</groupId>
|
<groupId>com.muyu</groupId>
|
||||||
<artifactId>cloud-server</artifactId>
|
<artifactId>cloud-modules</artifactId>
|
||||||
<version>3.6.3</version>
|
<version>3.6.3</version>
|
||||||
<relativePath>../../../pom.xml</relativePath>
|
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>enterpise-common</artifactId>
|
<artifactId>enterpise-common</artifactId>
|
||||||
|
|
|
@ -5,9 +5,8 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.muyu</groupId>
|
<groupId>com.muyu</groupId>
|
||||||
<artifactId>cloud-server</artifactId>
|
<artifactId>cloud-modules</artifactId>
|
||||||
<version>3.6.3</version>
|
<version>3.6.3</version>
|
||||||
<relativePath>../../../pom.xml</relativePath>
|
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>enterpise-remote</artifactId>
|
<artifactId>enterpise-remote</artifactId>
|
||||||
|
|
|
@ -5,9 +5,8 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.muyu</groupId>
|
<groupId>com.muyu</groupId>
|
||||||
<artifactId>cloud-server</artifactId>
|
<artifactId>cloud-modules</artifactId>
|
||||||
<version>3.6.3</version>
|
<version>3.6.3</version>
|
||||||
<relativePath>../../../pom.xml</relativePath>
|
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>enterpise-service</artifactId>
|
<artifactId>enterpise-service</artifactId>
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
|
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<description>
|
<description>
|
||||||
cloud-modules-enterprise
|
cloud-modules-enterprise闫🐕和🐂鹏
|
||||||
</description>
|
</description>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
|
|
@ -9,10 +9,10 @@
|
||||||
<version>3.6.3</version>
|
<version>3.6.3</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>cloud-modules-vehicleGateway</artifactId>
|
<artifactId>cloud-modules-vehicle-gateway</artifactId>
|
||||||
|
|
||||||
<description>
|
<description>
|
||||||
cloud-modules-vehicleGateway车辆网关模块
|
cloud-modules-vehicle-gateway车辆网关模块
|
||||||
</description>
|
</description>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
|
@ -0,0 +1,108 @@
|
||||||
|
//package com.muyu.vehicle.gateway;
|
||||||
|
//
|
||||||
|
//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.teautil.models.RuntimeOptions;
|
||||||
|
//import lombok.extern.log4j.Log4j2;
|
||||||
|
//import org.springframework.beans.factory.DisposableBean;
|
||||||
|
//import org.springframework.stereotype.Component;
|
||||||
|
//
|
||||||
|
//import java.util.ArrayList;
|
||||||
|
//
|
||||||
|
///**
|
||||||
|
// * 清除阿里云ECS实例
|
||||||
|
// * @ClassName ClearInstance
|
||||||
|
// * @author LiHD
|
||||||
|
// * @Date 2024/9/28 22:29
|
||||||
|
// */
|
||||||
|
//@Component
|
||||||
|
//@Log4j2
|
||||||
|
//public class ClearInstance implements DisposableBean {
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * <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("LTAI5tM4LSerzWQ9wUPXgybq")
|
||||||
|
// // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||||
|
// .setAccessKeySecret("hkiPr0QwkKMZauhXucgg1SDriXUW0Q");
|
||||||
|
// // Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||||
|
// config.endpoint = "ecs.cn-beijing.aliyuncs.com";
|
||||||
|
// return new com.aliyun.ecs20140526.Client(config);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public static void clearInstance() throws Exception {
|
||||||
|
//
|
||||||
|
// // 创建ECS客户端对象,用于后续调用ECS相关API
|
||||||
|
// com.aliyun.ecs20140526.Client client = ClearInstance.createClient();
|
||||||
|
//
|
||||||
|
// DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
|
||||||
|
// .setRegionId("cn-beijing");
|
||||||
|
//
|
||||||
|
// //创建运行时选择对象,用于配置运行时的选项参数
|
||||||
|
// 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());
|
||||||
|
// }
|
||||||
|
// log.info("Instance IDs: " + list);
|
||||||
|
// // 创建删除实例请求对象,并设置请求参数
|
||||||
|
// com.aliyun.ecs20140526.models.DeleteInstancesRequest deleteInstancesRequest = new com.aliyun.ecs20140526.models.DeleteInstancesRequest()
|
||||||
|
// // 设置地域ID,指定删除实例的地域
|
||||||
|
// .setRegionId("cn-beijing")
|
||||||
|
// // 设置DryRun为true,用于验证请求是否可以成功,但不实际执行删除操作
|
||||||
|
// .setDryRun(false)
|
||||||
|
// // 设置Force为true,表示即使实例有正在运行的任务,也强制删除实例
|
||||||
|
// .setForce(true)
|
||||||
|
// // 设置TerminateSubscription为true,表示删除按订阅付费的实例时终止订阅
|
||||||
|
// .setTerminateSubscription(true)
|
||||||
|
// .setInstanceId(list);
|
||||||
|
//
|
||||||
|
// // 创建运行时选项对象,用于配置运行时的选项参数
|
||||||
|
// com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||||
|
// try {
|
||||||
|
// // 复制代码运行请自行打印 API 的返回值
|
||||||
|
// 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() throws Exception {
|
||||||
|
// log.info("开始执行删除实例方法");
|
||||||
|
// clearInstance();
|
||||||
|
// log.info("删除实例方法执行完毕");
|
||||||
|
// }
|
||||||
|
//}
|
|
@ -0,0 +1,108 @@
|
||||||
|
//package com.muyu.vehicle.gateway;
|
||||||
|
//
|
||||||
|
//import com.aliyun.ecs20140526.models.RunInstancesResponse;
|
||||||
|
//import com.aliyun.ecs20140526.models.RunInstancesResponseBody;
|
||||||
|
//import com.aliyun.tea.TeaException;
|
||||||
|
//import lombok.extern.log4j.Log4j2;
|
||||||
|
//import org.springframework.boot.ApplicationArguments;
|
||||||
|
//import org.springframework.boot.ApplicationRunner;
|
||||||
|
//import org.springframework.stereotype.Component;
|
||||||
|
//
|
||||||
|
//import java.util.ArrayList;
|
||||||
|
//
|
||||||
|
///**
|
||||||
|
// * 创建阿里云ECS实例
|
||||||
|
// * @className Sample
|
||||||
|
// * @author LiHD
|
||||||
|
// * @date 2024/9/28 19:02
|
||||||
|
// */
|
||||||
|
//@Component
|
||||||
|
//@Log4j2
|
||||||
|
//public class CreateInstance implements ApplicationRunner{
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * <b>description</b> :
|
||||||
|
// * <p>使用AK&SK初始化账号Client</p>
|
||||||
|
// * @return Client
|
||||||
|
// *
|
||||||
|
// * @throws Exception
|
||||||
|
// */
|
||||||
|
// public static com.aliyun.ecs20140526.Client createClient() throws Exception {
|
||||||
|
// // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||||
|
// com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
||||||
|
// // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||||
|
// .setAccessKeyId("LTAI5tM4LSerzWQ9wUPXgybq")
|
||||||
|
// // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||||
|
// .setAccessKeySecret("hkiPr0QwkKMZauhXucgg1SDriXUW0Q");
|
||||||
|
// // Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||||
|
// config.endpoint = "ecs.cn-beijing.aliyuncs.com";
|
||||||
|
// return new com.aliyun.ecs20140526.Client(config);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 本程序的入口点
|
||||||
|
// * 主要功能是创建阿里云ECS实例
|
||||||
|
// *
|
||||||
|
// * @throws Exception 如果执行过程中发生错误则抛出异常
|
||||||
|
// */
|
||||||
|
// public static void sample() throws Exception {
|
||||||
|
//
|
||||||
|
// // 创建阿里云ECS客户端
|
||||||
|
// com.aliyun.ecs20140526.Client client = CreateInstance.createClient();
|
||||||
|
//
|
||||||
|
// // 配置系统盘参数
|
||||||
|
// com.aliyun.ecs20140526.models.RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = new com.aliyun.ecs20140526.models.RunInstancesRequest.RunInstancesRequestSystemDisk()
|
||||||
|
// .setSize("40") // 设置系统盘大小为40GB
|
||||||
|
// .setCategory("cloud_essd"); // 设置系统盘类型为cloud_essd
|
||||||
|
//
|
||||||
|
// // 创建创建实例请求对象并设置参数
|
||||||
|
// com.aliyun.ecs20140526.models.RunInstancesRequest runInstancesRequest = new com.aliyun.ecs20140526.models.RunInstancesRequest()
|
||||||
|
// .setRegionId("cn-beijing") // 设置地域ID
|
||||||
|
// .setImageId("m-2ze801i45fclmf1qwes5") // 设置镜像ID
|
||||||
|
// .setInstanceType("ecs.e-c1m1.large") // 设置实例类型
|
||||||
|
// .setSecurityGroupId("sg-2ze63acc0bas9vjm5gtv") // 设置安全组ID
|
||||||
|
// .setVSwitchId("vsw-2ze028qk4xpwk3i0ggket") // 设置虚拟交换机ID
|
||||||
|
// .setInstanceName("server-mqtt") // 设置实例名称
|
||||||
|
// .setInstanceChargeType("PostPaid") // 设置实例付费类型为后付费按量付费
|
||||||
|
// .setSystemDisk(systemDisk) // 设置系统盘配置
|
||||||
|
// .setHostName("root") // 设置主机名
|
||||||
|
// .setPassword("Lhd.123456") // 设置实例密码
|
||||||
|
// .setAmount(2) // 设置创建实例的数量
|
||||||
|
// .setInternetChargeType("PayByTraffic") // 设置互联网计费类型为按流量计费
|
||||||
|
// .setInternetMaxBandwidthOut(5); // 设置最大出带宽为5Mbps
|
||||||
|
//
|
||||||
|
// // 创建运行时选项对象
|
||||||
|
// com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.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
|
||||||
|
// 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"));
|
||||||
|
// com.aliyun.teautil.Common.assertAsString(error.message);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public void run(ApplicationArguments args) throws Exception {
|
||||||
|
// log.info("开始执行创建实例方法");
|
||||||
|
// sample();
|
||||||
|
// log.info("创建实例方法执行完毕");
|
||||||
|
// }
|
||||||
|
//}
|
|
@ -0,0 +1,56 @@
|
||||||
|
//package com.muyu.vehicle.gateway;
|
||||||
|
//
|
||||||
|
//import lombok.extern.log4j.Log4j2;
|
||||||
|
//import lombok.extern.slf4j.Slf4j;
|
||||||
|
//import org.eclipse.paho.client.mqttv3.*;
|
||||||
|
//
|
||||||
|
//@Log4j2
|
||||||
|
//@Slf4j
|
||||||
|
//public class MqttPublishSample {
|
||||||
|
//
|
||||||
|
// public static void main(String[] args) {
|
||||||
|
//
|
||||||
|
// // 定义一个用于MQTT消息发布的示例程序
|
||||||
|
// String topic = "vehicleGateway";
|
||||||
|
// String content = "Message from MqttPublishSample";
|
||||||
|
// int qos = 2;
|
||||||
|
// String broker = "tcp://123.57.152.124:1883";
|
||||||
|
// String clientId = "JavaSample";
|
||||||
|
//
|
||||||
|
// try {
|
||||||
|
// // 创建MQTT客户端并连接到指定的broker
|
||||||
|
// MqttClient sampleClient = new MqttClient(broker, clientId);
|
||||||
|
// MqttConnectOptions connOpts = new MqttConnectOptions();
|
||||||
|
// connOpts.setCleanSession(true);
|
||||||
|
// System.out.println("Connecting to broker: "+broker);
|
||||||
|
// sampleClient.connect(connOpts);
|
||||||
|
// sampleClient.subscribe(topic,0);
|
||||||
|
// // 设置回调处理
|
||||||
|
// sampleClient.setCallback(new MqttCallback() {
|
||||||
|
// @Override
|
||||||
|
// public void connectionLost(Throwable throwable) {
|
||||||
|
// // 连接丢失的处理
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
|
||||||
|
// // 消息到达的处理
|
||||||
|
// System.out.println(new String(mqttMessage.getPayload()));
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
|
||||||
|
// // 消息发送完成的处理
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// } catch(MqttException me) {
|
||||||
|
// // 处理MQTT异常
|
||||||
|
// System.out.println("reason "+me.getReasonCode());
|
||||||
|
// System.out.println("msg "+me.getMessage());
|
||||||
|
// System.out.println("loc "+me.getLocalizedMessage());
|
||||||
|
// System.out.println("cause "+me.getCause());
|
||||||
|
// System.out.println("excep "+me);
|
||||||
|
// me.printStackTrace();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.muyu.vehicle.gateway.aliyun.controller;
|
||||||
|
|
||||||
|
import com.muyu.common.core.web.controller.BaseController;
|
||||||
|
import com.muyu.vehicle.gateway.aliyun.service.InstanceService;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.annotation.PreDestroy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ECS实例 控制器
|
||||||
|
*
|
||||||
|
* @author LiHD
|
||||||
|
* @ClassName InstanceController
|
||||||
|
* @Date 2024/9/29 22:04
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/instance")
|
||||||
|
public class InstanceController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private InstanceService instanceService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建实例
|
||||||
|
*/
|
||||||
|
@PostConstruct
|
||||||
|
public void createInstance(){
|
||||||
|
instanceService.createInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清理实例
|
||||||
|
*/
|
||||||
|
@PreDestroy
|
||||||
|
public void cleanInstance(){
|
||||||
|
instanceService.cleanInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.muyu.vehicle.gateway.aliyun.domain;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.experimental.SuperBuilder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ECS实例信息
|
||||||
|
*
|
||||||
|
* @author LiHD
|
||||||
|
* @ClassName Instance
|
||||||
|
* @Date 2024/9/29 22:04
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@SuperBuilder
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@TableName("instance")
|
||||||
|
public class Instance {
|
||||||
|
|
||||||
|
@TableId(value = "id", type = IdType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.muyu.vehicle.gateway.aliyun.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.muyu.vehicle.gateway.aliyun.domain.Instance;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ECS实例 数据层
|
||||||
|
*
|
||||||
|
* @author LiHD
|
||||||
|
* @ClassName InstanceMapper
|
||||||
|
* @Date 2024/9/29 20:19
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface InstanceMapper {
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.muyu.vehicle.gateway.aliyun.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.muyu.vehicle.gateway.aliyun.domain.Instance;
|
||||||
|
import org.apache.catalina.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ECS实例服务
|
||||||
|
*
|
||||||
|
* @author LiHD
|
||||||
|
* @ClassName InstanceService
|
||||||
|
* @Date 2024/9/29 20:22
|
||||||
|
*/
|
||||||
|
|
||||||
|
public interface InstanceService {
|
||||||
|
/**
|
||||||
|
* 创建ECS实例
|
||||||
|
*/
|
||||||
|
void createInstance();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清理ECS实例
|
||||||
|
*/
|
||||||
|
void cleanInstance();
|
||||||
|
}
|
|
@ -0,0 +1,199 @@
|
||||||
|
package com.muyu.vehicle.gateway.aliyun.service.impl;
|
||||||
|
|
||||||
|
import com.aliyun.ecs20140526.Client;
|
||||||
|
import com.aliyun.ecs20140526.models.*;
|
||||||
|
import com.aliyun.tea.TeaException;
|
||||||
|
import com.aliyun.teautil.models.RuntimeOptions;
|
||||||
|
import com.muyu.vehicle.gateway.aliyun.mapper.InstanceMapper;
|
||||||
|
import com.muyu.vehicle.gateway.aliyun.service.InstanceService;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ECS实例服务 实现类
|
||||||
|
*
|
||||||
|
* @author LiHD
|
||||||
|
* @ClassName InstanceServiceImpl
|
||||||
|
* @Date 2024/9/29 20:23
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
@Service
|
||||||
|
public class InstanceServiceImpl implements InstanceService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private InstanceMapper instanceMapper;
|
||||||
|
|
||||||
|
// 阿里云ECS相关参数
|
||||||
|
private final static String AccessKeyId = "LTAI5tM4LSerzWQ9wUPXgybq"; // 请替换成您的 AccessKeyId
|
||||||
|
private final static String AccessKeySecret = "hkiPr0QwkKMZauhXucgg1SDriXUW0Q"; // 请替换成您的 AccessKeySecret
|
||||||
|
private final static String Endpoint = "ecs.cn-beijing.aliyuncs.com"; // 请替换成您的访问域名
|
||||||
|
private final static String Size = "40"; // 请替换成您的实例系统盘大小,单位为GB
|
||||||
|
private final static String Category = "cloud_essd"; // 请替换成您的实例系统盘类型,可选值:cloud_efficiency、cloud_ssd、cloud_essd
|
||||||
|
private final static String RegionId = "cn-beijing"; // 请替换成您的实例所在地域ID
|
||||||
|
private final static String ImageId = "m-2ze801i45fclmf1qwes5"; // 请替换成您的实例镜像ID
|
||||||
|
private final static String InstanceType = "ecs.e-c1m1.large"; // 请替换成您的实例类型
|
||||||
|
private final static String SecurityGroupId = "sg-2ze63acc0bas9vjm5gtv"; // 请替换成您的安全组ID
|
||||||
|
private final static String VSwitchId = "vsw-2ze028qk4xpwk3i0ggket"; // 请替换成您的虚拟交换机ID
|
||||||
|
private final static String InstanceName = "server-mqtt"; // 请替换成您的实例名称
|
||||||
|
private final static String InstanceChargeType = "PostPaid"; // 请替换成您的实例付费类型,可选值:PrePaid、PostPaid
|
||||||
|
private final static String HostName = "root"; // 请替换成您的主机名
|
||||||
|
private final static String Password = "Lhd.123456"; // 请替换成您的实例密码
|
||||||
|
private final static Integer Amount = 1; // 请替换成您要创建的实例数量
|
||||||
|
private final static String InternetChargeType = "PayByTraffic"; // 请替换成您的实例网络计费类型,可选值:PayByBandwidth、PayByTraffic
|
||||||
|
private final static Integer InternetMaxBandwidthOut = 5; // 请替换成您的实例网络出带宽,单位为Mbps
|
||||||
|
|
||||||
|
/**
|
||||||
|
* description
|
||||||
|
* 用AK&SK初始化账号Client
|
||||||
|
* @returnClient
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public com.aliyun.ecs20140526.Client createClient() throws Exception {
|
||||||
|
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||||
|
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
||||||
|
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||||
|
.setAccessKeyId(AccessKeyId)
|
||||||
|
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||||
|
.setAccessKeySecret(AccessKeySecret);
|
||||||
|
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||||
|
config.endpoint = Endpoint;
|
||||||
|
return new com.aliyun.ecs20140526.Client(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建ECS实例
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void createInstance(){
|
||||||
|
log.info("创建ECS实例");
|
||||||
|
// 创建阿里云ECS客户端
|
||||||
|
Client client = null;
|
||||||
|
try {
|
||||||
|
client = this.createClient();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
// 配置系统盘参数
|
||||||
|
com.aliyun.ecs20140526.models.RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk =
|
||||||
|
new com.aliyun.ecs20140526.models.RunInstancesRequest.RunInstancesRequestSystemDisk()
|
||||||
|
.setSize(Size) // 设置系统盘大小
|
||||||
|
.setCategory(Category);// 设置系统盘类型
|
||||||
|
// 创建创建实例请求对象并设置参数
|
||||||
|
com.aliyun.ecs20140526.models.RunInstancesRequest runInstancesRequest =
|
||||||
|
new com.aliyun.ecs20140526.models.RunInstancesRequest()
|
||||||
|
.setRegionId(RegionId) // 设置实例所在地域ID
|
||||||
|
.setImageId(ImageId) // 设置实例镜像ID
|
||||||
|
.setInstanceType(InstanceType) // 设置实例类型
|
||||||
|
.setSecurityGroupId(SecurityGroupId) // 设置安全组ID
|
||||||
|
.setVSwitchId(VSwitchId) // 设置虚拟交换机ID
|
||||||
|
.setInstanceName(InstanceName) // 设置实例名称
|
||||||
|
.setInstanceChargeType(InstanceChargeType) // 设置实例付费类型为后付费按量付费
|
||||||
|
.setSystemDisk(systemDisk) // 设置系统盘配置
|
||||||
|
.setHostName(HostName) // 设置主机名
|
||||||
|
.setPassword(Password) // 设置实例密码
|
||||||
|
.setAmount(Amount) // 设置创建实例的数量
|
||||||
|
.setInternetChargeType(InternetChargeType) // 设置互联网计费类型为按流量计费
|
||||||
|
.setInternetMaxBandwidthOut(InternetMaxBandwidthOut); // 设置最大出带宽为5Mbps
|
||||||
|
|
||||||
|
// 创建运行时选项对象
|
||||||
|
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.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
|
||||||
|
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"));
|
||||||
|
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除ECS实例
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void cleanInstance() {
|
||||||
|
log.info("清理ECS实例");
|
||||||
|
// 创建ECS客户端对象,用于后续调用ECS相关API
|
||||||
|
Client client = null;
|
||||||
|
try {
|
||||||
|
client = this.createClient();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
|
||||||
|
.setRegionId(RegionId);
|
||||||
|
|
||||||
|
//创建运行时选择对象,用于配置运行时的选项参数
|
||||||
|
RuntimeOptions runtimeOptions = new RuntimeOptions();
|
||||||
|
|
||||||
|
//获取实例列表
|
||||||
|
DescribeInstancesResponse describeInstancesResponse = null;
|
||||||
|
try {
|
||||||
|
describeInstancesResponse = client.describeInstancesWithOptions(describeInstancesRequest, runtimeOptions);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
//提取实例ID集合
|
||||||
|
ArrayList<String> list = new ArrayList<>();
|
||||||
|
DescribeInstancesResponseBody body = describeInstancesResponse.getBody();
|
||||||
|
|
||||||
|
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : body.getInstances().getInstance()) {
|
||||||
|
list.add(instance.getInstanceId());
|
||||||
|
}
|
||||||
|
log.info("清除的实例ID:{}", list);
|
||||||
|
// 创建删除实例请求对象,并设置请求参数
|
||||||
|
com.aliyun.ecs20140526.models.DeleteInstancesRequest deleteInstancesRequest = new com.aliyun.ecs20140526.models.DeleteInstancesRequest()
|
||||||
|
// 设置地域ID,指定删除实例的地域
|
||||||
|
.setRegionId(RegionId)
|
||||||
|
// 设置DryRun为true,用于验证请求是否可以成功,但不实际执行删除操作
|
||||||
|
.setDryRun(false)
|
||||||
|
// 设置Force为true,表示即使实例有正在运行的任务,也强制删除实例
|
||||||
|
.setForce(true)
|
||||||
|
// 设置TerminateSubscription为true,表示删除按订阅付费的实例时终止订阅
|
||||||
|
.setTerminateSubscription(true)
|
||||||
|
.setInstanceId(list);
|
||||||
|
|
||||||
|
// 创建运行时选项对象,用于配置运行时的选项参数
|
||||||
|
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||||
|
try {
|
||||||
|
// 复制代码运行请自行打印 API 的返回值
|
||||||
|
DeleteInstancesResponse deleteInstancesResponse = 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,9 +1,9 @@
|
||||||
package com.muyu.vehicleGateway.vehicle;
|
package com.muyu.vehicle.gateway.vehicle;
|
||||||
|
|
||||||
import com.muyu.vehicleGateway.vehicle.model.VehicleData;
|
import com.muyu.vehicle.gateway.vehicle.model.VehicleData;
|
||||||
import com.muyu.vehicleGateway.vehicle.thread.VehicleThread;
|
import com.muyu.vehicle.gateway.vehicle.thread.VehicleThread;
|
||||||
import com.muyu.vehicleGateway.web.domain.VehicleInfo;
|
import com.muyu.vehicle.gateway.web.domain.VehicleInfo;
|
||||||
import com.muyu.vehicleGateway.web.domain.model.PositionModel;
|
import com.muyu.vehicle.gateway.web.domain.model.PositionModel;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
|
@ -1,7 +1,7 @@
|
||||||
package com.muyu.vehicleGateway.vehicle.model;
|
package com.muyu.vehicle.gateway.vehicle.model;
|
||||||
|
|
||||||
import com.muyu.vehicleGateway.web.domain.VehicleInfo;
|
import com.muyu.vehicle.gateway.web.domain.VehicleInfo;
|
||||||
import com.muyu.vehicleGateway.web.util.VehicleUtils;
|
import com.muyu.vehicle.gateway.web.util.VehicleUtils;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
|
@ -1,6 +1,6 @@
|
||||||
package com.muyu.vehicleGateway.vehicle.thread;
|
package com.muyu.vehicle.gateway.vehicle.thread;
|
||||||
|
|
||||||
import com.muyu.vehicleGateway.vehicle.VehicleInstance;
|
import com.muyu.vehicle.gateway.vehicle.VehicleInstance;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package com.muyu.vehicleGateway.web.domain;
|
package com.muyu.vehicle.gateway.web.domain;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.*;
|
import com.baomidou.mybatisplus.annotation.*;
|
||||||
import com.muyu.vehicleGateway.vehicle.VehicleInstance;
|
import com.muyu.vehicle.gateway.vehicle.VehicleInstance;
|
||||||
import com.muyu.vehicleGateway.vehicle.model.VehicleData;
|
import com.muyu.vehicle.gateway.vehicle.model.VehicleData;
|
||||||
import com.muyu.vehicleGateway.web.util.VehicleUtils;
|
import com.muyu.vehicle.gateway.web.util.VehicleUtils;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.muyu.vehicleGateway.web.domain.model;
|
package com.muyu.vehicle.gateway.web.domain.model;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
|
@ -1,7 +1,7 @@
|
||||||
package com.muyu.vehicleGateway.web.mapper;
|
package com.muyu.vehicle.gateway.web.mapper;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
import com.muyu.vehicleGateway.web.domain.VehicleInfo;
|
import com.muyu.vehicle.gateway.web.domain.VehicleInfo;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
/**
|
/**
|
|
@ -1,8 +1,8 @@
|
||||||
package com.muyu.vehicleGateway.web.service.impl;
|
package com.muyu.vehicle.gateway.web.service.impl;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.muyu.vehicleGateway.web.domain.VehicleInfo;
|
import com.muyu.vehicle.gateway.web.domain.VehicleInfo;
|
||||||
import com.muyu.vehicleGateway.web.mapper.VehicleInfoMapper;
|
import com.muyu.vehicle.gateway.web.mapper.VehicleInfoMapper;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
|
@ -1,6 +1,6 @@
|
||||||
package com.muyu.vehicleGateway.web.util;
|
package com.muyu.vehicle.gateway.web.util;
|
||||||
|
|
||||||
import com.muyu.vehicleGateway.web.domain.model.PositionModel;
|
import com.muyu.vehicle.gateway.web.domain.model.PositionModel;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
|
@ -1,108 +0,0 @@
|
||||||
package com.muyu.vehicleGateway;
|
|
||||||
|
|
||||||
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.teautil.models.RuntimeOptions;
|
|
||||||
import lombok.extern.log4j.Log4j2;
|
|
||||||
import org.springframework.beans.factory.DisposableBean;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 清除阿里云ECS实例
|
|
||||||
* @ClassName ClearInstance
|
|
||||||
* @author LiHD
|
|
||||||
* @Date 2024/9/28 22:29
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
@Log4j2
|
|
||||||
public class ClearInstance implements DisposableBean {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <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("LTAI5tM4LSerzWQ9wUPXgybq")
|
|
||||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
|
||||||
.setAccessKeySecret("hkiPr0QwkKMZauhXucgg1SDriXUW0Q");
|
|
||||||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
|
||||||
config.endpoint = "ecs.cn-beijing.aliyuncs.com";
|
|
||||||
return new com.aliyun.ecs20140526.Client(config);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void clearInstance() throws Exception {
|
|
||||||
|
|
||||||
// 创建ECS客户端对象,用于后续调用ECS相关API
|
|
||||||
com.aliyun.ecs20140526.Client client = ClearInstance.createClient();
|
|
||||||
|
|
||||||
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
|
|
||||||
.setRegionId("cn-beijing");
|
|
||||||
|
|
||||||
//创建运行时选择对象,用于配置运行时的选项参数
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
log.info("Instance IDs: " + list);
|
|
||||||
// 创建删除实例请求对象,并设置请求参数
|
|
||||||
com.aliyun.ecs20140526.models.DeleteInstancesRequest deleteInstancesRequest = new com.aliyun.ecs20140526.models.DeleteInstancesRequest()
|
|
||||||
// 设置地域ID,指定删除实例的地域
|
|
||||||
.setRegionId("cn-beijing")
|
|
||||||
// 设置DryRun为true,用于验证请求是否可以成功,但不实际执行删除操作
|
|
||||||
.setDryRun(false)
|
|
||||||
// 设置Force为true,表示即使实例有正在运行的任务,也强制删除实例
|
|
||||||
.setForce(true)
|
|
||||||
// 设置TerminateSubscription为true,表示删除按订阅付费的实例时终止订阅
|
|
||||||
.setTerminateSubscription(true)
|
|
||||||
.setInstanceId(list);
|
|
||||||
|
|
||||||
// 创建运行时选项对象,用于配置运行时的选项参数
|
|
||||||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
|
||||||
try {
|
|
||||||
// 复制代码运行请自行打印 API 的返回值
|
|
||||||
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() throws Exception {
|
|
||||||
log.info("开始执行删除实例方法");
|
|
||||||
clearInstance();
|
|
||||||
log.info("删除实例方法执行完毕");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,101 +0,0 @@
|
||||||
package com.muyu.vehicleGateway;
|
|
||||||
|
|
||||||
import com.aliyun.tea.TeaException;
|
|
||||||
import lombok.extern.log4j.Log4j2;
|
|
||||||
import org.springframework.boot.ApplicationArguments;
|
|
||||||
import org.springframework.boot.ApplicationRunner;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建阿里云ECS实例
|
|
||||||
* @className Sample
|
|
||||||
* @author LiHD
|
|
||||||
* @date 2024/9/28 19:02
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
@Log4j2
|
|
||||||
public class CreateInstance implements ApplicationRunner{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <b>description</b> :
|
|
||||||
* <p>使用AK&SK初始化账号Client</p>
|
|
||||||
* @return Client
|
|
||||||
*
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public static com.aliyun.ecs20140526.Client createClient() throws Exception {
|
|
||||||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
|
||||||
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
|
||||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
|
||||||
.setAccessKeyId("LTAI5tM4LSerzWQ9wUPXgybq")
|
|
||||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
|
||||||
.setAccessKeySecret("hkiPr0QwkKMZauhXucgg1SDriXUW0Q");
|
|
||||||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
|
||||||
config.endpoint = "ecs.cn-beijing.aliyuncs.com";
|
|
||||||
return new com.aliyun.ecs20140526.Client(config);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 本程序的入口点
|
|
||||||
* 主要功能是创建阿里云ECS实例
|
|
||||||
*
|
|
||||||
* @throws Exception 如果执行过程中发生错误则抛出异常
|
|
||||||
*/
|
|
||||||
public static void sample() throws Exception {
|
|
||||||
|
|
||||||
// 创建阿里云ECS客户端
|
|
||||||
com.aliyun.ecs20140526.Client client = CreateInstance.createClient();
|
|
||||||
|
|
||||||
// 配置系统盘参数
|
|
||||||
com.aliyun.ecs20140526.models.RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = new com.aliyun.ecs20140526.models.RunInstancesRequest.RunInstancesRequestSystemDisk()
|
|
||||||
.setSize("40") // 设置系统盘大小为40GB
|
|
||||||
.setCategory("cloud_essd"); // 设置系统盘类型为cloud_essd
|
|
||||||
|
|
||||||
// 创建创建实例请求对象并设置参数
|
|
||||||
com.aliyun.ecs20140526.models.RunInstancesRequest runInstancesRequest = new com.aliyun.ecs20140526.models.RunInstancesRequest()
|
|
||||||
.setRegionId("cn-beijing") // 设置地域ID
|
|
||||||
.setImageId("m-2ze801i45fclmf1qwes5") // 设置镜像ID
|
|
||||||
.setInstanceType("ecs.e-c1m1.large") // 设置实例类型
|
|
||||||
.setSecurityGroupId("sg-2ze63acc0bas9vjm5gtv") // 设置安全组ID
|
|
||||||
.setVSwitchId("vsw-2ze028qk4xpwk3i0ggket") // 设置虚拟交换机ID
|
|
||||||
.setInstanceName("server-mqtt") // 设置实例名称
|
|
||||||
.setInstanceChargeType("PostPaid") // 设置实例付费类型为后付费按量付费
|
|
||||||
.setSystemDisk(systemDisk) // 设置系统盘配置
|
|
||||||
.setHostName("root") // 设置主机名
|
|
||||||
.setPassword("Lhd.123456") // 设置实例密码
|
|
||||||
.setAmount(1) // 设置创建实例的数量
|
|
||||||
.setInternetChargeType("PayByTraffic") // 设置互联网计费类型为按流量计费
|
|
||||||
.setInternetMaxBandwidthOut(5); // 设置最大出带宽为5Mbps
|
|
||||||
|
|
||||||
// 创建运行时选项对象
|
|
||||||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.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"));
|
|
||||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run(ApplicationArguments args) throws Exception {
|
|
||||||
log.info("开始执行创建实例方法");
|
|
||||||
sample();
|
|
||||||
log.info("创建实例方法执行完毕");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,56 +0,0 @@
|
||||||
package com.muyu.vehicleGateway;
|
|
||||||
|
|
||||||
import lombok.extern.log4j.Log4j2;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.eclipse.paho.client.mqttv3.*;
|
|
||||||
|
|
||||||
@Log4j2
|
|
||||||
@Slf4j
|
|
||||||
public class MqttPublishSample {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
|
|
||||||
// 定义一个用于MQTT消息发布的示例程序
|
|
||||||
String topic = "vehicleGateway";
|
|
||||||
String content = "Message from MqttPublishSample";
|
|
||||||
int qos = 2;
|
|
||||||
String broker = "tcp://123.57.152.124:1883";
|
|
||||||
String clientId = "JavaSample";
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 创建MQTT客户端并连接到指定的broker
|
|
||||||
MqttClient sampleClient = new MqttClient(broker, clientId);
|
|
||||||
MqttConnectOptions connOpts = new MqttConnectOptions();
|
|
||||||
connOpts.setCleanSession(true);
|
|
||||||
System.out.println("Connecting to broker: "+broker);
|
|
||||||
sampleClient.connect(connOpts);
|
|
||||||
sampleClient.subscribe(topic,0);
|
|
||||||
// 设置回调处理
|
|
||||||
sampleClient.setCallback(new MqttCallback() {
|
|
||||||
@Override
|
|
||||||
public void connectionLost(Throwable throwable) {
|
|
||||||
// 连接丢失的处理
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
|
|
||||||
// 消息到达的处理
|
|
||||||
System.out.println(new String(mqttMessage.getPayload()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
|
|
||||||
// 消息发送完成的处理
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch(MqttException me) {
|
|
||||||
// 处理MQTT异常
|
|
||||||
System.out.println("reason "+me.getReasonCode());
|
|
||||||
System.out.println("msg "+me.getMessage());
|
|
||||||
System.out.println("loc "+me.getLocalizedMessage());
|
|
||||||
System.out.println("cause "+me.getCause());
|
|
||||||
System.out.println("excep "+me);
|
|
||||||
me.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -14,6 +14,7 @@
|
||||||
<module>cloud-modules-file</module>
|
<module>cloud-modules-file</module>
|
||||||
<module>cloud-vx</module>
|
<module>cloud-vx</module>
|
||||||
<module>cloud-modules-enterprise</module>
|
<module>cloud-modules-enterprise</module>
|
||||||
|
<module>cloud-modules-vehicle-gateway</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<artifactId>cloud-modules</artifactId>
|
<artifactId>cloud-modules</artifactId>
|
||||||
|
|
Loading…
Reference in New Issue