fast()获取信息

car-ser
王熙朝 2024-06-17 22:09:15 +08:00
parent 1512c54b1a
commit fe2ae95a94
40 changed files with 946 additions and 2 deletions

View File

@ -0,0 +1,211 @@
package com.business.common.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
/**
*
* @ClassName VehicleInfo
* @Author GuanTieLin
* @Date 2024/5/30 18:40
*/
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class VehicleInfo {
/**
* vin
*/
private String vin;
/**
*
*/
private String time;
/**
*
*/
private String longitude;
/**
*
*/
private String latitude;
/**
*
*/
private String speed;
/**
*
*/
private String totalMileage;
/**
*
*/
private String totalVoltage;
/**
*
*/
private String totalCurrent;
/**
*
*/
private String insulationResistance;
/**
*
*/
private String gears;
/**
*
*/
private String acceleratorPedal;
/**
*
*/
private String brakePedal;
/**
*
*/
private String fuelConsumptionRate;
/**
*
*/
private String MotorControllerTemperature;
/**
*
*/
private String motorSpeed;
/**
*
*/
private String motorTorque;
/**
*
*/
private String motorTemperature;
/**
*
*/
private String motorVoltage;
/**
*
*/
private String motorCurrent;
/**
* SOC
*/
private String remainingPower;
/**
*
*/
private String maximumFeedbackPower;
/**
*
*/
private String maximumDischargePower;
/**
* BMS
*/
private String selfTestCounter;
/**
*
*/
private String batteryChargeOrDischargeCurrent;
/**
* V3
*/
private String totalVoltageV3;
/**
*
*/
private String maximumVoltage;
/**
*
*/
private String minimumBatteryVoltage;
/**
*
*/
private String maximumBatteryTemperature;
/**
*
*/
private String minimumBatteryTemperature;
/**
*
*/
private String usableBatteryCapacity;
/**
*
*/
private String vehicleStatus;
/**
*
*/
private String chargeStatus;
/**
*
*/
private String operationalStatus;
/**
* SOC
*/
private String soc;
/**
*
*/
private String energyStorageDeviceWorkingStatus;
/**
*
*/
private String driveMotorStatus;
/**
*
*/
private String positioningIsOrNotEffective;
/**
* EAS
*/
private String eas;
/**
* PTC
*/
private String ptc;
/**
* EPS
*/
private String eps;
/**
* ABS
*/
private String abs;
/**
* MCU
*/
private String mcu;
/**
*
*/
private String batteryHeatingStatus;
/**
*
*/
private String batteryCurrentStatus;
/**
*
*/
private String batteryInsulationStatus;
/**
* DCDC
*/
private String dcdc;
/**
* CHG
*/
private String chg;
/**
*
*/
private String checkDigit;
}

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.muyu</groupId>
<artifactId>muyu</artifactId>
<version>3.6.3</version>
<relativePath>../../../pom.xml</relativePath>
</parent>
<artifactId>muyu-business-kafka</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.muyu</groupId>
<artifactId>muyu-business-common</artifactId>
<version>3.6.3</version>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,13 @@
package muyu.business.kafka.common;
import org.springframework.stereotype.Component;
import org.springframework.kafka.annotation.KafkaListener;
@Component
public class KafkaMonitor {
@KafkaListener(topics = "${kafka.topic}")
public void receive(String message){
System.out.println("监听到:" + message);
}
}

View File

@ -95,6 +95,23 @@
<groupId>com.muyu</groupId> <groupId>com.muyu</groupId>
<artifactId>muyu-common-system</artifactId> <artifactId>muyu-common-system</artifactId>
</dependency> </dependency>
<!-- rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<!--定时器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -0,0 +1,21 @@
//package com.muyu.goods.client;
//
//import org.springframework.kafka.annotation.KafkaListener;
//import org.springframework.stereotype.Component;
//
//@Component
//public class KafkaConsumer {
//
// @KafkaListener(topics = "my-topic")
// public void listenToMessage(String message) {
// System.out.println("监听到kafka: " + message);
// // 在这里处理消息逻辑
// }
//
// // 如果需要监听多个主题,可以添加更多的@KafkaListener注解方法
// @KafkaListener(topics = "#{'${kafka.topics}'.split(',')}")
// public void listenToMultipleTopics(String message) {
// // ...
// }
//
//}

View File

@ -0,0 +1,24 @@
package com.muyu.goods.client;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class MqConsumer {
// @RabbitListener(queuesToDeclare = {@Queue("vehicle.event")})
//监听交换机 Queue(队列名) @Exchange(value = "交换机名称", type = ExchangeTypes.FANOUT)
@RabbitListener(bindings = {@QueueBinding(value = @Queue("vehicle.event"),
exchange = @Exchange(value = "vehicle.event", type = ExchangeTypes.FANOUT))})
public void handleMessage(String message) {
System.out.println("Received message: " + message);
// 在这里处理接收到的消息
}
}

View File

@ -6,6 +6,7 @@ import javax.servlet.http.HttpServletResponse;
import com.business.common.domain.Car; import com.business.common.domain.Car;
import com.muyu.goods.service.ICarService; import com.muyu.goods.service.ICarService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.PutMapping;
@ -34,6 +35,8 @@ public class CarController extends BaseController
{ {
@Autowired @Autowired
private ICarService carService; private ICarService carService;
@Autowired
private RedisTemplate<String,String> redisTemplate;
/** /**
* *
@ -47,6 +50,15 @@ public class CarController extends BaseController
return getDataTable(list); return getDataTable(list);
} }
/**
*
* @return
*/
@PostMapping("/lists")
public Result<List<Car>> lists() {
return success(carService.lists());
}
/** /**
* *
*/ */
@ -107,4 +119,24 @@ public class CarController extends BaseController
{ {
return toAjax(carService.deleteCarByCarIds(carIds)); return toAjax(carService.deleteCarByCarIds(carIds));
} }
/**
*
* @return
*/
@PostMapping("activate")
public Result activate() {
redisTemplate.opsForList();
return null;
}
/**
*
* @return
*/
@PostMapping("finish")
public Result finish() {
String s = redisTemplate.opsForValue().get("01");
return Result.success(s);
}
} }

View File

@ -0,0 +1,16 @@
package com.muyu.goods.incident.mq;
import com.muyu.common.core.domain.Result;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Producer {
@Autowired
private RabbitTemplate rabbitTemplate;
public void electronic(Integer id) {
rabbitTemplate.convertAndSend("vehicle.event","",id);
}
}

View File

@ -20,6 +20,11 @@ public interface CarMapper
*/ */
public Car selectCarByCarId(Long carId); public Car selectCarByCarId(Long carId);
/**
*
* @return
*/
List<Car> lists();
/** /**
* *
* *
@ -63,4 +68,5 @@ public interface CarMapper
Car query(Long carId); Car query(Long carId);
Car carByCar(); Car carByCar();
} }

View File

@ -1,5 +1,6 @@
package com.muyu.goods.mapper; package com.muyu.goods.mapper;
import com.business.common.domain.Car;
import com.business.common.domain.Fence; import com.business.common.domain.Fence;
import com.business.common.domain.FenceGroups; import com.business.common.domain.FenceGroups;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
@ -20,4 +21,5 @@ public interface MapMapper {
Fence queryFence(@Param("fenceId") Long fenceId); Fence queryFence(@Param("fenceId") Long fenceId);
int updateFence(@Param("fenceId") Long fenceId, @Param("status") String status); int updateFence(@Param("fenceId") Long fenceId, @Param("status") String status);
} }

View File

@ -20,6 +20,11 @@ public interface ICarService
*/ */
public Car selectCarByCarId(Long carId); public Car selectCarByCarId(Long carId);
/**
*
* @return
*/
List<Car> lists();
/** /**
* *
* *
@ -61,4 +66,5 @@ public interface ICarService
public int deleteCarByCarId(Long carId); public int deleteCarByCarId(Long carId);
Car query(Long carId); Car query(Long carId);
} }

View File

@ -40,6 +40,11 @@ public class CarServiceImpl implements ICarService
return carMapper.selectCarByCarId(carId); return carMapper.selectCarByCarId(carId);
} }
@Override
public List<Car> lists() {
return carMapper.lists();
}
/** /**
* *
* *

View File

@ -22,6 +22,10 @@ public class MapService implements IMapService {
@Autowired @Autowired
private MapMapper mapMapper; private MapMapper mapMapper;
/**
*
* @return
*/
@Override @Override
public List<FenceGroups> selectFenceGroups() { public List<FenceGroups> selectFenceGroups() {
List<FenceGroups> fenceGroupsList = mapMapper.selectFenceGroups(); List<FenceGroups> fenceGroupsList = mapMapper.selectFenceGroups();
@ -35,18 +39,32 @@ public class MapService implements IMapService {
return fenceGroupsList; return fenceGroupsList;
} }
/**
*
* @return
*/
@Override @Override
public List<Fence> selectFence() { public List<Fence> selectFence() {
List<Fence> fenceList = mapMapper.selectFence(); List<Fence> fenceList = mapMapper.selectFence();
return fenceList; return fenceList;
} }
/**
*
* @param carId
* @return
*/
@Override @Override
public FenceGroups selectFenceGroupsById(Long carId) { public FenceGroups selectFenceGroupsById(Long carId) {
List<FenceGroups> groups = selectFenceGroups().stream().filter(car -> car.getCarId() == carId).collect(Collectors.toList()); List<FenceGroups> groups = selectFenceGroups().stream().filter(car -> car.getCarId() == carId).collect(Collectors.toList());
return groups.get(0); return groups.get(0);
} }
/**
*
* @param pences
* @return
*/
@Override @Override
public String getSel(Pences pences) { public String getSel(Pences pences) {
System.out.println(pences); System.out.println(pences);
@ -68,6 +86,11 @@ public class MapService implements IMapService {
return "传入电子围栏失败"; return "传入电子围栏失败";
} }
/**
*
* @param fenceId
* @return
*/
@Override @Override
public String deleteFence(Long fenceId) { public String deleteFence(Long fenceId) {
int i = mapMapper.deleteFence(fenceId); int i = mapMapper.deleteFence(fenceId);
@ -77,11 +100,21 @@ public class MapService implements IMapService {
return "无效"; return "无效";
} }
/**
*
* @param fenceId
* @return
*/
@Override @Override
public Fence queryFence(Long fenceId) { public Fence queryFence(Long fenceId) {
return mapMapper.queryFence(fenceId); return mapMapper.queryFence(fenceId);
} }
/**
*
* @param fenceId
* @return
*/
@Override @Override
public String updateFence(Long fenceId) { public String updateFence(Long fenceId) {
Fence fence = queryFence(fenceId); Fence fence = queryFence(fenceId);

View File

@ -0,0 +1,43 @@
package com.muyu.goods.timer;
import lombok.extern.log4j.Log4j2;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@Component
@EnableScheduling
@Log4j2
public class Timer {
@Autowired
private RedisTemplate<String,String> redisTemplate;
@Scheduled(fixedDelay = 1000)
public void generateRandomNumber() {
// Random random = new Random();
// int i = random.nextInt(100);
// System.out.println(i);
String rightPopAndLeftPush = null;
try {
rightPopAndLeftPush = redisTemplate.opsForList().leftPop("event_handler:realtime_data:JAV0VJUJYOTOK9KSY"
, 3, TimeUnit.SECONDS);
if (rightPopAndLeftPush != null) {
log.info("处理消息: {}", rightPopAndLeftPush);
}
} catch (Exception e) {
// 处理超时异常,进行持久化操作,消息没处理可进行后续处理
// if (rightPopAndLeftPush != null) {
// redisTemplate.opsForList().leftPush("backupList", rightPopAndLeftPush);
// }
log.error(e.getMessage());
}
}
}

View File

@ -23,6 +23,14 @@ spring:
# 共享配置 # 共享配置
shared-configs: shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
kafka:
bootstrap-servers: 129.211.23.219:9200 # kafka集群地址
consumer:
group-id: my-group #消费者组id
auto-offset-reset: earliest #默认偏移量位置
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer #键序列化器
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer #值序列化器
enable-auto-commit: false # 是否自动提交offset
logging: logging:
level: level:
com.muyu.rule.mapper: DEBUG com.muyu.rule.mapper: DEBUG

View File

@ -46,6 +46,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<include refid="selectCarVo"></include> <include refid="selectCarVo"></include>
ORDER BY car_id DESC LIMIT 1; ORDER BY car_id DESC LIMIT 1;
</select> </select>
<select id="lists" resultType="com.business.common.domain.Car">
<include refid="selectCarVo"></include>
</select>
<insert id="insertCar" parameterType="com.business.common.domain.Car" useGeneratedKeys="true" keyProperty="carId"> <insert id="insertCar" parameterType="com.business.common.domain.Car" useGeneratedKeys="true" keyProperty="carId">
insert into car insert into car

View File

@ -16,6 +16,7 @@
<module>muyu-business-common</module> <module>muyu-business-common</module>
<module>muyu-business-server</module> <module>muyu-business-server</module>
<module>muyu-business-client</module> <module>muyu-business-client</module>
<module>muyu-business-kafka</module>
</modules> </modules>
<properties> <properties>

View File

@ -8,6 +8,7 @@ import com.muyu.common.system.domain.SysUser;
import com.muyu.goods.domain.Custom; import com.muyu.goods.domain.Custom;
import com.muyu.goods.domain.Enterprise; import com.muyu.goods.domain.Enterprise;
import com.muyu.goods.domain.Sources; import com.muyu.goods.domain.Sources;
import com.muyu.goods.mq.Producer;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import com.muyu.common.log.annotation.Log; import com.muyu.common.log.annotation.Log;
@ -32,6 +33,12 @@ public class EnterpriseController extends BaseController
@Autowired @Autowired
private IEnterpriseService enterpriseService; private IEnterpriseService enterpriseService;
@PostMapping("rest")
public void send() {
enterpriseService.send();
}
/** /**
* *
*/ */

View File

@ -14,5 +14,13 @@ public class Producer {
rabbitTemplate.convertAndSend("datasource",ip); rabbitTemplate.convertAndSend("datasource",ip);
} }
public void exchangeSource(String ip) {
rabbitTemplate.convertAndSend("datasource","",ip);
}
public void vehicleEvent(String name) {
rabbitTemplate.convertAndSend("vehicle.event","",name);
}
} }

View File

@ -97,4 +97,5 @@ public interface IEnterpriseService
List<Custom> listCustom(); List<Custom> listCustom();
void send();
} }

View File

@ -252,4 +252,11 @@ public class EnterpriseServiceImpl implements IEnterpriseService
return enterpriseMapper.listCustom(); return enterpriseMapper.listCustom();
} }
@Override
public void send() {
List<Enterprise> lists = lists();
String jsonString = JSONObject.toJSONString(lists);
producer.vehicleEvent(jsonString);
}
} }

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.muyu</groupId>
<artifactId>muyu-incident</artifactId>
<version>3.6.3</version>
</parent>
<artifactId>muyu-incident-client</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.muyu</groupId>
<artifactId>muyu-incident-common</artifactId>
<version>3.6.3</version>
</dependency>
<dependency>
<groupId>com.muyu</groupId>
<artifactId>muyu-incident-server</artifactId>
<version>3.6.3</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,14 @@
package com.muyu.incident.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*
*/
@SpringBootApplication
public class VehicleEventClientApplication {
public static void main(String[] args) {
SpringApplication.run(VehicleEventClientApplication.class,args);
}
}

View File

@ -0,0 +1,32 @@
package com.muyu.incident.client.config;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* kafka
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Configuration
@ConfigurationProperties(prefix = "kafka")
public class KafkaConfig {
private static final String queueName = null;
/**
*
*/
private String topic;
/**
*
*/
private Integer partition;
public String queueName() {
return topic + "." + partition;
}
}

View File

@ -0,0 +1,124 @@
package com.muyu.incident.client.config;
import com.alibaba.fastjson2.JSONObject;
import com.incident.server.model.IncidentModel;
import com.muyu.incident.common.config.MsgConfig;
import com.muyu.incident.common.constants.VehicleConstant;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.util.Arrays;
/**
*
*/
@Component
@Log4j2
public class MsgComponent {
/**
*
* @param kafkaConfig
* @return
*/
@Bean
public Queue initCehicleEventClientQueue(KafkaConfig kafkaConfig) {
return new Queue(kafkaConfig.queueName(),true,true,true);
}
/**
*
// * @param vehicleEventExchange 交换机
* @param initCehicleEventClientQueue
* @return
*/
@Bean
public Binding binding1(Queue initCehicleEventClientQueue) {
MsgConfig msgConfig = new MsgConfig();
FanoutExchange fanoutExchange = msgConfig.VehicleEvent();
System.out.println(fanoutExchange);
System.out.println(initCehicleEventClientQueue);
return BindingBuilder.bind(initCehicleEventClientQueue).to(fanoutExchange);
}
/**
* rabbitmq
* @param connectionFactory
* @param kafkaConfig
* @return
*/
@Bean
public SimpleMessageListenerContainer messageListenerContainer(ConnectionFactory connectionFactory,KafkaConfig kafkaConfig) {
SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer(connectionFactory);
//针对哪些队列(参数为可变参数)
simpleMessageListenerContainer.setQueueNames(kafkaConfig.queueName());
//同时有多少个消费者线程在消费这个队列,相当于线程池的线程数字。
simpleMessageListenerContainer.setConcurrentConsumers(3);
//最大的消费者线程数
simpleMessageListenerContainer.setMaxConcurrentConsumers(5);
//设置消息确认方式 NONE=不确认,MANUAL=手动确认,AUTO=自动确认;
//自动确认
simpleMessageListenerContainer.setAcknowledgeMode(AcknowledgeMode.AUTO);
// simpleMessageListenerContainer.setMessageListener(message -> log.info("springboot.rabbitmq-queue接收到的消息[{}]", message.toString()));
//手动确认(单条确认)
simpleMessageListenerContainer.setAcknowledgeMode(AcknowledgeMode.MANUAL);
simpleMessageListenerContainer.setMessageListener((ChannelAwareMessageListener) (message, channel) -> {
log.info("springboot.rabbitmq-queue接收到的消息[{}]",
JSONObject.parseObject(new String(message.getBody()), IncidentModel.class)
);
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
});
//消费端限流
simpleMessageListenerContainer.setPrefetchCount(1);
return simpleMessageListenerContainer;
}
@Bean
public ConnectionFactory connectionFactory(RabbitProperties rabbitProperties) {
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
cachingConnectionFactory.setHost(rabbitProperties.getHost());
cachingConnectionFactory.setPort(rabbitProperties.getPort());
cachingConnectionFactory.setUsername(rabbitProperties.getUsername());
cachingConnectionFactory.setPassword(rabbitProperties.getPassword());
//开启发送确认
cachingConnectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);
//开启消息返回
cachingConnectionFactory.setPublisherReturns(true);
cachingConnectionFactory.createConnection();
return cachingConnectionFactory;
}
@Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
rabbitAdmin.setAutoStartup(true);
return rabbitAdmin;
}
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMandatory(true);
//消息返回
rabbitTemplate.setReturnCallback((message, i, s, s1, s2) -> {
log.info("消息返回实现message{},replyCode{},replyText{},exchange{},routingKey{}", message, i, s, s1, s2);
});
//发送端确认
rabbitTemplate.setConfirmCallback((correlationData, b, s) -> {
log.info("发送端确认correlationData{},ack{},cause{}", correlationData, b, s);
});
return rabbitTemplate;
}
}

View File

@ -0,0 +1,11 @@
#server:
# port: 8088
kafka:
topic: vehicle.gateway.001
partition: 0
spring:
rabbitmq:
host: 129.211.23.219
port: 5672

View File

@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.muyu</groupId>
<artifactId>muyu</artifactId>
<version>3.6.3</version>
<relativePath>../../../pom.xml</relativePath>
</parent>
<artifactId>muyu-incident-common</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,21 @@
package com.muyu.incident.common.config;
import com.muyu.incident.common.constants.VehicleConstant;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
/**
*
*/
@Component
public class MsgConfig {
/**
*
* @return
*/
@Bean
public FanoutExchange VehicleEvent() {
return new FanoutExchange(VehicleConstant.VEHICLE_EVENT_EXCHANGE);
}
}

View File

@ -0,0 +1,14 @@
package com.muyu.incident.common.constants;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.context.annotation.Bean;
/**
*
*/
public class VehicleConstant {
/**
*
*/
public static String VEHICLE_EVENT_EXCHANGE = "vehicle.event";
}

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.muyu</groupId>
<artifactId>muyu-incident</artifactId>
<version>3.6.3</version>
</parent>
<artifactId>muyu-incident-server</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.muyu</groupId>
<artifactId>muyu-incident-common</artifactId>
<version>3.6.3</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,11 @@
package com.incident.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class VehicleEventSeverApplication {
public static void main(String[] args) {
SpringApplication.run(VehicleEventSeverApplication.class,args);
}
}

View File

@ -0,0 +1,26 @@
package com.incident.server.controller;
import com.alibaba.fastjson2.JSONObject;
import com.incident.server.model.IncidentModel;
import com.muyu.incident.common.constants.VehicleConstant;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class IncidentController {
@Autowired
private RabbitTemplate rabbitTemplate;
@PostMapping
public String sendMsg(@RequestBody IncidentModel incidentModel) {
rabbitTemplate.convertSendAndReceive(VehicleConstant.VEHICLE_EVENT_EXCHANGE,"",
JSONObject.toJSONString(incidentModel));
return null;
}
}

View File

@ -0,0 +1,22 @@
package com.incident.server.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class IncidentModel {
/**
*
*/
private String vin;
/**
*
*/
private String type;
}

View File

@ -0,0 +1,7 @@
server:
port: 8087
spring:
rabbitmq:
host: 129.211.23.219
port: 5672

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.muyu</groupId>
<artifactId>muyu</artifactId>
<version>3.6.3</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>muyu-incident</artifactId>
<packaging>pom</packaging>
<modules>
<module>muyu-incident-common</module>
<module>muyu-incident-server</module>
<module>muyu-incident-client</module>
</modules>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -89,12 +89,17 @@
<artifactId>muyu-common-swagger</artifactId> <artifactId>muyu-common-swagger</artifactId>
</dependency> </dependency>
<!-- rabbitmq--> <!-- rabbitmq依赖-->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId> <artifactId>spring-boot-starter-amqp</artifactId>
</dependency> </dependency>
<!-- 添加RabbitMQ支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
</dependencies> </dependencies>

View File

@ -4,6 +4,7 @@ import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSour
import com.muyu.common.security.annotation.EnableCustomConfig; import com.muyu.common.security.annotation.EnableCustomConfig;
import com.muyu.common.security.annotation.EnableMyFeignClients; import com.muyu.common.security.annotation.EnableMyFeignClients;
import com.muyu.common.swagger.annotation.EnableCustomSwagger2; import com.muyu.common.swagger.annotation.EnableCustomSwagger2;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@ -14,6 +15,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@EnableCustomConfig @EnableCustomConfig
@EnableCustomSwagger2 @EnableCustomSwagger2
@EnableMyFeignClients @EnableMyFeignClients
@EnableRabbit
@SpringBootApplication(exclude = {DynamicDataSourceAutoConfiguration.class, DataSourceAutoConfiguration.class}) @SpringBootApplication(exclude = {DynamicDataSourceAutoConfiguration.class, DataSourceAutoConfiguration.class})
public class CloudManyDataSourceApplication { public class CloudManyDataSourceApplication {
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -8,7 +8,11 @@ import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection; import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.ConnectionFactory;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue; import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -22,14 +26,22 @@ import static java.lang.Thread.sleep;
@Component @Component
@Log4j2 @Log4j2
public class Consumer { public class Consumer {
@Autowired
private AmqpAdmin amqpAdmin;
@Autowired @Autowired
private DruidConfig druidConfig; private DruidConfig druidConfig;
@Autowired @Autowired
private CloudController controller; private CloudController controller;
@RabbitListener(queuesToDeclare = {@Queue("datasource")})
// @RabbitListener(queuesToDeclare = {@Queue("datasource")})
@RabbitListener(bindings = {@QueueBinding(value = @Queue("datasource"),
exchange = @Exchange(value = "datasource", type = ExchangeTypes.FANOUT))})
public void dataSource(String ip){ public void dataSource(String ip){
System.out.println(ip);
controller.selectIp(ip); controller.selectIp(ip);
amqpAdmin.deleteQueue("datasource");
} }
} }

View File

@ -218,6 +218,7 @@
<module>muyu-modules/muyu-goods-enterprise</module> <module>muyu-modules/muyu-goods-enterprise</module>
<module>muyu-modules/muyu-business</module> <module>muyu-modules/muyu-business</module>
<module>muyu-modules/muyu-moudels-many-datasource</module> <module>muyu-modules/muyu-moudels-many-datasource</module>
<module>muyu-modules/muyu-incident</module>
</modules> </modules>
<packaging>pom</packaging> <packaging>pom</packaging>