From f81f05ae1c8ef4715fb55517594ee876967e248f Mon Sep 17 00:00:00 2001 From: dongxiaodong <13970843+dxdwork@user.noreply.gitee.com> Date: Fri, 5 Apr 2024 08:38:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A4=E6=96=AD=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/domain/CoupletTroubleLog.java | 4 +- .../core/constant/ServiceNameConstants.java | 2 + couplet-common/couplet-common-system/pom.xml | 5 + .../system/remote/RemoteCodeService.java | 22 +++++ .../factory/RemoteCodeFallbackFactory.java | 32 ++++++ ...ot.autoconfigure.AutoConfiguration.imports | 1 + .../couplet-analyze-msg/pom.xml | 6 ++ .../analyze/msg/model/ModelMessage.java | 16 +-- couplet-modules/couplet-business/pom.xml | 6 ++ .../server/CoupletBusinessApplication.java | 4 +- .../consumer/SendCodeQueueConsumer.java | 97 +++++++++++++++++++ .../controller/SysTroubleController.java | 14 +++ .../server/mapper/SysTroubleMapper.java | 5 +- .../server/service/SysTroubleService.java | 5 + .../service/impl/SysTroubleServiceImpl.java | 32 +++--- .../mapper/business/SysTroubleMapper.xml | 9 +- 16 files changed, 225 insertions(+), 35 deletions(-) create mode 100644 couplet-common/couplet-common-system/src/main/java/com/couplet/common/system/remote/RemoteCodeService.java create mode 100644 couplet-common/couplet-common-system/src/main/java/com/couplet/common/system/remote/factory/RemoteCodeFallbackFactory.java create mode 100644 couplet-modules/couplet-business/src/main/java/com/couplet/business/server/consumer/SendCodeQueueConsumer.java diff --git a/couplet-common/couplet-common-business/src/main/java/com/couplet/common/domain/CoupletTroubleLog.java b/couplet-common/couplet-common-business/src/main/java/com/couplet/common/domain/CoupletTroubleLog.java index 565ca22..4fc3902 100644 --- a/couplet-common/couplet-common-business/src/main/java/com/couplet/common/domain/CoupletTroubleLog.java +++ b/couplet-common/couplet-common-business/src/main/java/com/couplet/common/domain/CoupletTroubleLog.java @@ -40,13 +40,13 @@ public class CoupletTroubleLog { */ @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") - private Date troubleLogStartTime; + private Date troubleLogStart; /** * 结束预警时间 */ @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") - private Date troubleLogEndTime; + private Date troubleLogEnd; } diff --git a/couplet-common/couplet-common-core/src/main/java/com/couplet/common/core/constant/ServiceNameConstants.java b/couplet-common/couplet-common-core/src/main/java/com/couplet/common/core/constant/ServiceNameConstants.java index ed65064..3df67ab 100644 --- a/couplet-common/couplet-common-core/src/main/java/com/couplet/common/core/constant/ServiceNameConstants.java +++ b/couplet-common/couplet-common-core/src/main/java/com/couplet/common/core/constant/ServiceNameConstants.java @@ -30,4 +30,6 @@ public class ServiceNameConstants { * @date */ public static final String VEHICLE_SERVICE = "couplet-vehicle"; + + public static final String BUSINESS_SERVICE = "couplet-business"; } diff --git a/couplet-common/couplet-common-system/pom.xml b/couplet-common/couplet-common-system/pom.xml index 506f73d..5f98286 100644 --- a/couplet-common/couplet-common-system/pom.xml +++ b/couplet-common/couplet-common-system/pom.xml @@ -23,5 +23,10 @@ com.couplet couplet-common-core + + + com.couplet + couplet-common-business + diff --git a/couplet-common/couplet-common-system/src/main/java/com/couplet/common/system/remote/RemoteCodeService.java b/couplet-common/couplet-common-system/src/main/java/com/couplet/common/system/remote/RemoteCodeService.java new file mode 100644 index 0000000..4307c75 --- /dev/null +++ b/couplet-common/couplet-common-system/src/main/java/com/couplet/common/system/remote/RemoteCodeService.java @@ -0,0 +1,22 @@ +package com.couplet.common.system.remote; + +import com.couplet.common.core.constant.ServiceNameConstants; +import com.couplet.common.core.domain.Result; +import com.couplet.common.domain.CoupletTroubleLog; +import com.couplet.common.system.remote.factory.RemoteCodeFallbackFactory; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +/** + * @author DongXiaoDong + * @version 1.0 + * @date 2024/4/4 16:00 + * @description + */ +@FeignClient(contextId = "remoteCodeService",value = ServiceNameConstants.BUSINESS_SERVICE, fallbackFactory = RemoteCodeFallbackFactory.class) +public interface RemoteCodeService { + + @PostMapping("insertCode") + public Result insertCode(@RequestBody CoupletTroubleLog coupletTroubleLog); +} diff --git a/couplet-common/couplet-common-system/src/main/java/com/couplet/common/system/remote/factory/RemoteCodeFallbackFactory.java b/couplet-common/couplet-common-system/src/main/java/com/couplet/common/system/remote/factory/RemoteCodeFallbackFactory.java new file mode 100644 index 0000000..d71a4d9 --- /dev/null +++ b/couplet-common/couplet-common-system/src/main/java/com/couplet/common/system/remote/factory/RemoteCodeFallbackFactory.java @@ -0,0 +1,32 @@ +package com.couplet.common.system.remote.factory; + +import com.couplet.common.core.domain.Result; +import com.couplet.common.domain.CoupletTroubleLog; +import com.couplet.common.system.remote.RemoteCodeService; +import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.cloud.openfeign.FallbackFactory; +import org.springframework.stereotype.Component; + +/** + * @author DongXiaoDong + * @version 1.0 + * @date 2024/4/4 16:03 + * @description + */ +@Slf4j +@Component +public class RemoteCodeFallbackFactory implements FallbackFactory { + @Override + public RemoteCodeService create(Throwable cause) { + log.error("调用日志服务异常:{}", cause.getMessage()); + return new RemoteCodeService() + { + @Override + public Result insertCode(CoupletTroubleLog coupletTroubleLog) { + return null; + } + }; + } +} diff --git a/couplet-common/couplet-common-system/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/couplet-common/couplet-common-system/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 428b4e8..8c98848 100644 --- a/couplet-common/couplet-common-system/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/couplet-common/couplet-common-system/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -3,3 +3,4 @@ com.couplet.common.system.remote.factory.RemoteLogFallbackFactory com.couplet.common.system.remote.factory.RemoteFileFallbackFactory com.couplet.common.system.remote.factory.RemoteDeptFallbackFactory com.couplet.common.system.remote.factory.RemoteEmployeeFallbackFactory +com.couplet.common.system.remote.factory.RemoteCodeFallbackFactory diff --git a/couplet-modules/couplet-analyze/couplet-analyze-msg/pom.xml b/couplet-modules/couplet-analyze/couplet-analyze-msg/pom.xml index 2dd1c32..715187a 100644 --- a/couplet-modules/couplet-analyze/couplet-analyze-msg/pom.xml +++ b/couplet-modules/couplet-analyze/couplet-analyze-msg/pom.xml @@ -86,6 +86,12 @@ org.eclipse.paho.client.mqttv3 1.2.5 + + + + org.springframework.boot + spring-boot-starter-amqp + diff --git a/couplet-modules/couplet-analyze/couplet-analyze-msg/src/main/java/com/couplet/analyze/msg/model/ModelMessage.java b/couplet-modules/couplet-analyze/couplet-analyze-msg/src/main/java/com/couplet/analyze/msg/model/ModelMessage.java index 0a53f09..78c0477 100644 --- a/couplet-modules/couplet-analyze/couplet-analyze-msg/src/main/java/com/couplet/analyze/msg/model/ModelMessage.java +++ b/couplet-modules/couplet-analyze/couplet-analyze-msg/src/main/java/com/couplet/analyze/msg/model/ModelMessage.java @@ -6,6 +6,7 @@ import com.couplet.common.core.utils.SpringUtils; import com.couplet.common.core.utils.uuid.IdUtils; import lombok.extern.slf4j.Slf4j; import org.eclipse.paho.client.mqttv3.*; +import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Scheduled; @@ -35,13 +36,8 @@ import static io.lettuce.core.pubsub.PubSubOutput.Type.message; @Component public class ModelMessage { -// @Autowired -// private CoupletMsgService coupletMsgService; - -// @Autowired -// public ModelMessage(CoupletMsgService coupletMsgService){ -// this.coupletMsgService = coupletMsgService; -// } + @Autowired + private RabbitTemplate rabbitTemplate; static ArrayList strings = new ArrayList<>() { { add("breakdown"); @@ -87,7 +83,11 @@ public class ModelMessage { for (CoupletMsgData msgData : coupletMsgDataList) { log.info("解析到车辆数据:{}", msgData); - //发送日志到MQ + //发送消息到MQ + rabbitTemplate.convertAndSend("send-couplet-code",msgData,message -> { + message.getMessageProperties().setMessageId(UUID.randomUUID().toString()); + return message; + }); for (String string : strings) { IncidentService incidentService = SpringUtils.getBean(string); incidentService.incident(msgData); diff --git a/couplet-modules/couplet-business/pom.xml b/couplet-modules/couplet-business/pom.xml index 1c43041..2cd4db9 100644 --- a/couplet-modules/couplet-business/pom.xml +++ b/couplet-modules/couplet-business/pom.xml @@ -87,6 +87,12 @@ couplet-analyze-msg + + + org.springframework.boot + spring-boot-starter-amqp + + diff --git a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/CoupletBusinessApplication.java b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/CoupletBusinessApplication.java index e0beabf..a4f5624 100644 --- a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/CoupletBusinessApplication.java +++ b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/CoupletBusinessApplication.java @@ -13,8 +13,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; */ @EnableCustomConfig @EnableCustomSwagger2 -@EnableMyFeignClients(basePackages = ("com.couplet")) -@SpringBootApplication(scanBasePackages = {"com.couplet"}) +@EnableMyFeignClients(basePackages = ("com.couplet.**")) +@SpringBootApplication(scanBasePackages = {"com.couplet.**"}) public class CoupletBusinessApplication { public static void main (String[] args) { SpringApplication.run(CoupletBusinessApplication.class, args); diff --git a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/consumer/SendCodeQueueConsumer.java b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/consumer/SendCodeQueueConsumer.java new file mode 100644 index 0000000..9603263 --- /dev/null +++ b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/consumer/SendCodeQueueConsumer.java @@ -0,0 +1,97 @@ +package com.couplet.business.server.consumer; + +import com.alibaba.fastjson.JSONObject; +import com.couplet.analyze.msg.domain.CoupletMsgData; +import com.couplet.common.domain.CoupletTroubleLog; +import com.couplet.common.system.remote.RemoteCodeService; +import com.rabbitmq.client.Channel; +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.core.Message; +import org.springframework.amqp.rabbit.annotation.Queue; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; + +import java.util.Date; +import java.util.Random; +import java.util.concurrent.CompletableFuture; + +/** + * @author DongXiaoDong + * @version 1.0 + * @date 2024/3/14 22:09 + * @description + */ +@Component +@Slf4j +public class SendCodeQueueConsumer { + @Autowired + private RedisTemplate redisTemplate; + + @Autowired + private RemoteCodeService remoteCodeService; + + @RabbitListener(queuesToDeclare = {@Queue("send-couplet-code")}) + public void sendLogQueueConsumer(Message message, CoupletMsgData msgData, Channel channel) { + log.info("日志队列:{},接收到的消息:{},开始消费...","send-couplet-code", JSONObject.toJSONString(msgData)); + long start = System.currentTimeMillis(); + + String messageId = message.getMessageProperties().getMessageId(); + + try { + Long aLong = redisTemplate.opsForSet().add("send-log-queue", messageId); + if (aLong==1) { + //异步保存日志 + CompletableFuture.runAsync(() -> { + CoupletTroubleLog coupletTroubleLog = new CoupletTroubleLog(); + //判断状态是否为异常 + if (msgData.getVehicleStatus() !=1){ + String code = generateGTA(); + coupletTroubleLog.setTroubleLogCode(code); + coupletTroubleLog.setTroubleLogStart(new Date()); + String vin = msgData.getVin(); + coupletTroubleLog.setTroubleLogVin(vin); + // 如果状态为正常1时添加结束时间 + if (msgData.getVehicleStatus() == 1){ + coupletTroubleLog.setTroubleLogEnd(new Date()); + } + } + remoteCodeService.insertCode(coupletTroubleLog); + }); + log.info(""); + } + long end = System.currentTimeMillis(); + log.info("日志队列:{},接收到的消息:{},消费完成,耗时:{}毫秒","send-log-queue", JSONObject.toJSONString(msgData), (end-start)); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + /** + * + * 拼接GTA字符串 + * @return + */ + public static String generateGTA() { + // 生成以GTA开头的字符串 + String codefix = "GTA"; + // 删除4位数随机数字 + String s = generateRandomNumber(4); + //拼接 + return codefix + s; + } + + /** + * 随机生成1到10位的数字 + * @param length + * @return + */ + public static String generateRandomNumber(int length) { + Random random = new Random(); + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < length; i++) { + builder.append(random.nextInt(10)); + } + return builder.toString(); + } +} diff --git a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/controller/SysTroubleController.java b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/controller/SysTroubleController.java index 38f9a64..5777d97 100644 --- a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/controller/SysTroubleController.java +++ b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/controller/SysTroubleController.java @@ -6,10 +6,12 @@ import com.couplet.common.core.domain.Result; import com.couplet.common.core.web.controller.BaseController; import com.couplet.common.domain.CoupletTroubleCode; import com.couplet.common.domain.CoupletTroubleGrade; +import com.couplet.common.domain.CoupletTroubleLog; import com.couplet.common.domain.CoupletTroubleType; import com.couplet.common.domain.request.TroubleResp; import com.couplet.common.log.annotation.Log; import com.couplet.common.log.enums.BusinessType; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -24,6 +26,7 @@ import java.util.List; */ @RestController @RequestMapping("trouble") +@Slf4j public class SysTroubleController extends BaseController { @Autowired private SysTroubleService troubleService; @@ -80,4 +83,15 @@ public class SysTroubleController extends BaseController { troubleService.removeById(troubleId); return success(); } + + @PostMapping("insertCode") + public Result insertCode(@RequestBody CoupletTroubleLog coupletTroubleLog){ + long start = System.currentTimeMillis(); + int i = troubleService.insertMsgResq(coupletTroubleLog); + + long end = System.currentTimeMillis(); + log.info("记录异常信息成功,耗时:{}",(end-start)); + + return Result.success(i); + } } diff --git a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/mapper/SysTroubleMapper.java b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/mapper/SysTroubleMapper.java index d4ddf4b..6ca3d4b 100644 --- a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/mapper/SysTroubleMapper.java +++ b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/mapper/SysTroubleMapper.java @@ -8,7 +8,6 @@ import com.couplet.common.domain.CoupletTroubleLog; import com.couplet.common.domain.CoupletTroubleType; import com.couplet.common.domain.request.TroubleResp; import org.apache.ibatis.annotations.Mapper; -import org.apache.ibatis.annotations.Param; import java.util.List; @@ -25,4 +24,8 @@ public interface SysTroubleMapper extends BaseMapper { List selectTroubleListByType(); List selectTroubleListByGrade(); + + List selectTroubleListByMsg(); + + int insertMsgResq(CoupletTroubleLog coupletTroubleLog); } diff --git a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/SysTroubleService.java b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/SysTroubleService.java index 07dd7f4..0122c14 100644 --- a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/SysTroubleService.java +++ b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/SysTroubleService.java @@ -5,6 +5,7 @@ import com.couplet.analyze.msg.domain.CoupletMsgData; import com.couplet.common.core.domain.PageResult; import com.couplet.common.domain.CoupletTroubleCode; import com.couplet.common.domain.CoupletTroubleGrade; +import com.couplet.common.domain.CoupletTroubleLog; import com.couplet.common.domain.CoupletTroubleType; import com.couplet.common.domain.request.TroubleResp; import io.swagger.models.auth.In; @@ -24,4 +25,8 @@ public interface SysTroubleService extends IService { List selectTroubleListByType(); List selectTroubleListByGrade(); + + List selectTroubleListByMsg(); + + int insertMsgResq(CoupletTroubleLog coupletTroubleLog); } diff --git a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/impl/SysTroubleServiceImpl.java b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/impl/SysTroubleServiceImpl.java index acaf6c0..9c0321d 100644 --- a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/impl/SysTroubleServiceImpl.java +++ b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/impl/SysTroubleServiceImpl.java @@ -7,6 +7,7 @@ import com.couplet.business.server.service.SysTroubleService; import com.couplet.common.core.domain.PageResult; import com.couplet.common.domain.CoupletTroubleCode; import com.couplet.common.domain.CoupletTroubleGrade; +import com.couplet.common.domain.CoupletTroubleLog; import com.couplet.common.domain.CoupletTroubleType; import com.couplet.common.domain.request.TroubleResp; import com.github.pagehelper.PageHelper; @@ -14,6 +15,7 @@ import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.Date; import java.util.List; import java.util.Random; @@ -52,33 +54,23 @@ public class SysTroubleServiceImpl extends ServiceImpl selectTroubleListByMsg() { + return sysTroubleMapper.selectTroubleListByMsg(); } + /** - * 随机生成1到10位的数字 - * @param length + * 添加异常信息 + * @param coupletTroubleLog * @return */ - public static String generateRandomNumber(int length) { - Random random = new Random(); - StringBuilder builder = new StringBuilder(); - for (int i = 0; i < length; i++) { - builder.append(random.nextInt(10)); - } - return builder.toString(); + @Override + public int insertMsgResq(CoupletTroubleLog coupletTroubleLog) { + return sysTroubleMapper.insertMsgResq(coupletTroubleLog); } - } diff --git a/couplet-modules/couplet-business/src/main/resources/mapper/business/SysTroubleMapper.xml b/couplet-modules/couplet-business/src/main/resources/mapper/business/SysTroubleMapper.xml index aa2677d..4701557 100644 --- a/couplet-modules/couplet-business/src/main/resources/mapper/business/SysTroubleMapper.xml +++ b/couplet-modules/couplet-business/src/main/resources/mapper/business/SysTroubleMapper.xml @@ -19,6 +19,11 @@ LEFT JOIN couplet_trouble_grade g on t.grade_id = g.grade_id LEFT JOIN couplet_trouble_type y on t.type_id= y.type_id + + insert into couplet_trouble_log(trouble_log_code,toruble_log_vin,trouble_log_start,trouble_log_end) + values(#{troubleLogCode},#{troubleLogVin},#{troubleLogStart},#{troubleLogEnd}) + + select * from couplet_trouble_grade - + select * from couplet_msg_data