# Conflicts:
#	src/main/java/com/parseSystem/event/impl/FaultEvent.java
#	src/main/java/com/parseSystem/rabbitmq/ListenEventChangeRabbitMq.java
master
张海宁 2023-12-06 11:03:00 +08:00
commit 006991864e
16 changed files with 365 additions and 74 deletions

4
.gitignore vendored
View File

@ -10,7 +10,7 @@ target/
######################################################################
# IDE
IDEA/
### STS ###
.apt_generated
.classpath
@ -43,4 +43,4 @@ nbdist/
!*/build/*.java
!*/build/*.html
!*/build/*.xml
!*/build/*.xml

18
Dockerfile 100644
View File

@ -0,0 +1,18 @@
#起始镜像
FROM anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/openjdk:17-8.6
#暴露端口号
EXPOSE 8068
#挂载目录的位置
VOLUME /home/logs/parseSystem
#构建复制外部文件到docker
COPY /target/parsesystem.jar /home/app.jar
#工作目录 exec -it 进入容器内部后的默认的起始目录
WORKDIR /home
ENV TIME_ZONE Asia/Shanghai
#指定东八区
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
#启动java 程序
ENTRYPOINT ["java","-Dfile.encoding=UTF-8","-jar","/home/app.jar"]

32
pom.xml
View File

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.parseSystem</groupId>
<artifactId>parseSystem</artifactId>
<artifactId>parsesystem</artifactId>
<version>3.6.3</version>
<properties>
@ -19,6 +19,11 @@
<artifactId>spring-boot-starter</artifactId>
<version>2.7.15</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.7.15</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
@ -68,6 +73,7 @@
<artifactId>dragon-common-redis</artifactId>
<version>3.6.3</version>
</dependency>
</dependencies>
<distributionManagement>
<repository>
@ -91,4 +97,28 @@
</releases>
</repository>
</repositories>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>16</source><target>16</target></configuration></plugin>
</plugins>
</build>
</project>

View File

@ -2,6 +2,7 @@ package com.parseSystem;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* @author
@ -10,6 +11,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @date 2023/11/25 15:34
*/
@SpringBootApplication
@EnableScheduling
public class ParseSystemApplication {
/**

View File

@ -1,25 +1,20 @@
package com.parseSystem.event;
import com.alibaba.fastjson.JSONObject;
import com.dragon.common.redis.service.RedisService;
import com.parseSystem.utils.SpringUtils;
import com.parseSystem.vehicle.VehicleData;
import lombok.extern.log4j.Log4j2;
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 javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
*
*
*
* @author
* @version 1.0
* @description:
@ -29,41 +24,39 @@ import java.util.concurrent.ConcurrentHashMap;
@Log4j2
public class EventHandlerService {
/**
* map
*/
public static final Map<String, List<String>> eventMap = new ConcurrentHashMap<>();
@Autowired
private RedisService redisService;
/**
* map
*/
public static final Map<String, List<String>> eventMap=new ConcurrentHashMap<>();
/**
*
*
* @param vin
* @param eventServiceName
*/
public void registerEvent(String vin,String eventServiceName){
public void registerEvent(String vin, String eventServiceName) {
getEventList(vin).add(eventServiceName);
}
/**
*
*
* @param vin
* @param eventServiceName
*/
public void removeEvent(String vin,String eventServiceName){
public void removeEvent(String vin, String eventServiceName) {
getEventList(vin).remove(eventServiceName);
}
/**
*
*
* @param vehicleData
*/
public void executeEvent(VehicleData vehicleData){
public void executeEvent(VehicleData vehicleData) {
List<String> eventList = getEventList(vehicleData.getVin());
eventList.forEach(eventServiceName -> {
VehicleEventService vehicleEventService = SpringUtils.getBean(eventServiceName);
@ -73,16 +66,17 @@ public class EventHandlerService {
/**
*
*
* @param vin
* @return
*/
public List<String> getEventList(String vin){
public List<String> getEventList(String vin) {
List<String> cacheList = redisService.getCacheList("event_VIN123456789");
eventMap.put(vin,cacheList);
eventMap.put(vin, cacheList);
List<String> eventList = eventMap.get(vin);
if (eventList==null){
if (eventList == null) {
ArrayList<String> list = new ArrayList<>();
eventMap.put(vin,list);
eventMap.put(vin, list);
}
return eventList;
}

View File

@ -1,10 +1,13 @@
package com.parseSystem.event.impl;
import com.alibaba.fastjson.JSONObject;
import com.dragon.common.redis.service.RedisService;
import com.parseSystem.event.EventHandlerService;
import com.parseSystem.event.VehicleEventService;
import com.parseSystem.utils.eventRuleJudge.PolygonUtil;
import com.parseSystem.vehicle.FenceData;
import com.parseSystem.vehicle.VehicleData;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -23,6 +26,7 @@ import java.util.stream.Collectors;
* @date 2023/11/27 20:57
*/
@Service("fenceEvent")
@Log4j2
public class FenceEvent extends EventHandlerService implements VehicleEventService {
@Autowired
@ -33,25 +37,40 @@ public class FenceEvent extends EventHandlerService implements VehicleEventServi
*/
@Override
public void executeEvent(VehicleData vehicleData) {
//获取报文解析的车辆vin
String vin = vehicleData.getVin();
List<String> fenceList = redisService.getCacheList("fence_VIN123456789");
List<VehicleData> arr1 = null;
arr1.add(vehicleData);
//redis为vinfenceList即vin为键电子围栏为值存入redis
List<String> fenceList = redisService.getCacheList("fence_"+vin);
redisService.setCacheList("fence_VIN123456789",arr1);
//将报文的经纬度赋值给变量
Double latitude = Double.valueOf(vehicleData.getLatitude()); //实时纬度
Double longitude = Double.valueOf(vehicleData.getLongitude());
//创建工具类使用所需要的对象变量类赋值
Point2D.Double point = new Point2D.Double();
//赋值
point.setLocation(longitude,latitude);
List<Point2D.Double> pts = new ArrayList<>();
//循环切割redis中拿到的电子围栏数据
fenceList.stream().forEach(item -> {
//将围栏存放的信息转型拿到每个对象的属性
FenceData fenceData = JSONObject.parseObject(item, FenceData.class);
//循环得到每个电子围栏的坐标拼接的一个以;为分割点表示多边形的一个字符串
List<Point2D.Double> locationPts = Arrays.stream(item.split(";"))
.map(s -> s.split(","))
.map(arr -> new Point2D.Double(Double.valueOf(arr[0]), Double.valueOf(arr[1])))
.collect(Collectors.toList());
boolean inPolygon = PolygonUtil.isInPolygon(point, locationPts);
if (inPolygon){
System.out.println("在电子围栏内");
}else{
System.out.println("在电子围栏外");
//判断如果在围栏平面内,则判断围栏的报警状态 0-驶入警告 1-驶出警告
if (inPolygon == true && fenceData.getAlarmType()==0){
log.info("----当前车辆已驶入围栏 警告!!!");
}else if(inPolygon == false && fenceData.getAlarmType()==1){
log.info("----当前车辆已驶出围栏 警告!!!");
}else {
log.info("----该车辆为绑定围栏");
}
});
}
}

View File

@ -1,10 +1,20 @@
package com.parseSystem.event.impl;
import com.alibaba.fastjson2.JSONObject;
import com.dragon.common.redis.service.RedisService;
import com.parseSystem.event.EventHandlerService;
import com.parseSystem.event.VehicleEventService;
import com.parseSystem.vehicle.VehicleData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
*
*
@ -15,6 +25,8 @@ import org.springframework.stereotype.Service;
*/
@Service("runtimeTraceEvent")
public class RuntimeTraceEvent extends EventHandlerService implements VehicleEventService {
@Autowired
private RedisService redisService;
/**
*
@ -22,6 +34,6 @@ public class RuntimeTraceEvent extends EventHandlerService implements VehicleEve
*/
@Override
public void executeEvent(VehicleData vehicleData) {
System.out.println("你好实时轨迹");
redisService.setCacheObject("runtimeTraceEvent_"+vehicleData.getVin(),vehicleData);
}
}

View File

@ -1,6 +1,8 @@
package com.parseSystem.kafka.ConsumerConfig;
import com.parseSystem.storage.service.StorageDateService;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@ -15,8 +17,10 @@ import java.util.Properties;
@Component
public class KafkaConsumerConfig {
@Bean
public KafkaConsumer<String, String> consumerInit() {
KafkaConsumer<String, String> consumer;
Properties properties = new Properties();
properties.put("bootstrap.servers", "117.72.43.22:9092");

View File

@ -3,7 +3,7 @@ package com.parseSystem.rabbitmq;
import com.alibaba.fastjson.JSONObject;
import com.parseSystem.event.EventHandlerService;
import com.parseSystem.kafka.constant.kafkaConstants;
import com.parseSystem.storage.service.StorageDateService;
import com.parseSystem.utils.DataQueueManager;
import com.parseSystem.utils.ParseUtil;
import com.parseSystem.vehicle.VehicleData;
import com.rabbitmq.client.Channel;
@ -23,25 +23,24 @@ import java.time.Duration;
import java.util.Collections;
/**
*
*
* Rabbit Mq
* @author
* @version 1.0
*
* @date 2023/11/27 22:19
*/
@Component
@Log4j2
public class ListenEventChangeRabbitMq {
@Autowired
private KafkaConsumer<String, String> consumer; // 自动装配一个String类型键和String类型值的Kafka消费者
@Autowired
private StorageDateService storageDateService; // 自动装配StorageDateService对象
@Autowired
private EventHandlerService eventHandlerService; // 自动装配EventHandlerService对象
@Autowired
private DataQueueManager dataQueueManager;
@Autowired
private KafkaConsumer<String, String> consumer;
@Autowired
private EventHandlerService eventHandlerService;
/**
* RabbitMQ"kafka_top"
@ -52,7 +51,7 @@ public class ListenEventChangeRabbitMq {
*/
@RabbitListener(queuesToDeclare = {@Queue(value = "kafka_top")})
public void consumerSubscribe(String mesg, Message message, Channel channel) {
log.info("收到准备订阅主题:" + mesg);
log.info("收到准备订阅主题:{}", mesg);
// 将接收到的消息解析为kafkaConstants对象
kafkaConstants kafkaConstants = JSONObject.parseObject(mesg, kafkaConstants.class);
@ -66,35 +65,31 @@ public class ListenEventChangeRabbitMq {
// 将消费者订阅的主题设置为"kafka_top"队列的第一个分区
consumer.assign(Collections.singleton(topicPartition));
// 开启线程持续拉取数据
while (true) {
ConsumerRecords<String, String> records = null;
try {
// 每个一秒钟拉取一次数据
records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord<String, String> record : records) {
System.out.println(record.value());
// 解析数据
String data = ParseUtil.sixteenToStr(record.value());
// 构建数据对象
VehicleData vehicleData = VehicleData.getBuild(data);
// 存储数据到tiDB
storageDateService.save(vehicleData);
System.out.println(vehicleData);
// 调用执行事件
eventHandlerService.executeEvent(vehicleData);
}
} catch (Exception e) {
log.info("records {}", records);
log.error(e);
while (true) {
ConsumerRecords<String, String> records = null;
try {
// 每个一秒钟拉取一次数据
records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord<String, String> record : records) {
System.out.println(record.value());
long startTime = System.currentTimeMillis();
log.info("开始消费时间{}:", startTime);
// 解析数据
String data = ParseUtil.sixteenToStr(record.value());
// 构建数据对象
VehicleData vehicleData = VehicleData.getBuild(data);
log.info(vehicleData);
dataQueueManager.enqueueData(vehicleData);
// 调用执行事件
eventHandlerService.executeEvent(vehicleData);
log.info("耗费时间{}", System.currentTimeMillis() - startTime);
}
} catch (Exception e) {
log.info("records {}", records);
log.error(e);
}
}
// 订阅特定分区

View File

@ -0,0 +1,68 @@
package com.parseSystem.utils;
import com.parseSystem.storage.service.StorageDateService;
import com.parseSystem.vehicle.VehicleData;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
/**
* @author
* @version 1.0
* @description:
* @date 2023/12/1 20:47
*/
@Component
@Log4j2
public class DataQueueManager {
private int count;
@Autowired
private StorageDateService storageDateService;
@Autowired
private BlockingQueue<VehicleData> dataQueue1;
@Autowired
private BlockingQueue<VehicleData> dataQueue2;
public void enqueueData(VehicleData vehicleData) {
// 将数据放入第一个队列中,如果已满,则放入第二个队列中
if (!dataQueue1.offer(vehicleData)) {
log.warn("第一个队列已满,将数据放入第二个队列");
dataQueue2.offer(vehicleData);
}
}
@Scheduled(fixedRate = 5000)
public void processQueues() {
// 如果第一个队列中的数据数量达到阈值,就从第一个队列中取出数据进行批量持久化操作
if (!dataQueue1.isEmpty()) {
List<VehicleData> batchData = new ArrayList<>();
count+=batchData.size();
dataQueue1.drainTo(batchData);
storageDateService.saveBatch(batchData);
}
// 如果第一个队列为空,但是第二个队列不为空,就从第二个队列中取出数据进行持久化操作
else if (dataQueue1.isEmpty() && !dataQueue2.isEmpty()) {
log.warn("第一个队列为空,从第二个队列中取出数据进行持久化操作");
List<VehicleData> batchData = new ArrayList<>();
count+=batchData.size();
dataQueue2.drainTo(batchData);
storageDateService.saveBatch(batchData);
}
}
}

View File

@ -0,0 +1,27 @@
package com.parseSystem.utils;
import com.parseSystem.vehicle.VehicleData;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
/**
* @author
* @version 1.0
* @description:
* @date 2023/12/1 21:15
*/
@Configuration
public class QueueConfig {
@Bean
public BlockingQueue<VehicleData> dataQueue1() {
return new LinkedBlockingQueue<>();
}
@Bean
public BlockingQueue<VehicleData> dataQueue2() {
return new LinkedBlockingQueue<>();
}
}

View File

@ -0,0 +1,61 @@
package com.parseSystem.vehicle;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
/**
*
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class FenceData {
/**
* id
*/
@TableId(type = IdType.AUTO)
private Integer fenceId;
/**
*
*/
private String fenceName;
/**
*
*/
private String fenceData;
/**s
*
*/
private Integer status;
/**
*
*/
private Integer alarmType;
/**
* id
*/
private Integer fenceTagId;
/**
*
*/
private String createdBy;
/**
*
*/
private Date createdTime;
/**
*
*/
private String updatedBy;
/**
*
*/
private Date updatedTime;
}

View File

@ -14,7 +14,7 @@ import java.util.Date;
@ToString
@NoArgsConstructor
@AllArgsConstructor
@TableName("vehicle_data")
@TableName("vehicle_data_copy1")
public class VehicleData {
/**

View File

@ -0,0 +1,14 @@
spring:
profiles:
active: dev
redis:
host: 10.100.1.2
port: 6379
password:
application:
name: parseSystem
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://117.72.43.22:4000/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
server:
port: 8097

View File

@ -1,4 +1,6 @@
spring:
profiles:
active: dev
redis:
host: 10.100.1.2
port: 6379
@ -7,11 +9,11 @@ spring:
name: parseSystem
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://117.72.43.22:4000/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
url: jdbc:mysql://117.72.43.22:4000/test1?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: 123456
rabbitmq:
host: 47.120.38.248
host: 182.254.222.21
port: 5672
template:
mandatory: true
@ -24,4 +26,4 @@ spring:
publisher-confirms: true #确认消息已发送到交换机(Exchange)
publisher-returns: true #确认消息已发送到队列(Queue)
server:
port: 8066
port: 8068

View File

@ -0,0 +1,45 @@
import com.parseSystem.ParseSystemApplication;
import com.parseSystem.event.impl.FenceEvent;
import com.parseSystem.vehicle.VehicleData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest(classes = ParseSystemApplication.class)
public class Test {
@Autowired
private FenceEvent fenceEvent;
@org.junit.jupiter.api.Test
public void aa(){
ArrayList<VehicleData> vehicleData = new ArrayList<>();
VehicleData vehicleData1 = new VehicleData();
//车辆当前地位
vehicleData1.setVin("1234567989000");
vehicleData1.setLongitude("116.48965348217772");
vehicleData1.setLatitude("39.90816602515441");
//车辆一分钟后的位置
VehicleData vehicleData2 = new VehicleData();
vehicleData2.setVin("1234567989000");
vehicleData2.setLongitude("117.48965348217772");
vehicleData2.setLatitude("40.90816602515441");
//车辆二分钟后的位置
VehicleData vehicleData3 = new VehicleData();
vehicleData3.setVin("1234567989000");
vehicleData3.setLongitude("118.48965348217772");
vehicleData3.setLatitude("41.90816602515441");
vehicleData.add(vehicleData1);
vehicleData.add(vehicleData2);
vehicleData.add(vehicleData3);
for (VehicleData vehicleDatum : vehicleData) {
fenceEvent.executeEvent(vehicleDatum);
}
}
}