判断异常

server_five_liuyunhu
dongxiaodong 2024-04-05 08:38:19 +08:00
parent 846073f528
commit f81f05ae1c
16 changed files with 225 additions and 35 deletions

View File

@ -40,13 +40,13 @@ public class CoupletTroubleLog {
*/ */
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @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") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date troubleLogEndTime; private Date troubleLogEnd;
} }

View File

@ -30,4 +30,6 @@ public class ServiceNameConstants {
* @date * @date
*/ */
public static final String VEHICLE_SERVICE = "couplet-vehicle"; public static final String VEHICLE_SERVICE = "couplet-vehicle";
public static final String BUSINESS_SERVICE = "couplet-business";
} }

View File

@ -23,5 +23,10 @@
<groupId>com.couplet</groupId> <groupId>com.couplet</groupId>
<artifactId>couplet-common-core</artifactId> <artifactId>couplet-common-core</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.couplet</groupId>
<artifactId>couplet-common-business</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -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<Integer> insertCode(@RequestBody CoupletTroubleLog coupletTroubleLog);
}

View File

@ -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<RemoteCodeService> {
@Override
public RemoteCodeService create(Throwable cause) {
log.error("调用日志服务异常:{}", cause.getMessage());
return new RemoteCodeService()
{
@Override
public Result<Integer> insertCode(CoupletTroubleLog coupletTroubleLog) {
return null;
}
};
}
}

View File

@ -3,3 +3,4 @@ com.couplet.common.system.remote.factory.RemoteLogFallbackFactory
com.couplet.common.system.remote.factory.RemoteFileFallbackFactory com.couplet.common.system.remote.factory.RemoteFileFallbackFactory
com.couplet.common.system.remote.factory.RemoteDeptFallbackFactory com.couplet.common.system.remote.factory.RemoteDeptFallbackFactory
com.couplet.common.system.remote.factory.RemoteEmployeeFallbackFactory com.couplet.common.system.remote.factory.RemoteEmployeeFallbackFactory
com.couplet.common.system.remote.factory.RemoteCodeFallbackFactory

View File

@ -86,6 +86,12 @@
<artifactId>org.eclipse.paho.client.mqttv3</artifactId> <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.5</version> <version>1.2.5</version>
</dependency> </dependency>
<!-- RabbitMQ依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -6,6 +6,7 @@ import com.couplet.common.core.utils.SpringUtils;
import com.couplet.common.core.utils.uuid.IdUtils; import com.couplet.common.core.utils.uuid.IdUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.*; 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.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
@ -35,13 +36,8 @@ import static io.lettuce.core.pubsub.PubSubOutput.Type.message;
@Component @Component
public class ModelMessage { public class ModelMessage {
// @Autowired @Autowired
// private CoupletMsgService coupletMsgService; private RabbitTemplate rabbitTemplate;
// @Autowired
// public ModelMessage(CoupletMsgService coupletMsgService){
// this.coupletMsgService = coupletMsgService;
// }
static ArrayList<String> strings = new ArrayList<>() { static ArrayList<String> strings = new ArrayList<>() {
{ {
add("breakdown"); add("breakdown");
@ -87,7 +83,11 @@ public class ModelMessage {
for (CoupletMsgData msgData : coupletMsgDataList) { for (CoupletMsgData msgData : coupletMsgDataList) {
log.info("解析到车辆数据:{}", msgData); log.info("解析到车辆数据:{}", msgData);
//发送日志到MQ //发送消息到MQ
rabbitTemplate.convertAndSend("send-couplet-code",msgData,message -> {
message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
return message;
});
for (String string : strings) { for (String string : strings) {
IncidentService incidentService = SpringUtils.getBean(string); IncidentService incidentService = SpringUtils.getBean(string);
incidentService.incident(msgData); incidentService.incident(msgData);

View File

@ -87,6 +87,12 @@
<artifactId>couplet-analyze-msg</artifactId> <artifactId>couplet-analyze-msg</artifactId>
</dependency> </dependency>
<!-- RabbitMQ依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -13,8 +13,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
*/ */
@EnableCustomConfig @EnableCustomConfig
@EnableCustomSwagger2 @EnableCustomSwagger2
@EnableMyFeignClients(basePackages = ("com.couplet")) @EnableMyFeignClients(basePackages = ("com.couplet.**"))
@SpringBootApplication(scanBasePackages = {"com.couplet"}) @SpringBootApplication(scanBasePackages = {"com.couplet.**"})
public class CoupletBusinessApplication { public class CoupletBusinessApplication {
public static void main (String[] args) { public static void main (String[] args) {
SpringApplication.run(CoupletBusinessApplication.class, args); SpringApplication.run(CoupletBusinessApplication.class, args);

View File

@ -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<String, String> 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;
}
/**
* 110
* @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();
}
}

View File

@ -6,10 +6,12 @@ import com.couplet.common.core.domain.Result;
import com.couplet.common.core.web.controller.BaseController; import com.couplet.common.core.web.controller.BaseController;
import com.couplet.common.domain.CoupletTroubleCode; import com.couplet.common.domain.CoupletTroubleCode;
import com.couplet.common.domain.CoupletTroubleGrade; import com.couplet.common.domain.CoupletTroubleGrade;
import com.couplet.common.domain.CoupletTroubleLog;
import com.couplet.common.domain.CoupletTroubleType; import com.couplet.common.domain.CoupletTroubleType;
import com.couplet.common.domain.request.TroubleResp; import com.couplet.common.domain.request.TroubleResp;
import com.couplet.common.log.annotation.Log; import com.couplet.common.log.annotation.Log;
import com.couplet.common.log.enums.BusinessType; import com.couplet.common.log.enums.BusinessType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -24,6 +26,7 @@ import java.util.List;
*/ */
@RestController @RestController
@RequestMapping("trouble") @RequestMapping("trouble")
@Slf4j
public class SysTroubleController extends BaseController { public class SysTroubleController extends BaseController {
@Autowired @Autowired
private SysTroubleService troubleService; private SysTroubleService troubleService;
@ -80,4 +83,15 @@ public class SysTroubleController extends BaseController {
troubleService.removeById(troubleId); troubleService.removeById(troubleId);
return success(); return success();
} }
@PostMapping("insertCode")
public Result<Integer> 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);
}
} }

View File

@ -8,7 +8,6 @@ import com.couplet.common.domain.CoupletTroubleLog;
import com.couplet.common.domain.CoupletTroubleType; import com.couplet.common.domain.CoupletTroubleType;
import com.couplet.common.domain.request.TroubleResp; import com.couplet.common.domain.request.TroubleResp;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List; import java.util.List;
@ -25,4 +24,8 @@ public interface SysTroubleMapper extends BaseMapper<CoupletTroubleCode> {
List<CoupletTroubleType> selectTroubleListByType(); List<CoupletTroubleType> selectTroubleListByType();
List<CoupletTroubleGrade> selectTroubleListByGrade(); List<CoupletTroubleGrade> selectTroubleListByGrade();
List<CoupletMsgData> selectTroubleListByMsg();
int insertMsgResq(CoupletTroubleLog coupletTroubleLog);
} }

View File

@ -5,6 +5,7 @@ import com.couplet.analyze.msg.domain.CoupletMsgData;
import com.couplet.common.core.domain.PageResult; import com.couplet.common.core.domain.PageResult;
import com.couplet.common.domain.CoupletTroubleCode; import com.couplet.common.domain.CoupletTroubleCode;
import com.couplet.common.domain.CoupletTroubleGrade; import com.couplet.common.domain.CoupletTroubleGrade;
import com.couplet.common.domain.CoupletTroubleLog;
import com.couplet.common.domain.CoupletTroubleType; import com.couplet.common.domain.CoupletTroubleType;
import com.couplet.common.domain.request.TroubleResp; import com.couplet.common.domain.request.TroubleResp;
import io.swagger.models.auth.In; import io.swagger.models.auth.In;
@ -24,4 +25,8 @@ public interface SysTroubleService extends IService<CoupletTroubleCode> {
List<CoupletTroubleType> selectTroubleListByType(); List<CoupletTroubleType> selectTroubleListByType();
List<CoupletTroubleGrade> selectTroubleListByGrade(); List<CoupletTroubleGrade> selectTroubleListByGrade();
List<CoupletMsgData> selectTroubleListByMsg();
int insertMsgResq(CoupletTroubleLog coupletTroubleLog);
} }

View File

@ -7,6 +7,7 @@ import com.couplet.business.server.service.SysTroubleService;
import com.couplet.common.core.domain.PageResult; import com.couplet.common.core.domain.PageResult;
import com.couplet.common.domain.CoupletTroubleCode; import com.couplet.common.domain.CoupletTroubleCode;
import com.couplet.common.domain.CoupletTroubleGrade; import com.couplet.common.domain.CoupletTroubleGrade;
import com.couplet.common.domain.CoupletTroubleLog;
import com.couplet.common.domain.CoupletTroubleType; import com.couplet.common.domain.CoupletTroubleType;
import com.couplet.common.domain.request.TroubleResp; import com.couplet.common.domain.request.TroubleResp;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
@ -14,6 +15,7 @@ import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
@ -52,33 +54,23 @@ public class SysTroubleServiceImpl extends ServiceImpl<SysTroubleMapper, Couplet
return sysTroubleMapper.selectTroubleListByGrade(); return sysTroubleMapper.selectTroubleListByGrade();
} }
/** /**
* *
* GTA
* @return * @return
*/ */
public static String generateGTA() { @Override
// 生成以GTA开头的字符串 public List<CoupletMsgData> selectTroubleListByMsg() {
String codefix = "GTA"; return sysTroubleMapper.selectTroubleListByMsg();
// 删除4位数随机数字
String s = generateRandomNumber(4);
//拼接
return codefix + s;
} }
/** /**
* 110 *
* @param length * @param coupletTroubleLog
* @return * @return
*/ */
public static String generateRandomNumber(int length) { @Override
Random random = new Random(); public int insertMsgResq(CoupletTroubleLog coupletTroubleLog) {
StringBuilder builder = new StringBuilder(); return sysTroubleMapper.insertMsgResq(coupletTroubleLog);
for (int i = 0; i < length; i++) {
builder.append(random.nextInt(10));
}
return builder.toString();
} }
} }

View File

@ -19,6 +19,11 @@
LEFT JOIN couplet_trouble_grade g on t.grade_id = g.grade_id 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 LEFT JOIN couplet_trouble_type y on t.type_id= y.type_id
</sql> </sql>
<insert id="insertMsgResq">
insert into couplet_trouble_log(trouble_log_code,toruble_log_vin,trouble_log_start,trouble_log_end)
values(#{troubleLogCode},#{troubleLogVin},#{troubleLogStart},#{troubleLogEnd})
</insert>
<select id="selectTroubleList" parameterType="com.couplet.business.server.mapper.SysTroubleMapper" resultMap="SysTroubleResult"> <select id="selectTroubleList" parameterType="com.couplet.business.server.mapper.SysTroubleMapper" resultMap="SysTroubleResult">
<include refid="selectTroubleVo"/> <include refid="selectTroubleVo"/>
@ -37,8 +42,8 @@
<select id="selectTroubleListByGrade" resultType="com.couplet.common.domain.CoupletTroubleGrade"> <select id="selectTroubleListByGrade" resultType="com.couplet.common.domain.CoupletTroubleGrade">
select * from couplet_trouble_grade select * from couplet_trouble_grade
</select> </select>
<select id="isTroubleLogCode" resultType="com.couplet.common.domain.CoupletTroubleLog"> <select id="selectTroubleListByMsg" resultType="com.couplet.analyze.msg.domain.CoupletMsgData">
select * from couplet_trouble_log where trouble_log_id = #{troubleLogId} select * from couplet_msg_data
</select> </select>