From 98fa19923db305d8d58b3d0067a0e4a5d9be3578 Mon Sep 17 00:00:00 2001
From: liuyibo <14460729+liuyibo12345@user.noreply.gitee.com>
Date: Sun, 6 Oct 2024 10:57:27 +0800
Subject: [PATCH] =?UTF-8?q?build():=E6=B7=BB=E5=8A=A0=E8=BD=A6=E8=BE=86?=
=?UTF-8?q?=E8=BF=9E=E6=8E=A5=E8=8E=B7=E5=8F=96=E8=BD=A6=E8=BE=86=E4=BF=A1?=
=?UTF-8?q?=E6=81=AF=E5=AD=98=E5=85=A5=E6=95=B0=E6=8D=AE=E5=BA=93=E4=B8=AD?=
=?UTF-8?q?=EF=BC=8C=E5=8F=91=E9=80=81rabbitmq=E6=B6=88=E6=81=AF=E9=98=9F?=
=?UTF-8?q?=E5=88=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../cloud-modules-vehicle-gateway/pom.xml | 6 ++
.../controller/ConnectController.java | 51 +++++++++++++++++
.../vehiclegateway/domain/ConnectMemory.java | 37 ++++++++++++
.../muyu/vehiclegateway/domain/Instance.java | 37 ++++++++++++
.../domain/MqttServerModel.java | 32 +++++++++++
.../domain/req/VehicleConnectionReq.java | 44 ++++++++++++++
.../instance/GenerateInstance.java | 21 +++----
.../vehiclegateway/mapper/ConnectMapper.java | 10 ++++
.../service/ConnectService.java | 21 +++++++
.../service/impl/ConnectServiceImpl.java | 57 +++++++++++++++++++
.../src/main/resources/bootstrap.yml | 2 +
.../main/resources/mapper/ConnectMapper.xml | 10 ++++
12 files changed, 318 insertions(+), 10 deletions(-)
create mode 100644 cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/controller/ConnectController.java
create mode 100644 cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/domain/ConnectMemory.java
create mode 100644 cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/domain/Instance.java
create mode 100644 cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/domain/MqttServerModel.java
create mode 100644 cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/domain/req/VehicleConnectionReq.java
create mode 100644 cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/mapper/ConnectMapper.java
create mode 100644 cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/service/ConnectService.java
create mode 100644 cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/service/impl/ConnectServiceImpl.java
create mode 100644 cloud-modules/cloud-modules-vehicle-gateway/src/main/resources/mapper/ConnectMapper.xml
diff --git a/cloud-modules/cloud-modules-vehicle-gateway/pom.xml b/cloud-modules/cloud-modules-vehicle-gateway/pom.xml
index 12b2946..2d95b3e 100644
--- a/cloud-modules/cloud-modules-vehicle-gateway/pom.xml
+++ b/cloud-modules/cloud-modules-vehicle-gateway/pom.xml
@@ -107,6 +107,12 @@
com.aliyun
cloudapi20160714
+
+
+
+ com.muyu
+ cloud-common-rabbit
+
diff --git a/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/controller/ConnectController.java b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/controller/ConnectController.java
new file mode 100644
index 0000000..7046819
--- /dev/null
+++ b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/controller/ConnectController.java
@@ -0,0 +1,51 @@
+package com.muyu.vehiclegateway.controller;
+
+import com.muyu.common.core.domain.Result;
+import com.muyu.vehiclegateway.domain.req.VehicleConnectionReq;
+import com.muyu.vehiclegateway.service.ConnectService;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import lombok.extern.log4j.Log4j2;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+/**
+ * @ClassName ConnectController
+ * @Description 连接车辆
+ * @Author YiBo.Liu
+ * @Date 2024/10/2 16:25
+ */
+@RequestMapping("/vehicleGateway")
+@RestController
+@Log4j2
+@Tag(name = "连接车辆控制层")
+public class ConnectController {
+
+ @Autowired
+ private ConnectService connectService;
+
+ /**
+ * 获取车辆信息
+ * @param vehicleConnectionReq
+ * @return
+ */
+ @PostMapping("/receiveMsg/connect")
+ private Result receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq){
+ log.info("=======>" + vehicleConnectionReq);
+ connectService.receiveMsg(vehicleConnectionReq);
+ return Result.success();
+ }
+
+ /**
+ * 创建ECS实例
+ * @throws Exception
+ */
+ @GetMapping("/createConnect")
+ private void createConnect() throws Exception {
+ connectService.createConnect();
+ log.info("创建实例成功");
+ }
+
+}
diff --git a/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/domain/ConnectMemory.java b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/domain/ConnectMemory.java
new file mode 100644
index 0000000..92dc9e3
--- /dev/null
+++ b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/domain/ConnectMemory.java
@@ -0,0 +1,37 @@
+package com.muyu.vehiclegateway.domain;
+
+import io.swagger.v3.oas.annotations.tags.Tag;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * @ClassName ConnectMemory
+ * @Description 内存使用情况
+ * @Author YiBo.Liu
+ * @Date 2024/10/4 11:02
+ */
+@Data
+@Builder
+@AllArgsConstructor
+@NoArgsConstructor
+@Tag(name = "服务器内存使用情况")
+public class ConnectMemory {
+
+ /**
+ * 节点ID
+ */
+ private String clusterId;
+
+ /**
+ * 所属IP
+ */
+ private String ipAddress;
+
+ /**
+ * 剩余连接数
+ */
+ private String remainingNum;
+
+}
diff --git a/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/domain/Instance.java b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/domain/Instance.java
new file mode 100644
index 0000000..205d23e
--- /dev/null
+++ b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/domain/Instance.java
@@ -0,0 +1,37 @@
+package com.muyu.vehiclegateway.domain;
+
+import io.swagger.v3.oas.annotations.tags.Tag;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * @ClassName Instance
+ * @Description 服务器数据
+ * @Author YiBo.Liu
+ * @Date 2024/9/30 20:42
+ */
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+@Builder
+@Tag(name = "服务器数据")
+public class Instance {
+
+ /**
+ * 服务器ID
+ */
+ private String instanceId;
+
+ /**
+ * 服务器IP
+ */
+ private String ipAddress;
+
+ /**
+ * 服务器状态
+ */
+ private String status;
+
+}
diff --git a/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/domain/MqttServerModel.java b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/domain/MqttServerModel.java
new file mode 100644
index 0000000..d254664
--- /dev/null
+++ b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/domain/MqttServerModel.java
@@ -0,0 +1,32 @@
+package com.muyu.vehiclegateway.domain;
+
+import io.swagger.v3.oas.annotations.tags.Tag;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * @ClassName MqttServerModel
+ * @Description 描述
+ * @Author YiBo.Liu
+ * @Date 2024/10/4 14:49
+ */
+@Data
+@Builder
+@AllArgsConstructor
+@NoArgsConstructor
+@Tag(name = "mqtt服务器模型")
+public class MqttServerModel {
+
+ /**
+ * MQTT服务节点
+ */
+ private String broker;
+
+ /**
+ * MQTT订阅主题
+ */
+ private String topic;
+
+}
diff --git a/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/domain/req/VehicleConnectionReq.java b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/domain/req/VehicleConnectionReq.java
new file mode 100644
index 0000000..ce28378
--- /dev/null
+++ b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/domain/req/VehicleConnectionReq.java
@@ -0,0 +1,44 @@
+package com.muyu.vehiclegateway.domain.req;
+
+import com.alibaba.fastjson.annotation.JSONField;
+import com.baomidou.mybatisplus.annotation.TableField;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * @ClassName VehicleConnectionReq
+ * @Description 描述
+ * @Author YiBo.Liu
+ * @Date 2024/10/2 16:10
+ */
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+@Builder
+public class VehicleConnectionReq {
+
+ /**
+ * id
+ */
+
+ private Integer id;
+
+ /**
+ * 车辆vin码
+ */
+ @JSONField(name = "vehicleVin")
+ private String vehicleVin;
+
+ /**
+ *时间戳
+ */
+ private String timestamp;
+
+ /**
+ * 随机字符串
+ */
+ private String nonce;
+
+}
diff --git a/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/instance/GenerateInstance.java b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/instance/GenerateInstance.java
index 4d5bd74..304b23d 100644
--- a/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/instance/GenerateInstance.java
+++ b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/instance/GenerateInstance.java
@@ -9,13 +9,12 @@ import com.aliyun.tea.TeaException;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;
import com.muyu.common.redis.service.RedisService;
+import com.muyu.vehiclegateway.domain.Instance;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
-import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
@@ -23,7 +22,7 @@ import java.util.List;
/**
* @ClassName GenerateInstance
- * @Description 描述
+ * @Description 程序启动创建ECS服务器实例
* @Author YiBo.Liu
* @Date 2024/9/28 19:39
*/
@@ -40,7 +39,7 @@ public class GenerateInstance implements ApplicationRunner {
* 启动自动创建实例
* @throws Exception
*/
- public List generateInstance() throws Exception {
+ public List generateInstance() throws Exception {
// 创建ECS客户端对象,用于后续调用ECS相关API
Client client = CreateClient.createClient();
@@ -52,7 +51,7 @@ public class GenerateInstance implements ApplicationRunner {
// 设置地域ID
.setRegionId("cn-shanghai")
// 设置镜像ID
- .setImageId("m-uf63thq7h50ng72jpoq2")
+ .setImageId("m-uf6f7atj16s3cjn9q5l8")
// 设置实例类型
.setInstanceType("ecs.t6-c1m1.large")
// 设置安全组ID
@@ -105,27 +104,29 @@ public class GenerateInstance implements ApplicationRunner {
DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(describeInstancesRequest, runtimeOptions);
//提取实例ID集合
- ArrayList list = new ArrayList<>();
+ List list = new ArrayList<>();
DescribeInstancesResponseBody body = describeInstancesResponse.getBody();
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : body.getInstances().getInstance()) {
- list.add(instance.getInstanceId());
- list.add(instance.getPublicIpAddress().ipAddress.get(0));
- list.add(instance.getStatus());
log.info("实例id为:"+instance.getInstanceId());
log.info("实例ip为:"+instance.getPublicIpAddress().ipAddress.get(0));
log.info("实例状态为:"+instance.getStatus());
+ Instance instance1 = new Instance(instance.getInstanceId(), instance.getPublicIpAddress().ipAddress.get(0), instance.getStatus());
+
+ list.add(instance1);
}
- redisService.setCacheList("shili",list);
+// Thread.sleep(20000);
+// redisService.setCacheList("aaa",list);
return list;
}
+
@Override
public void run(ApplicationArguments args) throws Exception {
generateInstance();
diff --git a/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/mapper/ConnectMapper.java b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/mapper/ConnectMapper.java
new file mode 100644
index 0000000..dbf5b18
--- /dev/null
+++ b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/mapper/ConnectMapper.java
@@ -0,0 +1,10 @@
+package com.muyu.vehiclegateway.mapper;
+
+import com.muyu.vehiclegateway.domain.req.VehicleConnectionReq;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface ConnectMapper {
+ void addVehicle(VehicleConnectionReq vehicleConnectionReq);
+
+}
diff --git a/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/service/ConnectService.java b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/service/ConnectService.java
new file mode 100644
index 0000000..03cbc4b
--- /dev/null
+++ b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/service/ConnectService.java
@@ -0,0 +1,21 @@
+package com.muyu.vehiclegateway.service;
+
+import com.muyu.vehiclegateway.domain.req.VehicleConnectionReq;
+
+public interface ConnectService {
+
+ /**
+ * 创建实例
+ * @throws Exception
+ */
+ void createConnect() throws Exception;
+
+ /**
+ * 获取车辆信息
+ * @param vehicleConnectionReq
+ * @return
+ */
+ void receiveMsg(VehicleConnectionReq vehicleConnectionReq);
+
+
+}
diff --git a/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/service/impl/ConnectServiceImpl.java b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/service/impl/ConnectServiceImpl.java
new file mode 100644
index 0000000..f4ab059
--- /dev/null
+++ b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/vehiclegateway/service/impl/ConnectServiceImpl.java
@@ -0,0 +1,57 @@
+package com.muyu.vehiclegateway.service.impl;
+
+import cn.hutool.json.JSON;
+import com.alibaba.fastjson2.JSONObject;
+import com.muyu.common.core.constant.GenConstants;
+import com.muyu.common.core.utils.uuid.UUID;
+import com.muyu.vehiclegateway.domain.req.VehicleConnectionReq;
+import com.muyu.vehiclegateway.instance.GenerateInstance;
+import com.muyu.vehiclegateway.mapper.ConnectMapper;
+import com.muyu.vehiclegateway.service.ConnectService;
+import org.springframework.amqp.core.Message;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import static io.lettuce.core.pubsub.PubSubOutput.Type.message;
+
+/**
+ * @ClassName ConnectServiceImpl
+ * @Description 描述
+ * @Author YiBo.Liu
+ * @Date 2024/10/2 16:25
+ */
+@Service
+public class ConnectServiceImpl implements ConnectService {
+
+ @Autowired
+ private RabbitTemplate rabbitTemplate;
+
+
+ @Autowired
+ private ConnectMapper connectMapper;
+
+ /**
+ * 创建实例
+ * @throws Exception
+ */
+ @Override
+ public void createConnect() throws Exception {
+ GenerateInstance generateInstance = new GenerateInstance();
+ generateInstance.generateInstance();
+ }
+
+ /**
+ * 获取车辆信息
+ * @param vehicleConnectionReq
+ * @return
+ */
+ @Override
+ public void receiveMsg(VehicleConnectionReq vehicleConnectionReq) {
+ rabbitTemplate.convertAndSend("GO_OFFLINE", vehicleConnectionReq.getVehicleVin(),message1 -> {
+ message1.getMessageProperties().setMessageId(UUID.fastUUID().toString());
+ return message1;
+ });
+ connectMapper.addVehicle(vehicleConnectionReq);
+ }
+}
diff --git a/cloud-modules/cloud-modules-vehicle-gateway/src/main/resources/bootstrap.yml b/cloud-modules/cloud-modules-vehicle-gateway/src/main/resources/bootstrap.yml
index 26e2e1b..0eb5252 100644
--- a/cloud-modules/cloud-modules-vehicle-gateway/src/main/resources/bootstrap.yml
+++ b/cloud-modules/cloud-modules-vehicle-gateway/src/main/resources/bootstrap.yml
@@ -40,6 +40,8 @@ spring:
file-extension: yml
# 共享配置
shared-configs:
+ # rabbitmq配置
+ - application-rabbit-config-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
# 系统共享配置
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
# 系统环境Config共享配置
diff --git a/cloud-modules/cloud-modules-vehicle-gateway/src/main/resources/mapper/ConnectMapper.xml b/cloud-modules/cloud-modules-vehicle-gateway/src/main/resources/mapper/ConnectMapper.xml
new file mode 100644
index 0000000..0d96adf
--- /dev/null
+++ b/cloud-modules/cloud-modules-vehicle-gateway/src/main/resources/mapper/ConnectMapper.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+ insert into connect(id,vehicle_vin,timestamp,nonce)
+ values (#{id},#{vehicleVin},#{timestamp},#{nonce})
+
+