commit 6cab9b66d060edad3f7283d6e4a1a342cdca200a Author: rouchen <3133657697@qq.com> Date: Fri Jun 14 22:32:56 2024 +0800 feat 测试事件处理系统 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..2eda274 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..8d66637 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..82dbec8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..11a7eac --- /dev/null +++ b/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + com.muyu + vehicle-event + 1.0.0 + pom + + vehicle-event-server + vehicle-event-client + vehicle-event-common + + + + 17 + 17 + UTF-8 + + + + + org.springframework.boot + spring-boot-dependencies + 2.7.8 + pom + import + + + + \ No newline at end of file diff --git a/vehicle-event-client/pom.xml b/vehicle-event-client/pom.xml new file mode 100644 index 0000000..237f607 --- /dev/null +++ b/vehicle-event-client/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + com.muyu + vehicle-event + 1.0.0 + + + vehicle-event-client + + + 17 + 17 + UTF-8 + + + + + + com.muyu + vehicle-event-common + 1.0.0 + + + \ No newline at end of file diff --git a/vehicle-event-client/src/main/java/com/muyu/vehicle/event/client/VehicleEvenClientApplication.java b/vehicle-event-client/src/main/java/com/muyu/vehicle/event/client/VehicleEvenClientApplication.java new file mode 100644 index 0000000..b425775 --- /dev/null +++ b/vehicle-event-client/src/main/java/com/muyu/vehicle/event/client/VehicleEvenClientApplication.java @@ -0,0 +1,17 @@ +package com.muyu.vehicle.event.client; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * 事件系统启动类 VehicleEvenClientApplication + * + * @author Yangle + * Date 2024/6/14 15:28 + */ +@SpringBootApplication +public class VehicleEvenClientApplication { + public static void main(String[] args) { + SpringApplication.run(VehicleEvenClientApplication.class, args); + } +} diff --git a/vehicle-event-client/src/main/java/com/muyu/vehicle/event/client/config/KafKaConfig.java b/vehicle-event-client/src/main/java/com/muyu/vehicle/event/client/config/KafKaConfig.java new file mode 100644 index 0000000..d9bb9b6 --- /dev/null +++ b/vehicle-event-client/src/main/java/com/muyu/vehicle/event/client/config/KafKaConfig.java @@ -0,0 +1,37 @@ +package com.muyu.vehicle.event.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配置 KafKaConfig + * + * @author Yangle + * Date 2024/6/14 17:11 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +@Configuration +@ConfigurationProperties(prefix = "kafka") +public class KafKaConfig { + + /** + * 主题 + */ + private String topic; + + /** + * 分区 + */ + private Integer partition; + + public String queueName(){ + return topic + "-" + partition; + } +} diff --git a/vehicle-event-client/src/main/java/com/muyu/vehicle/event/client/config/MsgComponent.java b/vehicle-event-client/src/main/java/com/muyu/vehicle/event/client/config/MsgComponent.java new file mode 100644 index 0000000..9759774 --- /dev/null +++ b/vehicle-event-client/src/main/java/com/muyu/vehicle/event/client/config/MsgComponent.java @@ -0,0 +1,118 @@ +package com.muyu.vehicle.event.client.config; + + + +import lombok.extern.log4j.Log4j2; +import org.springframework.amqp.core.*; +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.context.annotation.Bean; +import org.springframework.stereotype.Component; + + + +/** + * 消息处理配置 MsgCongif + * + * @author Yangle + * Date 2024/6/14 17:07 + */ +@Log4j2 +@Component +public class MsgComponent { + + /** + * 队列声明 + * @param kafKaConfig 名称依据 + * @return 队列对象 + */ + @Bean + public Queue initVehicleEventQueue(KafKaConfig kafKaConfig){ + return new Queue(kafKaConfig.queueName(),true,true,true); +} + + /** + * 绑定 + * @param vehicleEventExchange 交换机 + * @param initVehicleEventQueue 队列 + * @return 绑定 + */ + @Bean + public Binding binding1(FanoutExchange vehicleEventExchange, + Queue initVehicleEventQueue) { + return BindingBuilder.bind(initVehicleEventQueue).to(vehicleEventExchange); + } + + + @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; + } + + @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)); + //手动确认(单条确认) + simpleMessageListenerContainer.setAcknowledgeMode(AcknowledgeMode.MANUAL); + simpleMessageListenerContainer.setMessageListener((ChannelAwareMessageListener) (message, channel) -> { + log.info("springboot.rabbitmq-queue接收到的消息:[{}]",new String(message.getBody())); + channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); + }); + //消费端限流 + simpleMessageListenerContainer.setPrefetchCount(1); + return simpleMessageListenerContainer; + } + +} diff --git a/vehicle-event-client/src/main/resources/application.yml b/vehicle-event-client/src/main/resources/application.yml new file mode 100644 index 0000000..5a42b1e --- /dev/null +++ b/vehicle-event-client/src/main/resources/application.yml @@ -0,0 +1,8 @@ +kafka: + topic: vehicle.gateway.001 + partition: 0 + +spring: + rabbitmq: + host: 101.34.243.166 + port: 5672 diff --git a/vehicle-event-common/pom.xml b/vehicle-event-common/pom.xml new file mode 100644 index 0000000..94a661a --- /dev/null +++ b/vehicle-event-common/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + com.muyu + vehicle-event + 1.0.0 + + + vehicle-event-common + + + 17 + 17 + UTF-8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-amqp + + + + org.projectlombok + lombok + + + + + \ No newline at end of file diff --git a/vehicle-event-common/src/main/java/com/muyu/chilce/event/common/config/MsgConfig.java b/vehicle-event-common/src/main/java/com/muyu/chilce/event/common/config/MsgConfig.java new file mode 100644 index 0000000..6c87d0e --- /dev/null +++ b/vehicle-event-common/src/main/java/com/muyu/chilce/event/common/config/MsgConfig.java @@ -0,0 +1,25 @@ +package com.muyu.chilce.event.common.config; + +import com.muyu.chilce.event.common.constants.VehicleConstant; +import org.springframework.amqp.core.FanoutExchange; +import org.springframework.context.annotation.Bean; + +/** + * 消息配置 MsgConfig + * + * @author Yangle + * Date 2024/6/14 15:27 + */ +public class MsgConfig { + + /** + * 创建交换机 + * @return 事件交互交换机 + */ + @Bean + public FanoutExchange vehicleEventExchange() { + return new FanoutExchange(VehicleConstant.VEHICLE_EVENT_EXCHANGE); + } + + +} diff --git a/vehicle-event-common/src/main/java/com/muyu/chilce/event/common/constants/VehicleConstant.java b/vehicle-event-common/src/main/java/com/muyu/chilce/event/common/constants/VehicleConstant.java new file mode 100644 index 0000000..9695fd3 --- /dev/null +++ b/vehicle-event-common/src/main/java/com/muyu/chilce/event/common/constants/VehicleConstant.java @@ -0,0 +1,15 @@ +package com.muyu.chilce.event.common.constants; + +/** + * 时间系统常量 VechleConstant + * + * @author Yangle + * Date 2024/6/14 15:23 + */ +public class VehicleConstant { + + /** + * 车辆事件系统交换机 + */ + public static final String VEHICLE_EVENT_EXCHANGE = "vehicle.event"; +} diff --git a/vehicle-event-common/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/vehicle-event-common/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..d016d77 --- /dev/null +++ b/vehicle-event-common/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.muyu.chilce.event.common.config.MsgConfig \ No newline at end of file diff --git a/vehicle-event-server/pom.xml b/vehicle-event-server/pom.xml new file mode 100644 index 0000000..d4a885a --- /dev/null +++ b/vehicle-event-server/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + com.muyu + vehicle-event + 1.0.0 + + + vehicle-event-server + + + 17 + 17 + UTF-8 + + + \ No newline at end of file