优化代码第三次

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,18 +7,19 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
* @author Lenovo
* @ ToolIntelliJ IDEA * @ ToolIntelliJ IDEA
* @ AuthorCHX * @ AuthorCHX
* @ Date2024-10-07-21:51 * @ Date2024-10-07-21:51
* @ Version1.0 * @ Version1.0
* @ Description * @ Description
* @author Lenovo
*/ */
@Log4j2 @Log4j2
@Component @Component
public class DeleteSample implements DisposableBean { public class DeleteSample implements DisposableBean {
@Autowired @Autowired
private AliYunEcsService aliYunEcsService; private AliYunEcsService aliYunEcsService;
@Override @Override
public void destroy() { public void destroy() {
try { try {

View File

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

View File

@ -19,29 +19,49 @@ import org.springframework.context.annotation.Configuration;
@Log4j2 @Log4j2
@Configuration @Configuration
public class RabbitmqConfig { public class RabbitmqConfig {
// 日志 /**
*
*/
private static final Logger logger = LoggerFactory.getLogger(RabbitmqConfig.class); 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"; 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.#"; public static final String ROUTINGKEY_EMAIL = "inform.#.email.#";
/** /**
* key * key
*/ */
public static final String ROUTINGKEY_SMS = "inform.#.sms.#"; 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) @Bean(QUEUE_INFORM_EMAIL)
public Queue queueInformEmail() { public Queue queueInformEmail() {
try { try {
@ -71,7 +93,9 @@ public class RabbitmqConfig {
} }
} }
// 声明QUEUE_INFORM_SMS队列 /**
* QUEUE_INFORM_SMS
*/
@Bean(QUEUE_INFORM_SMS) @Bean(QUEUE_INFORM_SMS)
public Queue queueInformSms() { public Queue queueInformSms() {
try { 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 @Bean
public Binding bindingQueueInformEmail(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue, public Binding bindingQueueInformEmail(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue,
@Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) { @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL).noargs(); return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL).noargs();
} }
//ROUTINGKEY_SMS队列绑定交换机指定routingKey /**
* QUEUE_INFORM_SMS routingKey ROUTINGKEY_SMS
*
* @param queue QUEUE_INFORM_SMS
* @param exchange EXCHANGE_TOPICS_INFORM
*/
@Bean @Bean
public Binding bindingRoutingKeySms(@Qualifier(QUEUE_INFORM_SMS) Queue queue, public Binding bindingRoutingKeySms(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
@Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) { @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs(); return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs();
} }
/**
* 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,12 +13,12 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
/** /**
* @author Lenovo
* @ ToolIntelliJ IDEA * @ ToolIntelliJ IDEA
* @ AuthorCHX * @ AuthorCHX
* @ Date2024-09-26-20:14 * @ Date2024-09-26-20:14
* @ Version1.0 * @ Version1.0
* @ Description * @ Description
* @author Lenovo
*/ */
@Log4j2 @Log4j2
@RestController @RestController
@ -30,12 +30,13 @@ public class CarOneClickOperationController {
/** /**
* http * http
*
* @param vehicleConnectionReq * @param vehicleConnectionReq
* @return * @return
*/ */
@PostMapping("/receiveMsg/connect") @PostMapping("/receiveMsg/connect")
public Result<MqttServerModel> receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq) { public Result<MqttServerModel> receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq) {
log.info(">"+vehicleConnectionReq); log.info("======================" + vehicleConnectionReq);
return carOneClickOperationService.getConnect(vehicleConnectionReq); return carOneClickOperationService.getConnect(vehicleConnectionReq);
} }

View File

@ -6,12 +6,12 @@ import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
/** /**
* @author Lenovo
* @ ToolIntelliJ IDEA * @ ToolIntelliJ IDEA
* @ AuthorCHX * @ AuthorCHX
* @ Date2024-09-30-9:33 * @ Date2024-09-30-9:33
* @ Version1.0 * @ Version1.0
* @ Description * @ Description
* @author Lenovo
*/ */
@Data @Data
@Builder @Builder

View File

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

View File

@ -5,12 +5,12 @@ import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
/** /**
* @author Lenovo
* @ ToolIntelliJ IDEA * @ ToolIntelliJ IDEA
* @ AuthorCHX * @ AuthorCHX
* @ Date2024-10-04-15:16 * @ Date2024-10-04-15:16
* @ Version1.0 * @ Version1.0
* @ Description * @ Description
* @author Lenovo
*/ */
@Data @Data
@AllArgsConstructor @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,12 +9,12 @@ import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
/** /**
* @author Lenovo
* @ ToolIntelliJ IDEA * @ ToolIntelliJ IDEA
* @ AuthorCHX * @ AuthorCHX
* @ Date2024-09-27-20:56 * @ Date2024-09-27-20:56
* @ Version1.0 * @ Version1.0
* @ Description * @ Description
* @author Lenovo
*/ */
@Data @Data
@Builder @Builder

View File

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

View File

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

View File

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

View File

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

View File

@ -6,12 +6,12 @@ import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder; import lombok.experimental.SuperBuilder;
/** /**
* @author Lenovo
* @ ToolIntelliJ IDEA * @ ToolIntelliJ IDEA
* @ AuthorCHX * @ AuthorCHX
* @ Date2024-09-26-15:53 * @ Date2024-09-26-15:53
* @ Version1.0 * @ Version1.0
* @ DescriptionAli * @ DescriptionAli
* @author Lenovo
*/ */
@Data @Data
@SuperBuilder @SuperBuilder

View File

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

View File

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

View File

@ -12,7 +12,6 @@ import com.muyu.common.redis.service.RedisService;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; 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; import static com.muyu.car.gateway.config.RabbitmqConfig.ROUTINGKEY_SMS;
/** /**
* @author Lenovo
* @ ToolIntelliJ IDEA * @ ToolIntelliJ IDEA
* @ AuthorCHX * @ AuthorCHX
* @ Date2024-09-26-20:16 * @ Date2024-09-26-20:16
* @ Version1.0 * @ Version1.0
* @ Description * @ Description
* @author Lenovo
*/ */
@Log4j2 @Log4j2
@Service @Service
@ -44,6 +43,7 @@ public class CarOneClickOperationServiceImpl implements CarOneClickOperationServ
/** /**
* *
*
* @param vehicleConnectionReq * @param vehicleConnectionReq
* @return * @return
*/ */
@ -51,10 +51,6 @@ public class CarOneClickOperationServiceImpl implements CarOneClickOperationServ
public Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq) { public Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq) {
log.info("车辆连接请求:{}", vehicleConnectionReq.toString()); log.info("车辆连接请求:{}", vehicleConnectionReq.toString());
// // 使用交换机发送消息 给事件系统发
// rabbitTemplate.convertAndSend("exchange_topics_inform","inform.#.email.#",vehicleConnectionReq.getVehicleVin());
// log.info("发送消息成功:{}",vehicleConnectionReq.getVehicleVin());
VehicleConnection vehicleConnection = new VehicleConnection(); VehicleConnection vehicleConnection = new VehicleConnection();
//车辆vin //车辆vin
vehicleConnection.setVehicleVin(vehicleConnectionReq.getVehicleVin()); vehicleConnection.setVehicleVin(vehicleConnectionReq.getVehicleVin());
@ -74,10 +70,9 @@ public class CarOneClickOperationServiceImpl implements CarOneClickOperationServ
} }
//先判断vin码 //先判断vin码
HashOperations<String, String, String> hashOps = redisTemplate.opsForHash(); if (redisService.hasKey(vehicleConnectionReq.getVehicleVin())) {
String vinIp = hashOps.get("oneVinIp", vehicleConnectionReq.getVehicleVin()); log.error("=============车辆:{}已经绑定过了", vehicleConnectionReq.getVehicleVin());
if(vinIp!=null){ throw new RuntimeException("=============车辆已经绑定过了");
throw new RuntimeException("车辆绑定ip失败已经存在");
} }
MqttProperties mqttProperties = new MqttProperties(); MqttProperties mqttProperties = new MqttProperties();
List<VehicleConnection> vehicleVin = selectByVehicleVin(vehicleConnectionReq.getVehicleVin()); List<VehicleConnection> vehicleVin = selectByVehicleVin(vehicleConnectionReq.getVehicleVin());
@ -126,6 +121,7 @@ public class CarOneClickOperationServiceImpl implements CarOneClickOperationServ
return Result.success(new MqttServerModel("tcp://" + ipList + ":1883", "vehicle")); return Result.success(new MqttServerModel("tcp://" + ipList + ":1883", "vehicle"));
} }
} }
/** /**
* IPredis * IPredis
*/ */
@ -133,11 +129,12 @@ public class CarOneClickOperationServiceImpl implements CarOneClickOperationServ
if (vinIp == null || vinIp.getVin() == null || vinIp.getVin().isEmpty() || vinIp.getIp() == null || vinIp.getIp().isEmpty()) { if (vinIp == null || vinIp.getVin() == null || vinIp.getVin().isEmpty() || vinIp.getIp() == null || vinIp.getIp().isEmpty()) {
throw new IllegalArgumentException("vin 或 ip 不能为空或无效"); throw new IllegalArgumentException("vin 或 ip 不能为空或无效");
} }
redisTemplate.opsForHash().put("oneVinIp", vinIp.getVin(), vinIp.getIp()); redisService.setCacheObject(vinIp.getVin(), vinIp.getIp());
} }
/** /**
* *
*
* @param vehicleVin vin * @param vehicleVin vin
* @return * @return
*/ */

View File

@ -1,6 +1,6 @@
# Tomcat # Tomcat
server: server:
port: 12900 port: 10099
# nacos线上地址 # nacos线上地址
nacos: nacos:

View File

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

View File

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