Merge branch 'dev' of https://gitea.qinmian.online/group-four/cloud-car into dev
commit
13a67257fc
|
@ -33,9 +33,5 @@
|
|||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
package com.muyu.cloud.common.many.datasource;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
|
||||
|
@ -22,12 +21,10 @@ import org.springframework.boot.ApplicationRunner;
|
|||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author: DongZeLiang
|
||||
* @date: 2024/6/3
|
||||
|
@ -67,7 +64,6 @@ public class ManyDataSource implements ApplicationRunner{
|
|||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// private List<EntInfo> dataPrimarySourceInfoList(){
|
||||
// List<EntInfo> list = new ArrayList<>();
|
||||
// list.add(
|
||||
|
@ -79,7 +75,6 @@ public class ManyDataSource implements ApplicationRunner{
|
|||
// );
|
||||
// return list;
|
||||
// }
|
||||
|
||||
@Bean
|
||||
public DynamicDataSource dynamicDataSource(DruidDataSourceFactory druidDataSourceFactory) {
|
||||
// 企业列表 企业CODE,端口,IP
|
||||
|
@ -98,7 +93,6 @@ public class ManyDataSource implements ApplicationRunner{
|
|||
dynamicDataSource.setDefineTargetDataSources(dataSourceMap);
|
||||
return dynamicDataSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) {
|
||||
DruidDataSourceFactory druidDataSourceFactory = SpringUtils.getBean(DruidDataSourceFactory.class);
|
||||
|
|
|
@ -114,6 +114,18 @@
|
|||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-rabbit</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.muyu.common</groupId>
|
||||
<artifactId>saas-common</artifactId>
|
||||
<version>3.6.3</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>saas-cache</artifactId>
|
||||
<version>3.6.3</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package com.muyu.event.consumer;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.muyu.event.basic.EventPublisher;
|
||||
import com.muyu.event.service.IncidentService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.apache.kafka.clients.consumer.ConsumerRecords;
|
||||
import org.apache.kafka.clients.consumer.KafkaConsumer;
|
||||
|
@ -9,13 +9,10 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.bouncycastle.asn1.x500.style.RFC4519Style.l;
|
||||
|
||||
/**
|
||||
* kafka监听
|
||||
* @author 刘武
|
||||
|
@ -29,16 +26,18 @@ public class MessageConsumer implements ApplicationRunner {
|
|||
|
||||
@Autowired
|
||||
public KafkaConsumer consumer;
|
||||
@Autowired
|
||||
|
||||
private EventPublisher eventPublisher;
|
||||
|
||||
private final String topic="four_car";
|
||||
private final String topic="kafka-topic";
|
||||
|
||||
@Autowired
|
||||
private IncidentService incidentService;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
List<String> list = Collections.singletonList(topic);
|
||||
consumer.subscribe(list);
|
||||
|
||||
while (true){
|
||||
ConsumerRecords<String,String> consumerRecords = consumer.poll(Duration.ofMillis(100));
|
||||
consumerRecords.forEach(record -> {
|
||||
|
@ -46,6 +45,12 @@ public class MessageConsumer implements ApplicationRunner {
|
|||
JSONObject jsonObject = JSONObject.parseObject(value);
|
||||
log.info("value:{}",value);
|
||||
eventPublisher.publishEvent(jsonObject);
|
||||
try {
|
||||
incidentService.warnEventProcess(jsonObject);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,9 @@
|
|||
package com.muyu.event.listener;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.muyu.event.basic.EventCustom;
|
||||
import com.muyu.event.basic.EventListener;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 添加数据库事件
|
||||
* @program: cloud-server
|
||||
|
@ -29,9 +25,6 @@ public class AddDatabaseListener implements EventListener {
|
|||
values.add((String) value);
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package com.muyu.event.service;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
|
||||
/**
|
||||
* @author liuxinyue
|
||||
* @Package:com.muyu.event.service
|
||||
* @name:IncidentService
|
||||
* @Date:2024/10/9 15:02
|
||||
*/
|
||||
public interface IncidentService {
|
||||
|
||||
void warnEventProcess(JSONObject jsonObject) throws Exception;
|
||||
|
||||
void eventAlarmProcessing(JSONObject jsonObject) throws Exception;
|
||||
|
||||
}
|
|
@ -0,0 +1,178 @@
|
|||
package com.muyu.event.service.impl;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.muyu.cache.*;
|
||||
import com.muyu.common.domain.MessageTemplateType;
|
||||
import com.muyu.common.domain.SysCar;
|
||||
import com.muyu.common.domain.SysCarFault;
|
||||
import com.muyu.common.domain.resp.SysCarVo;
|
||||
import com.muyu.common.domain.resp.WarnRuleResp;
|
||||
import com.muyu.common.domain.resp.WarnStrategyResp;
|
||||
import com.muyu.event.consumer.MessageConsumer;
|
||||
import com.muyu.event.service.IncidentService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
||||
import org.apache.kafka.clients.consumer.ConsumerRecords;
|
||||
import org.apache.kafka.clients.consumer.KafkaConsumer;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.stereotype.Service;
|
||||
import javax.annotation.Resource;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
/**
|
||||
* @author liuxinyue
|
||||
* @Package:com.muyu.event.service.impl
|
||||
* @name:IncidentServiceImpl
|
||||
* @Date:2024/10/9 15:02
|
||||
*/
|
||||
@Log4j2
|
||||
@Service
|
||||
public class IncidentServiceImpl implements IncidentService {
|
||||
|
||||
private static int DURATION_SECONDS = 5;
|
||||
private static List<JSONObject> receivedStrings = new ArrayList<>();
|
||||
private static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
||||
private static int elapsedSeconds = 0;
|
||||
private static String file="elapsed";
|
||||
private static List<MessageTemplateType> messageTemplateTypes=null;
|
||||
private static Long msgTypeId=null;
|
||||
//滑窗时间
|
||||
private static Long slideTime=null;
|
||||
//增长率
|
||||
private static Long slideFrequency=null;
|
||||
//预警策略
|
||||
@Resource
|
||||
private WarnStrategyCacheService warnStrategyCacheService;
|
||||
//车辆
|
||||
@Resource
|
||||
private SysCarCacheService sysCarCacheService;
|
||||
//预警规则
|
||||
@Resource
|
||||
private WarnRuleCacheService warnRuleCacheService;
|
||||
//报文模版
|
||||
@Resource
|
||||
private MessageTemplateTypeCacheService messageTemplateTypeCacheService;
|
||||
//kafka的主题名称
|
||||
private final String topic="kafka-topic";
|
||||
@Autowired
|
||||
private SysCarFaultCacheService sysCarFaultCacheService;
|
||||
@Override
|
||||
public void warnEventProcess(JSONObject jsonObject) throws Exception {
|
||||
receivedStrings.add(jsonObject);
|
||||
//协议解析:每秒穿过来一个JSONObject jsonObject; 添加进receivedStrings
|
||||
//根据这个车辆VIN查询出他对应的车辆类型
|
||||
String carVin=null;
|
||||
//报文模版的ID
|
||||
Integer templateId=null;
|
||||
//取出这辆车的carVin
|
||||
for (JSONObject receivedString : receivedStrings) {
|
||||
carVin = (String) receivedString.get("carVin");
|
||||
}
|
||||
//这辆车的信息
|
||||
SysCar carByVin = null;
|
||||
List<SysCarVo> carVoList = sysCarCacheService.get(sysCarCacheService.keyPre());
|
||||
Map<String, SysCarVo> carMap = carVoList.stream()
|
||||
.collect(Collectors.toMap(SysCarVo::getCarVin, Function.identity()));
|
||||
//获取到了这个车辆的信息
|
||||
carByVin = carMap.get(carVin);
|
||||
//获取到这辆车绑定的报文模版
|
||||
templateId=carByVin.getTemplateId();
|
||||
//这个是这辆车对应的所有策略
|
||||
List<WarnStrategyResp> carWithWarnStrategyList=null;
|
||||
List<WarnStrategyResp> warnStrategyResps = warnStrategyCacheService.get(warnStrategyCacheService.keyPre());
|
||||
for (WarnStrategyResp warnStrategyResp : warnStrategyResps) {
|
||||
//策略中有绑定的车辆ID
|
||||
if(warnStrategyResp.getCarTypeId()==carByVin.getCarTypeId()){
|
||||
carWithWarnStrategyList.add(warnStrategyResp);
|
||||
}
|
||||
}
|
||||
//该车对应的所有预警规则
|
||||
List<WarnRuleResp> warnRuleResp=null;
|
||||
List<WarnRuleResp> warnRuleResps = warnRuleCacheService.get(warnRuleCacheService.keyPre());
|
||||
for (WarnStrategyResp warnStrategyResp : carWithWarnStrategyList) {
|
||||
for (WarnRuleResp ruleResp : warnRuleResps) {
|
||||
if(warnStrategyResp.getId().equals(ruleResp.getStrategyId())){
|
||||
warnRuleResp.add(ruleResp);
|
||||
}
|
||||
}
|
||||
}
|
||||
//报文模版
|
||||
messageTemplateTypes = messageTemplateTypeCacheService.get(messageTemplateTypeCacheService.keyPre());
|
||||
for (WarnRuleResp ruleResp : warnRuleResp) {
|
||||
//每一个规则他绑定了报文模版里面对应的一个配置 比如:电池,或者车速
|
||||
msgTypeId = ruleResp.getMsgTypeId();
|
||||
//将规则中对应的滑窗时间赋值为DURATION_SECONDS
|
||||
DURATION_SECONDS = Math.toIntExact(ruleResp.getSlideTime());
|
||||
slideFrequency = ruleResp.getSlideFrequency();
|
||||
}
|
||||
// 定义一个任务,每秒执行一次
|
||||
Runnable task = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// 清理超过的数据
|
||||
cleanUpOldStrings();
|
||||
// 检查超速条件
|
||||
checkForSpeeding();
|
||||
}
|
||||
};
|
||||
// 每隔1秒执行一次任务
|
||||
scheduler.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS);
|
||||
}
|
||||
// 清理超过60秒的数据
|
||||
private static void cleanUpOldStrings() {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
receivedStrings.removeIf(jsonObject ->
|
||||
currentTime - jsonObject.getLong("time") > TimeUnit.SECONDS.toMillis(DURATION_SECONDS)
|
||||
);
|
||||
}
|
||||
// 检查是否有超速情况
|
||||
private static void checkForSpeeding() {
|
||||
if (receivedStrings.size() < 2) return; // 如果数据不足,直接返回
|
||||
for (int i = 0; i < receivedStrings.size(); i++) {
|
||||
JSONObject current = receivedStrings.get(i);
|
||||
JSONObject next = receivedStrings.get(i + 1);
|
||||
for (MessageTemplateType messageTemplateType : messageTemplateTypes) {
|
||||
if(messageTemplateType.getMessageTemplateTypeId().equals(msgTypeId)){
|
||||
Short currentElapsed = current.getShort(messageTemplateType.getMessageField());
|
||||
Short nextElapsed = next.getShort(messageTemplateType.getMessageField());
|
||||
if (nextElapsed > currentElapsed + slideFrequency) {
|
||||
log.info("出错啦,出错啦,您的"+messageTemplateType.getMessageField()+"不正常,请检查!!!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void eventAlarmProcessing(JSONObject jsonObject) throws Exception {
|
||||
String carVin = (String) jsonObject.get("carVin");
|
||||
//车辆缓存配置
|
||||
List<SysCarVo> carVoList = sysCarCacheService.get(sysCarCacheService.keyPre());
|
||||
//这辆车的信息
|
||||
SysCar carByVin = null;
|
||||
Map<String, SysCarVo> carMap = carVoList.stream()
|
||||
.collect(Collectors.toMap(SysCarVo::getCarVin, Function.identity()));
|
||||
//获取到了这个车辆的信息
|
||||
carByVin = carMap.get(carVin);
|
||||
Long carTypeId = carByVin.getCarTypeId();
|
||||
//车辆绑定的报警规则
|
||||
List<SysCarFault> sysCarFaults = sysCarFaultCacheService.get(sysCarFaultCacheService.keyPre());
|
||||
//车辆的所有报警规则都重新存储在sysCarFaultList里面
|
||||
List<SysCarFault> sysCarFaultList=null;
|
||||
SysCar finalCarByVin = carByVin;
|
||||
sysCarFaults.forEach(sysCarFault -> {
|
||||
if(sysCarFault.getCarTypeId().equals(carTypeId)){
|
||||
sysCarFaultList.add(sysCarFault);
|
||||
}
|
||||
});
|
||||
//报文模版
|
||||
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@ nacos:
|
|||
addr: 47.101.53.251:8848
|
||||
user-name: nacos
|
||||
password: nacos
|
||||
namespace: yzl
|
||||
namespace: lxy
|
||||
# SPRING_AMQP_DESERIALIZATION_TRUST_ALL=true spring.amqp.deserialization.trust.all
|
||||
# Spring
|
||||
spring:
|
||||
|
|
|
@ -7,7 +7,7 @@ nacos:
|
|||
addr: 47.101.53.251:8848
|
||||
user-name: nacos
|
||||
password: nacos
|
||||
namespace: yzl
|
||||
namespace: lxy
|
||||
|
||||
# Spring
|
||||
spring:
|
||||
|
|
|
@ -7,7 +7,7 @@ nacos:
|
|||
addr: 47.101.53.251:8848
|
||||
user-name: nacos
|
||||
password: nacos
|
||||
namespace: yzl
|
||||
namespace: lxy
|
||||
# SPRING_AMQP_DESERIALIZATION_TRUST_ALL=true spring.amqp.deserialization.trust.all
|
||||
# Spring
|
||||
spring:
|
||||
|
|
|
@ -104,7 +104,6 @@ public class MqttConfigure {
|
|||
me.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public JSONObject messageParsing(String templateMessage) {
|
||||
//给一个JSON对象
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
@ -161,7 +160,6 @@ public class MqttConfigure {
|
|||
log.info("发送kafka成功");
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
//kafka发送消息
|
||||
public void sendKafka(JSONObject jsonObject){
|
||||
ProducerRecord<String, String> stringStringProducerRecord = new ProducerRecord<>("four_car", jsonObject.toString());
|
||||
|
|
|
@ -50,6 +50,7 @@ public class test {
|
|||
//报文模版
|
||||
@Resource
|
||||
private MessageTemplateTypeCacheService messageTemplateTypeCacheService;
|
||||
|
||||
public void main(String[] args) {
|
||||
//协议解析:每秒穿过来一个JSONObject jsonObject; 添加进receivedStrings
|
||||
//根据这个车辆VIN查询出他对应的车辆类型
|
||||
|
@ -107,7 +108,6 @@ public class test {
|
|||
// 每隔1秒执行一次任务
|
||||
scheduler.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
// 清理超过60秒的数据
|
||||
private static void cleanUpOldStrings() {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
|
@ -115,7 +115,6 @@ public class test {
|
|||
currentTime - jsonObject.getLong("time") > TimeUnit.SECONDS.toMillis(DURATION_SECONDS)
|
||||
);
|
||||
}
|
||||
|
||||
// 检查是否有超速情况
|
||||
private static void checkForSpeeding() {
|
||||
if (receivedStrings.size() < 2) return; // 如果数据不足,直接返回
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
# Tomcat
|
||||
server:
|
||||
port: 15277
|
||||
|
||||
# nacos线上地址
|
||||
nacos:
|
||||
addr: 47.101.53.251:8848
|
||||
user-name: nacos
|
||||
password: nacos
|
||||
namespace: lxy
|
||||
# SPRING_AMQP_DESERIALIZATION_TRUST_ALL=true spring.amqp.deserialization.trust.all
|
||||
# Spring
|
||||
spring:
|
||||
mvc:
|
||||
pathmatch:
|
||||
matching-strategy: ant_path_matcher
|
||||
amqp:
|
||||
deserialization:
|
||||
trust:
|
||||
all: true
|
||||
main:
|
||||
allow-bean-definition-overriding: true
|
||||
application:
|
||||
# 应用名称
|
||||
name: cloud-template
|
||||
profiles:
|
||||
# 环境配置
|
||||
active: dev
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: ${nacos.addr}
|
||||
# nacos用户名
|
||||
username: ${nacos.user-name}
|
||||
# nacos密码
|
||||
password: ${nacos.password}
|
||||
# 命名空间
|
||||
namespace: ${nacos.namespace}
|
||||
config:
|
||||
# 服务注册地址
|
||||
server-addr: ${nacos.addr}
|
||||
# nacos用户名
|
||||
username: ${nacos.user-name}
|
||||
# nacos密码
|
||||
password: ${nacos.password}
|
||||
# 命名空间
|
||||
namespace: ${nacos.namespace}
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
shared-configs:
|
||||
# 系统共享配置
|
||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
# 系统环境Config共享配置
|
||||
- application-config-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
# xxl-job 配置文件
|
||||
- application-xxl-config-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
|
||||
logging:
|
||||
level:
|
||||
com.muyu.system.mapper: DEBUG
|
|
@ -91,6 +91,13 @@
|
|||
<artifactId>cloud-common-rabbit</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<version>5.0.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
|
|
|
@ -14,6 +14,7 @@ import lombok.extern.log4j.Log4j2;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -30,10 +31,12 @@ public class ManageInstance implements ApplicationRunner {
|
|||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 镜像ID
|
||||
*/
|
||||
public static final String IMAGE_ID = "m-uf6ffgkry85fwu4znr6s";
|
||||
public static final String IMAGE_ID = "m-uf62k8zpy0ga35jnmkwt";
|
||||
|
||||
/**
|
||||
* 实例类型
|
||||
|
@ -57,6 +60,8 @@ public class ManageInstance implements ApplicationRunner {
|
|||
public static final String INSTANCE_CHARGE_TY = "PostPaid";
|
||||
|
||||
public static List<String> generateInstance() throws Exception {
|
||||
|
||||
|
||||
// 创建阿里云ECS客户端
|
||||
// 创建ECS客户端对象,用于后续调用ECS相关API
|
||||
Client client = CreateClient.createClient();
|
||||
|
@ -118,16 +123,18 @@ public class ManageInstance implements ApplicationRunner {
|
|||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
List<String> list = generateInstance();
|
||||
ArrayList<String> ipList = new ArrayList<>();
|
||||
log.info("创建实例成功");
|
||||
log.info("正在加载实例");
|
||||
Thread.sleep(30000);
|
||||
List<InstanceInfo> instanceInfos = SelectInstance.selectInstance(list);
|
||||
log.info("实例信息查询成功");
|
||||
for (InstanceInfo instanceInfo : instanceInfos) {
|
||||
ipList.add(instanceInfo.getIpAddress());
|
||||
redisService.setCacheObject("FourInstanceIdKey:"+instanceInfo.getInstanceId(),instanceInfo);
|
||||
}
|
||||
redisService.setCacheList("FourIpList",ipList);
|
||||
System.out.println("实例信息:"+instanceInfos);
|
||||
log.info("实例信息:", JSONObject.toJSONString(instanceInfos));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,13 +8,17 @@ import com.aliyun.tea.TeaException;
|
|||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.aliyun.teautil.Common;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
import com.muyu.vehicle.utils.CreateClient;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import redis.clients.jedis.Jedis;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 删除实例信息
|
||||
|
@ -23,6 +27,8 @@ import java.util.ArrayList;
|
|||
@Component
|
||||
@Log4j2
|
||||
public class CloseInstance implements DisposableBean{
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
/**
|
||||
* <b>description</b> :
|
||||
|
@ -33,6 +39,7 @@ public class CloseInstance implements DisposableBean{
|
|||
*/
|
||||
public static void delInstance() throws Exception {
|
||||
|
||||
|
||||
// 创建ECS客户端对象,用于后续调用ECS相关API
|
||||
Client client = CreateClient.createClient();
|
||||
|
||||
|
@ -95,6 +102,19 @@ public class CloseInstance implements DisposableBean{
|
|||
public void destroy() throws Exception {
|
||||
log.info("开始删除实例");
|
||||
delInstance();
|
||||
redisService.deleteObject("FourIpList");
|
||||
// 连接到Redis服务器
|
||||
Jedis jedis = new Jedis("47.116.173.119", 6379);
|
||||
// 指定要删除的文件夹(命名空间)
|
||||
String namespace = "FourInstanceIdKey:";
|
||||
// 获取所有以namespace为前缀的键
|
||||
Set<String> keys = jedis.keys(namespace + "*");
|
||||
// 如果存在键,则删除它们
|
||||
if (keys.size() > 0) {
|
||||
jedis.del(keys.toArray(new String[0]));
|
||||
}
|
||||
// 关闭连接
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,10 +9,15 @@ import org.springframework.boot.ApplicationArguments;
|
|||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 创建交换机
|
||||
*/
|
||||
@Log4j2
|
||||
@Component
|
||||
public class CreateExchange implements ApplicationRunner {
|
||||
|
||||
|
||||
|
||||
@Autowired
|
||||
private ConnectionFactory connectionFactory;
|
||||
|
||||
|
|
|
@ -29,7 +29,9 @@ import java.util.List;
|
|||
|
||||
@Log4j2
|
||||
public class SelectInstance {
|
||||
|
||||
public static List<InstanceInfo> selectInstance(List<String> instanceIds) throws Exception {
|
||||
|
||||
// 创建ECS客户端对象,用于后续调用ECS相关API
|
||||
Client client = CreateClient.createClient();
|
||||
ArrayList<InstanceInfo> instanceInfos = new ArrayList<>();// 实例基础信息
|
||||
|
@ -56,7 +58,6 @@ public class SelectInstance {
|
|||
list.add(instanceInfo);
|
||||
}
|
||||
System.out.println("实例信息:"+list);
|
||||
log.info("实例信息:",list);
|
||||
return list;
|
||||
}
|
||||
|
||||
|
|
|
@ -17,10 +17,11 @@ public class CarInstanceController {
|
|||
|
||||
|
||||
@PostMapping("/receiveMsg")
|
||||
public Result receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq){
|
||||
public Result<MqttServerModel> receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq){
|
||||
log.info("=======>"+vehicleConnectionReq);
|
||||
Result<MqttServerModel> connect = vehicleConnectService.getConnect(vehicleConnectionReq);
|
||||
return Result.success(connect);
|
||||
MqttServerModel data = connect.getData();
|
||||
return Result.success(data);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,10 +3,12 @@ package com.muyu.vehicle.mapper;
|
|||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.muyu.vehicle.domain.req.VehicleConnectionReq;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
@Mapper
|
||||
public interface VehicleConnectMapper extends BaseMapper<VehicleConnectionReq> {
|
||||
|
||||
VehicleConnectionReq selectByVehicleVin(String vin);
|
||||
Integer insertVehicleConnection(VehicleConnectionReq vehicleConnectionReq);
|
||||
VehicleConnectionReq selectByVehicleVin(@Param("vin") String vin);
|
||||
|
||||
}
|
||||
|
|
|
@ -7,6 +7,6 @@ import com.muyu.vehicle.domain.req.VehicleConnectionReq;
|
|||
|
||||
public interface VehicleConnectService extends IService<VehicleConnectionReq> {
|
||||
|
||||
Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq);
|
||||
Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq);
|
||||
|
||||
}
|
||||
|
|
|
@ -3,20 +3,31 @@ package com.muyu.vehicle.service.impl;
|
|||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
import com.muyu.rabbitmq.consumer.RabbitMQConsumerUtil;
|
||||
import com.muyu.rabbitmq.producer.RabbitMQProducerUtil;
|
||||
import com.muyu.vehicle.domain.MqttServerModel;
|
||||
import com.muyu.vehicle.domain.req.VehicleConnectionReq;
|
||||
import com.muyu.vehicle.mapper.VehicleConnectMapper;
|
||||
import com.muyu.vehicle.service.VehicleConnectService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Log4j2
|
||||
public class VehicleConnectServiceImpl extends ServiceImpl<VehicleConnectMapper, VehicleConnectionReq> implements VehicleConnectService {
|
||||
@Autowired
|
||||
private VehicleConnectMapper vehicleConnectMapper;
|
||||
|
||||
@Autowired private RedisService redisService;
|
||||
|
||||
@Autowired
|
||||
private RabbitMQProducerUtil rabbitMQProducerUtil;
|
||||
|
||||
@Override
|
||||
public Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq) {
|
||||
log.info("车辆连接信息:{}", vehicleConnectionReq);
|
||||
|
@ -24,13 +35,43 @@ public class VehicleConnectServiceImpl extends ServiceImpl<VehicleConnectMapper,
|
|||
vehicleConnectionReq.setPassword(vehicleConnectionReq.getVehicleVin() + vehicleConnectionReq.getTimestamp()
|
||||
+ vehicleConnectionReq.getNonce());
|
||||
|
||||
VehicleConnectionReq connection = vehicleConnectMapper.selectByVehicleVin(vehicleConnectionReq.getVehicleVin());
|
||||
if (connection==null){
|
||||
vehicleConnectMapper.insert(vehicleConnectionReq);
|
||||
|
||||
Integer i = vehicleConnectMapper.insertVehicleConnection(vehicleConnectionReq);
|
||||
if (i > 0) {
|
||||
log.info("车辆预上线成功");
|
||||
}else {
|
||||
log.info("车辆已预上线成功,禁止重复");
|
||||
log.info("车辆预上线失败");
|
||||
}
|
||||
rabbitMQProducerUtil.basicSendMessage("SendVin",vehicleConnectionReq.getVehicleVin());
|
||||
//从redis获取信息
|
||||
// 获取名为 "ipList" 的列表
|
||||
List<String> ipList = redisService.getCacheList("FourIpList");
|
||||
if (ipList.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 获取当前使用的索引位置
|
||||
String indexStr = redisService.getCacheObject("currentIndex");
|
||||
int index = indexStr!= null? Integer.parseInt(indexStr) : 0;
|
||||
String selectedIp = ipList.get(index);
|
||||
// 获取该 IP 的使用次数
|
||||
String countStr = redisService.getCacheMapValue("ipCounts", selectedIp);
|
||||
log.info("IP:[{}]车辆连接数:[{}]",selectedIp,countStr);
|
||||
int count = countStr!= null? Integer.parseInt(countStr) : 0;
|
||||
if (count < 6) {
|
||||
// 使用次数加一
|
||||
count++;
|
||||
redisService.setCacheMapValue("ipCounts", selectedIp, String.valueOf(count));
|
||||
// 更新索引
|
||||
index = (index + 1) % ipList.size();
|
||||
redisService.setCacheObject("currentIndex", String.valueOf(index));
|
||||
MqttServerModel mqttServerModel = new MqttServerModel("tcp://" + selectedIp.substring(1, selectedIp.length() - 1) + ":1883", "vehicle");
|
||||
return Result.success(mqttServerModel);
|
||||
} else {
|
||||
// 如果使用次数达到 6 次,跳过该 IP 并更新索引
|
||||
index = (index + 1) % ipList.size();
|
||||
redisService.setCacheObject("currentIndex", String.valueOf(index));
|
||||
return getConnect(vehicleConnectionReq);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
|
||||
<mapper namespace="com.muyu.vehicle.mapper.VehicleConnectMapper">
|
||||
<insert id="insertVehicleConnection">
|
||||
INSERT INTO `four`.`vehicle_connection` ( `vehicle_vin`, `timestamp`, `username`, `nonce`, `password`) VALUES
|
||||
(#{vehicleVin},#{timestamp},#{username},#{nonce},#{password})
|
||||
</insert>
|
||||
|
||||
<select id="selectByVehicleVin" resultType="com.muyu.vehicle.domain.req.VehicleConnectionReq">
|
||||
select * from vehicle_connection where vehicle_vin=#{vin}
|
||||
</select>
|
||||
</mapper>
|
|
@ -1,5 +1,4 @@
|
|||
package com.muyu.common.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
|
@ -8,7 +7,6 @@ import lombok.AllArgsConstructor;
|
|||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 车辆管理表
|
||||
* @author sx
|
||||
|
@ -22,6 +20,7 @@ import lombok.NoArgsConstructor;
|
|||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName(value = "sys_car",autoResultMap = true)
|
||||
public class SysCar extends BaseEntity {
|
||||
|
||||
@TableId(value = "id",type = IdType.AUTO)
|
||||
private Long id;
|
||||
private String carVin;
|
||||
|
|
Loading…
Reference in New Issue