优化代码第三次

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;
/**
* @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() {
try {

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,12 +13,12 @@ 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
@ -30,12 +30,13 @@ public class CarOneClickOperationController {
/**
* http
*
* @param vehicleConnectionReq
* @return
*/
@PostMapping("/receiveMsg/connect")
public Result<MqttServerModel> receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq) {
log.info(">"+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

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

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

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,6 +43,7 @@ public class CarOneClickOperationServiceImpl implements CarOneClickOperationServ
/**
*
*
* @param vehicleConnectionReq
* @return
*/
@ -51,10 +51,6 @@ public class CarOneClickOperationServiceImpl implements CarOneClickOperationServ
public Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq) {
log.info("车辆连接请求:{}", vehicleConnectionReq.toString());
// // 使用交换机发送消息 给事件系统发
// rabbitTemplate.convertAndSend("exchange_topics_inform","inform.#.email.#",vehicleConnectionReq.getVehicleVin());
// log.info("发送消息成功:{}",vehicleConnectionReq.getVehicleVin());
VehicleConnection vehicleConnection = new VehicleConnection();
//车辆vin
vehicleConnection.setVehicleVin(vehicleConnectionReq.getVehicleVin());
@ -74,10 +70,9 @@ public class CarOneClickOperationServiceImpl implements CarOneClickOperationServ
}
//先判断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());
@ -126,6 +121,7 @@ public class CarOneClickOperationServiceImpl implements CarOneClickOperationServ
return Result.success(new MqttServerModel("tcp://" + ipList + ":1883", "vehicle"));
}
}
/**
* 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()) {
throw new IllegalArgumentException("vin 或 ip 不能为空或无效");
}
redisTemplate.opsForHash().put("oneVinIp", vinIp.getVin(), vinIp.getIp());
redisService.setCacheObject(vinIp.getVin(), vinIp.getIp());
}
/**
*
*
* @param vehicleVin vin
* @return
*/

View File

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

View File

@ -7,20 +7,20 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<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())