优化代码第三次

dev.car.gateway
冷调 2024-10-10 16:08:01 +08:00
parent 6b571dfddc
commit 35fa5b2ed9
21 changed files with 182 additions and 186 deletions

View File

@ -7,20 +7,21 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-10-07-21:51
* @ Version1.0
* @ Description
* @author Lenovo
*/
@Log4j2
@Component
public class DeleteSample implements DisposableBean {
@Autowired
private AliYunEcsService aliYunEcsService;
@Override
public void destroy() {
public void destroy() {
try {
log.info("===开始执行删除实例方法");
Thread.sleep(10000);

View File

@ -21,7 +21,7 @@ import java.util.List;
*/
@Log4j2
@Component
public class Sample implements ApplicationRunner{
public class Sample implements ApplicationRunner {
@Autowired
private AliYunEcsService aliYunEcsService;
@ -46,7 +46,7 @@ public class Sample implements ApplicationRunner{
throw new RuntimeException(e);
}
List<AliInstance> aliInstances = aliYunEcsService.selectInstance(list);
log.info("================查询实例信息成功:{}",aliInstances);
log.info("================查询实例信息成功:{}", aliInstances);
}
// @Override

View File

@ -7,12 +7,12 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-09-17-15:00
* @ Version1.0
* @ Description
* @author Lenovo
*/
@Log4j2
@EnableCustomConfig

View File

@ -19,29 +19,49 @@ import org.springframework.context.annotation.Configuration;
@Log4j2
@Configuration
public class RabbitmqConfig {
// 日志
/**
*
*/
private static final Logger logger = LoggerFactory.getLogger(RabbitmqConfig.class);
/**
*
*/
public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
/**
*
*/
public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
/**
*
*/
public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";
/**
* key
* 线vin
*/
public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
/**
*
*/
public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
/**
* 线vin
*/
public static final String QUEUE_INFORM_SEND = "queue_inform_send";
/**
* saas
*/
public static final String QUEUE_INFORM_SAAS = "queue_inform_saas";
/**
* key 线
*/
public static final String ROUTINGKEY_EMAIL = "inform.#.email.#";
/**
* key
* key
*/
public static final String ROUTINGKEY_SMS = "inform.#.sms.#";
/**
* key 线
*/
public static final String ROUTINGKEY_SEND = "inform.#.send.#";
/**
* key saas
*/
public static final String ROUTINGKEY_SAAS = "inform.#.saas.#";
/**
* ,
@ -58,7 +78,9 @@ public class RabbitmqConfig {
}
}
// 声明QUEUE_INFORM_EMAIL队列
/**
* QUEUE_INFORM_EMAIL
*/
@Bean(QUEUE_INFORM_EMAIL)
public Queue queueInformEmail() {
try {
@ -71,7 +93,9 @@ public class RabbitmqConfig {
}
}
// 声明QUEUE_INFORM_SMS队列
/**
* QUEUE_INFORM_SMS
*/
@Bean(QUEUE_INFORM_SMS)
public Queue queueInformSms() {
try {
@ -84,17 +108,82 @@ public class RabbitmqConfig {
}
}
//ROUTINGKEY_EMAIL队列绑定交换机指定routingKey
/**
* QUEUE_INFORM_SEND
*/
@Bean(QUEUE_INFORM_SEND)
public Queue queueInformSend() {
try {
Queue queue = new Queue(QUEUE_INFORM_SEND);
log.info("创建的队列为: {}", QUEUE_INFORM_SEND);
return queue;
} catch (Exception e) {
log.error("创建该: {} 队列失败", QUEUE_INFORM_SEND, e);
throw e;
}
}
/**
* QUEUE_INFORM_SAAS
*/
@Bean(QUEUE_INFORM_SAAS)
public Queue queueInformSaas() {
try {
Queue queue = new Queue(QUEUE_INFORM_SAAS);
log.info("创建的队列为: {}", QUEUE_INFORM_SAAS);
return queue;
} catch (Exception e) {
log.error("创建该: {} 队列失败", QUEUE_INFORM_SAAS, e);
throw e;
}
}
/**
* QUEUE_INFORM_EMAILroutingKey ROUTINGKEY_EMAIL
*
* @param queue QUEUE_INFORM_EMAIL
* @param exchange EXCHANGE_TOPICS_INFORM
*/
@Bean
public Binding bindingQueueInformEmail(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue,
@Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL).noargs();
}
//ROUTINGKEY_SMS队列绑定交换机指定routingKey
/**
* QUEUE_INFORM_SMS routingKey ROUTINGKEY_SMS
*
* @param queue QUEUE_INFORM_SMS
* @param exchange EXCHANGE_TOPICS_INFORM
*/
@Bean
public Binding bindingRoutingKeySms(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
@Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs();
}
/**
* QUEUE_INFORM_SENDroutingKey ROUTINGKEY_SEND
*
* @param queue QUEUE_INFORM_SEND
* @param exchange EXCHANGE_TOPICS_INFORM
*/
@Bean
public Binding bindingRoutingKeySend(@Qualifier(QUEUE_INFORM_SEND) Queue queue,
@Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SEND).noargs();
}
/**
* QUEUE_INFORM_SAASroutingKey ROUTINGKEY_SAAS
*
* @param queue QUEUE_INFORM_SAAS
* @param exchange EXCHANGE_TOPICS_INFORM
*/
@Bean
public Binding bindingRoutingKeySaas(@Qualifier(QUEUE_INFORM_SAAS) Queue queue,
@Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SAAS).noargs();
}
}

View File

@ -13,29 +13,30 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-09-26-20:14
* @ Version1.0
* @ Description
* @author Lenovo
*/
@Log4j2
@RestController
@RequestMapping("/vehicleGateway")
@Tag(name = "连接车辆控制层")
public class CarOneClickOperationController {
@Autowired
@Autowired
private CarOneClickOperationService carOneClickOperationService;
/**
* http
*
* @param vehicleConnectionReq
* @return
*/
@PostMapping("/receiveMsg/connect")
public Result<MqttServerModel> receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq){
log.info(">"+vehicleConnectionReq);
public Result<MqttServerModel> receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq) {
log.info("======================" + vehicleConnectionReq);
return carOneClickOperationService.getConnect(vehicleConnectionReq);
}

View File

@ -6,12 +6,12 @@ import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-09-30-9:33
* @ Version1.0
* @ Description
* @author Lenovo
*/
@Data
@Builder
@ -19,7 +19,7 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
public class AliInstance {
/**
*ID
* ID
*/
private String instanceId;
/**

View File

@ -5,12 +5,12 @@ import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-09-28-16:37
* @ Version1.0
* @ Description
* @author Lenovo
*/
@Data
@AllArgsConstructor

View File

@ -5,12 +5,12 @@ import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-10-04-15:16
* @ Version1.0
* @ Description
* @author Lenovo
*/
@Data
@AllArgsConstructor

View File

@ -1,93 +0,0 @@
package com.muyu.car.gateway.domain;
/**
*
*
* @author ruoyi
*/
public class HttpStatus {
/**
*
*/
public static final int SUCCESS = 200;
/**
*
*/
public static final int CREATED = 201;
/**
*
*/
public static final int ACCEPTED = 202;
/**
*
*/
public static final int NO_CONTENT = 204;
/**
*
*/
public static final int MOVED_PERM = 301;
/**
*
*/
public static final int SEE_OTHER = 303;
/**
*
*/
public static final int NOT_MODIFIED = 304;
/**
*
*/
public static final int BAD_REQUEST = 400;
/**
*
*/
public static final int UNAUTHORIZED = 401;
/**
* 访
*/
public static final int FORBIDDEN = 403;
/**
*
*/
public static final int NOT_FOUND = 404;
/**
* http
*/
public static final int BAD_METHOD = 405;
/**
*
*/
public static final int CONFLICT = 409;
/**
*
*/
public static final int UNSUPPORTED_TYPE = 415;
/**
*
*/
public static final int ERROR = 500;
/**
*
*/
public static final int NOT_IMPLEMENTED = 501;
/**
*
*/
public static final int WARN = 601;
}

View File

@ -9,23 +9,23 @@ import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-09-27-20:56
* @ Version1.0
* @ Description
* @author Lenovo
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName(value="server_config")
@TableName(value = "server_config")
public class ServerConfig {
/**
*
*/
@TableId(value = "id",type = IdType.AUTO)
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* id

View File

@ -5,12 +5,12 @@ import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-10-06-11:05
* @ Version1.0
* @ Description
* @author Lenovo
*/
@Data
@AllArgsConstructor

View File

@ -5,12 +5,12 @@ import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-10-03-10:10
* @ Version1.0
* @ Descriptionvin
* @author Lenovo
*/
@Data
@AllArgsConstructor

View File

@ -6,12 +6,12 @@ import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-10-03-10:12
* @ Version1.0
* @ DescriptionMqtt
* @author Lenovo
*/
@Data
@Builder

View File

@ -6,12 +6,12 @@ import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-10-03-10:04
* @ Version1.0
* @ Description
* @author Lenovo
*/
@Data
@Builder

View File

@ -6,12 +6,12 @@ import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-09-26-15:53
* @ Version1.0
* @ DescriptionAli
* @author Lenovo
*/
@Data
@SuperBuilder
@ -21,7 +21,7 @@ public class AliServerConfig {
/**
*
*/
private Long id;
private Long id;
/**
* id (ID)
*/
@ -29,7 +29,7 @@ public class AliServerConfig {
/**
* id
*/
private String imageId;
private String imageId;
/**
* ()
*/

View File

@ -6,12 +6,12 @@ import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-09-26-20:15
* @ Version1.0
* @ Description
* @author Lenovo
*/
@Mapper
public interface CarOneClickOperationMapper {

View File

@ -5,17 +5,18 @@ import com.muyu.car.gateway.domain.req.VehicleConnectionReq;
import com.muyu.common.core.domain.Result;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-09-26-20:15
* @ Version1.0
* @ Description
* @author Lenovo
*/
public interface CarOneClickOperationService {
/**
*
*
* @param vehicleConnectionReq
* @return
*/

View File

@ -12,7 +12,6 @@ import com.muyu.common.redis.service.RedisService;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
@ -22,12 +21,12 @@ import static com.muyu.car.gateway.config.RabbitmqConfig.EXCHANGE_TOPICS_INFORM;
import static com.muyu.car.gateway.config.RabbitmqConfig.ROUTINGKEY_SMS;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-09-26-20:16
* @ Version1.0
* @ Description
* @author Lenovo
*/
@Log4j2
@Service
@ -44,16 +43,13 @@ public class CarOneClickOperationServiceImpl implements CarOneClickOperationServ
/**
*
*
* @param vehicleConnectionReq
* @return
*/
@Override
public Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq) {
log.info("车辆连接请求:{}",vehicleConnectionReq.toString());
// // 使用交换机发送消息 给事件系统发
// rabbitTemplate.convertAndSend("exchange_topics_inform","inform.#.email.#",vehicleConnectionReq.getVehicleVin());
// log.info("发送消息成功:{}",vehicleConnectionReq.getVehicleVin());
log.info("车辆连接请求:{}", vehicleConnectionReq.toString());
VehicleConnection vehicleConnection = new VehicleConnection();
//车辆vin
@ -61,23 +57,22 @@ public class CarOneClickOperationServiceImpl implements CarOneClickOperationServ
//用户名
vehicleConnection.setUsername(vehicleConnectionReq.getUsername());
//密码(vin+时间戳+随机数)
vehicleConnection.setPassword(vehicleConnectionReq.getVehicleVin()+vehicleConnectionReq.getTimestamp()+vehicleConnectionReq.getNonce());
vehicleConnection.setPassword(vehicleConnectionReq.getVehicleVin() + vehicleConnectionReq.getTimestamp() + vehicleConnectionReq.getNonce());
//查询有没有这辆车的vin码
List<String> selectVehicle = carOneClickOperationMapper.selectByVehicleVin(vehicleConnectionReq.getVehicleVin());
if(selectVehicle.isEmpty()){
if (selectVehicle.isEmpty()) {
//添加连接信息
carOneClickOperationMapper.addConnect(vehicleConnection);
log.info("车辆上线成功");
}else {
} else {
throw new RuntimeException("车辆无法重复上线");
}
//先判断vin码
HashOperations<String, String, String> hashOps = redisTemplate.opsForHash();
String vinIp = hashOps.get("oneVinIp", vehicleConnectionReq.getVehicleVin());
if(vinIp!=null){
throw new RuntimeException("车辆绑定ip失败已经存在");
if (redisService.hasKey(vehicleConnectionReq.getVehicleVin())) {
log.error("=============车辆:{}已经绑定过了", vehicleConnectionReq.getVehicleVin());
throw new RuntimeException("=============车辆已经绑定过了");
}
MqttProperties mqttProperties = new MqttProperties();
List<VehicleConnection> vehicleVin = selectByVehicleVin(vehicleConnectionReq.getVehicleVin());
@ -89,43 +84,44 @@ public class CarOneClickOperationServiceImpl implements CarOneClickOperationServ
mqttProperties.setTopic("vehicle");
mqttProperties.setQos(0);
//判断redis有没有count键
if(redisTemplate.hasKey("oneCount")){
if (redisTemplate.hasKey("oneCount")) {
//取出count
Integer count = Integer.valueOf(redisTemplate.opsForValue().get("oneCount"));
if(count == 1){
redisTemplate.opsForValue().set("oneCount",String.valueOf(0));
}else {
redisTemplate.opsForValue().set("oneCount",String.valueOf(count+1));
if (count == 1) {
redisTemplate.opsForValue().set("oneCount", String.valueOf(0));
} else {
redisTemplate.opsForValue().set("oneCount", String.valueOf(count + 1));
}
//根据游标count获取服务IP
// String ip = redisTemplate.opsForList().index("ipList", count);
Object ipList = redisService.redisTemplate.opsForList().index("oneIpList", count);
log.info("=========================oneIpList:"+ipList);
log.info("=========================oneIpList:" + ipList);
//关联车辆和服务
this.addIpAddress(new VinIp(vehicleConnectionReq.getVehicleVin(),ipList.toString()));
this.addIpAddress(new VinIp(vehicleConnectionReq.getVehicleVin(), ipList.toString()));
//响应信息
log.info("车辆:{}",vehicleConnectionReq.getVehicleVin()+"绑定成功:{}",ipList);
mqttProperties.setBroker("tcp://"+ipList+":1883");
log.info("车辆:{}", vehicleConnectionReq.getVehicleVin() + "绑定成功:{}", ipList);
mqttProperties.setBroker("tcp://" + ipList + ":1883");
// 使用交换机发送消息
rabbitTemplate.convertAndSend(EXCHANGE_TOPICS_INFORM,ROUTINGKEY_SMS,mqttProperties);
log.info("============================发送消息成功:{}",mqttProperties);
return Result.success(new MqttServerModel("tcp://"+ipList+":1883","vehicle"));
}else {
redisTemplate.opsForValue().set("oneCount",String.valueOf(0));
rabbitTemplate.convertAndSend(EXCHANGE_TOPICS_INFORM, ROUTINGKEY_SMS, mqttProperties);
log.info("============================发送消息成功:{}", mqttProperties);
return Result.success(new MqttServerModel("tcp://" + ipList + ":1883", "vehicle"));
} else {
redisTemplate.opsForValue().set("oneCount", String.valueOf(0));
//根据游标count获取服务器Ip
Object ipList = redisService.redisTemplate.opsForList().index("oneIpList", 0);
//关联车辆和服务
this.addIpAddress(new VinIp(vehicleConnectionReq.getVehicleVin(),ipList.toString()));
this.addIpAddress(new VinIp(vehicleConnectionReq.getVehicleVin(), ipList.toString()));
//响应信息
log.info("车辆:{}",vehicleConnectionReq.getVehicleVin(),"与:{}绑定成功",ipList);
mqttProperties.setBroker("tcp://"+ipList+":1883");
log.info("车辆:{}", vehicleConnectionReq.getVehicleVin(), "与:{}绑定成功", ipList);
mqttProperties.setBroker("tcp://" + ipList + ":1883");
// 使用交换机发送消息
rabbitTemplate.convertAndSend(EXCHANGE_TOPICS_INFORM,ROUTINGKEY_SMS,mqttProperties);
log.info("============================发送消息成功:{}",mqttProperties);
return Result.success(new MqttServerModel("tcp://"+ipList+":1883","vehicle"));
rabbitTemplate.convertAndSend(EXCHANGE_TOPICS_INFORM, ROUTINGKEY_SMS, mqttProperties);
log.info("============================发送消息成功:{}", mqttProperties);
return Result.success(new MqttServerModel("tcp://" + ipList + ":1883", "vehicle"));
}
}
/**
* IPredis
*/
@ -133,15 +129,16 @@ public class CarOneClickOperationServiceImpl implements CarOneClickOperationServ
if (vinIp == null || vinIp.getVin() == null || vinIp.getVin().isEmpty() || vinIp.getIp() == null || vinIp.getIp().isEmpty()) {
throw new IllegalArgumentException("vin 或 ip 不能为空或无效");
}
redisTemplate.opsForHash().put("oneVinIp", vinIp.getVin(), vinIp.getIp());
redisService.setCacheObject(vinIp.getVin(), vinIp.getIp());
}
/**
*
*
* @param vehicleVin vin
* @return
*/
public List<VehicleConnection> selectByVehicleVin(String vehicleVin) {
public List<VehicleConnection> selectByVehicleVin(String vehicleVin) {
return carOneClickOperationMapper.getMqttServerModel(vehicleVin);
}
}

View File

@ -1,6 +1,6 @@
# Tomcat
server:
port: 12900
port: 10099
# nacos线上地址
nacos:
@ -27,10 +27,10 @@ spring:
publisher-returns: true #确认消息已发送到队列(Queue)
amqp:
deserialization:
trust:
all: true
trust:
all: true
main:
allow-bean-definition-overriding: true
allow-bean-definition-overriding: true
application:
# 应用名称

View File

@ -1,26 +1,26 @@
<?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">
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.muyu.car.gateway.mapper.CarOneClickOperationMapper">
<insert id="addConnect">
insert into car_one_click_operation
(vehicle_vin,user_name,password)
values
(#{vehicleVin},#{username},#{password})
(vin, user_name, password)
values (#{vehicleVin}, #{username}, #{password})
</insert>
<select id="selectByVehicleVin" resultType="java.lang.String">
select vehicle_vin from car_one_click_operation where vehicle_vin = #{vehicleVin}
select vin
from car_one_click_operation
where vin = #{vehicleVin}
</select>
<select id="getMqttServerModel" resultType="com.muyu.car.gateway.domain.VehicleConnection">
select
vehicle_vin,user_name,password
from
car_one_click_operation
where
vehicle_vin = #{vehicleVin}
select vin vehicleVin,
user_name,
password
from car_one_click_operation
where vin = #{vehicleVin}
</select>

View File

@ -76,7 +76,7 @@ public class WarnLogs {
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId())
.append("vin", getVin())
.append("vin", getVehicleVin())
.append("warnRuleId", getWarnRuleId())
.append("startTime", getStartTime())
.append("endTime", getEndTime())