build():添加车辆连接获取车辆信息存入数据库中,发送rabbitmq消息队列
parent
5d66ce181f
commit
98fa19923d
|
@ -107,6 +107,12 @@
|
||||||
<groupId>com.aliyun</groupId>
|
<groupId>com.aliyun</groupId>
|
||||||
<artifactId>cloudapi20160714</artifactId>
|
<artifactId>cloudapi20160714</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- rabbitmq模块 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>cloud-common-rabbit</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
|
|
|
@ -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("创建实例成功");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
|
|
||||||
|
}
|
|
@ -9,13 +9,12 @@ import com.aliyun.tea.TeaException;
|
||||||
import com.aliyun.teautil.Common;
|
import com.aliyun.teautil.Common;
|
||||||
import com.aliyun.teautil.models.RuntimeOptions;
|
import com.aliyun.teautil.models.RuntimeOptions;
|
||||||
import com.muyu.common.redis.service.RedisService;
|
import com.muyu.common.redis.service.RedisService;
|
||||||
|
import com.muyu.vehiclegateway.domain.Instance;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
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.boot.ApplicationArguments;
|
import org.springframework.boot.ApplicationArguments;
|
||||||
import org.springframework.boot.ApplicationRunner;
|
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 org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -23,7 +22,7 @@ import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ClassName GenerateInstance
|
* @ClassName GenerateInstance
|
||||||
* @Description 描述
|
* @Description 程序启动创建ECS服务器实例
|
||||||
* @Author YiBo.Liu
|
* @Author YiBo.Liu
|
||||||
* @Date 2024/9/28 19:39
|
* @Date 2024/9/28 19:39
|
||||||
*/
|
*/
|
||||||
|
@ -40,7 +39,7 @@ public class GenerateInstance implements ApplicationRunner {
|
||||||
* 启动自动创建实例
|
* 启动自动创建实例
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public List<String> generateInstance() throws Exception {
|
public List<Instance> generateInstance() throws Exception {
|
||||||
|
|
||||||
// 创建ECS客户端对象,用于后续调用ECS相关API
|
// 创建ECS客户端对象,用于后续调用ECS相关API
|
||||||
Client client = CreateClient.createClient();
|
Client client = CreateClient.createClient();
|
||||||
|
@ -52,7 +51,7 @@ public class GenerateInstance implements ApplicationRunner {
|
||||||
// 设置地域ID
|
// 设置地域ID
|
||||||
.setRegionId("cn-shanghai")
|
.setRegionId("cn-shanghai")
|
||||||
// 设置镜像ID
|
// 设置镜像ID
|
||||||
.setImageId("m-uf63thq7h50ng72jpoq2")
|
.setImageId("m-uf6f7atj16s3cjn9q5l8")
|
||||||
// 设置实例类型
|
// 设置实例类型
|
||||||
.setInstanceType("ecs.t6-c1m1.large")
|
.setInstanceType("ecs.t6-c1m1.large")
|
||||||
// 设置安全组ID
|
// 设置安全组ID
|
||||||
|
@ -105,27 +104,29 @@ public class GenerateInstance implements ApplicationRunner {
|
||||||
DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(describeInstancesRequest, runtimeOptions);
|
DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(describeInstancesRequest, runtimeOptions);
|
||||||
|
|
||||||
//提取实例ID集合
|
//提取实例ID集合
|
||||||
ArrayList<String> list = new ArrayList<>();
|
List<Instance> list = new ArrayList<>();
|
||||||
|
|
||||||
DescribeInstancesResponseBody body = describeInstancesResponse.getBody();
|
DescribeInstancesResponseBody body = describeInstancesResponse.getBody();
|
||||||
|
|
||||||
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : body.getInstances().getInstance()) {
|
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("实例id为:"+instance.getInstanceId());
|
||||||
log.info("实例ip为:"+instance.getPublicIpAddress().ipAddress.get(0));
|
log.info("实例ip为:"+instance.getPublicIpAddress().ipAddress.get(0));
|
||||||
log.info("实例状态为:"+instance.getStatus());
|
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;
|
return list;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(ApplicationArguments args) throws Exception {
|
public void run(ApplicationArguments args) throws Exception {
|
||||||
generateInstance();
|
generateInstance();
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
|
}
|
|
@ -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);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -40,6 +40,8 @@ spring:
|
||||||
file-extension: yml
|
file-extension: yml
|
||||||
# 共享配置
|
# 共享配置
|
||||||
shared-configs:
|
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}
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||||
# 系统环境Config共享配置
|
# 系统环境Config共享配置
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
|
||||||
|
<mapper namespace="com.muyu.vehiclegateway.mapper.ConnectMapper">
|
||||||
|
|
||||||
|
<insert id="addVehicle">
|
||||||
|
insert into connect(id,vehicle_vin,timestamp,nonce)
|
||||||
|
values (#{id},#{vehicleVin},#{timestamp},#{nonce})
|
||||||
|
</insert>
|
||||||
|
</mapper>
|
Loading…
Reference in New Issue