From b8c3eb314eb76561f96d3310928c02a0b817779a Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Sun, 18 May 2025 22:30:32 +0800 Subject: [PATCH] =?UTF-8?q?:wrench:=20=E6=B7=BB=E5=8A=A0=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mq-demo/LICENSE | 21 + mq-demo/pom.xml | 11 +- .../src/main/resources/application-dev.yml | 10 + mq-demo/src/main/resources/application.yaml | 28 +- mq-demo/src/main/resources/banner.txt | 10 + mq-demo/src/main/resources/logback.xml | 69 ++ .../src/main/resources/templates/index.html | 593 ++++++++++++++++++ 7 files changed, 730 insertions(+), 12 deletions(-) create mode 100644 mq-demo/LICENSE create mode 100644 mq-demo/src/main/resources/application-dev.yml create mode 100644 mq-demo/src/main/resources/banner.txt create mode 100644 mq-demo/src/main/resources/logback.xml create mode 100644 mq-demo/src/main/resources/templates/index.html diff --git a/mq-demo/LICENSE b/mq-demo/LICENSE new file mode 100644 index 0000000..e796427 --- /dev/null +++ b/mq-demo/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Bunny + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/mq-demo/pom.xml b/mq-demo/pom.xml index 1fadd0d..0195f24 100644 --- a/mq-demo/pom.xml +++ b/mq-demo/pom.xml @@ -1,5 +1,5 @@ - 4.0.0 @@ -47,10 +47,6 @@ org.springframework.amqp spring-rabbit-stream - - org.springframework.modulith - spring-modulith-events-api - org.springframework.modulith spring-modulith-starter-core @@ -62,11 +58,6 @@ runtime true - - org.springframework.modulith - spring-modulith-events-amqp - runtime - org.projectlombok lombok diff --git a/mq-demo/src/main/resources/application-dev.yml b/mq-demo/src/main/resources/application-dev.yml new file mode 100644 index 0000000..22c3674 --- /dev/null +++ b/mq-demo/src/main/resources/application-dev.yml @@ -0,0 +1,10 @@ +server: + port: 7070 + +bunny: + rabbitmq: + host: 192.168.3.144 + port: 5672 + virtual-host: / + username: admin + password: admin diff --git a/mq-demo/src/main/resources/application.yaml b/mq-demo/src/main/resources/application.yaml index 24106c6..3bbef10 100644 --- a/mq-demo/src/main/resources/application.yaml +++ b/mq-demo/src/main/resources/application.yaml @@ -1,5 +1,29 @@ +server: + port: 7070 + spring: application: name: mq-demo -server: - port: 7070 \ No newline at end of file + profiles: + active: dev + + rabbitmq: + host: ${bunny.rabbitmq.host} + port: ${bunny.rabbitmq.port} + username: ${bunny.rabbitmq.username} + password: ${bunny.rabbitmq.password} + virtual-host: ${bunny.rabbitmq.virtual-host} + # publisher-confirm-type: correlated # 交换机确认 + # publisher-returns: true # 队列确认 + # listener: + # simple: + # acknowledge-mode: manual # 手动处理消息 + # connection-timeout: 1s # 设置MQ连接超时时间 + # template: + # retry: + # enabled: true # 失败重试 + # initial-interval: 1000ms # 失败后初始时间 + # multiplier: 1 # 失败后下次等待时长倍数 initial-interval * multiplier + # max-attempts: 3 # 最大重试次数 + + diff --git a/mq-demo/src/main/resources/banner.txt b/mq-demo/src/main/resources/banner.txt new file mode 100644 index 0000000..f8a20fa --- /dev/null +++ b/mq-demo/src/main/resources/banner.txt @@ -0,0 +1,10 @@ + _ _ +| |__ _ _ _ __ _ __ _ _ (_) __ ___ ____ _ +| '_ \| | | | '_ \| '_ \| | | | | |/ _` \ \ / / _` | +| |_) | |_| | | | | | | | |_| | | | (_| |\ V | (_| | +|_.__/ \__,_|_| |_|_| |_|\__, | _/ |\__,_| \_/ \__,_| + |___/ |__/ + +Service Name${spring.application.name} +SpringBoot Version: ${spring-boot.version}${spring-boot.formatted-version} +SpringActive:${spring.profiles.active} diff --git a/mq-demo/src/main/resources/logback.xml b/mq-demo/src/main/resources/logback.xml new file mode 100644 index 0000000..fe0a953 --- /dev/null +++ b/mq-demo/src/main/resources/logback.xml @@ -0,0 +1,69 @@ + + + logback + + + + + + + + + + + + INFO + + + %cyan([%thread %d{yyyy-MM-dd HH:mm:ss}]) %yellow(%-5level) %green(%logger{100}).%boldRed(%method)-%boldMagenta(%line)-%blue(%msg%n) + + ${ENCODING} + + + + + + logs/${datetime}/financial-server.log + true + + %date{yyyy-MM-dd HH:mm:ss} [%-5level] %thread %file:%line %logger %msg%n + ${ENCODING} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/mq-demo/src/main/resources/templates/index.html b/mq-demo/src/main/resources/templates/index.html new file mode 100644 index 0000000..23306e5 --- /dev/null +++ b/mq-demo/src/main/resources/templates/index.html @@ -0,0 +1,593 @@ + + + + + + RabbitMQ学习项目 - SpringBoot实践 + + + + + + + + + +
+
+

掌握RabbitMQ与Spring Boot

+

通过实践项目学习消息队列的核心概念与应用场景

+ +
+
+ + +
+
+
+

关于这个学习项目

+

这是一个使用Spring + Boot和RabbitMQ构建的综合性学习项目,旨在帮助开发者掌握消息队列的核心概念和实践应用。

+

项目涵盖了RabbitMQ的所有主要功能,从基础的消息发送/接收到高级主题如死信队列、延迟消息和集群配置。

+
+ Spring Boot集成 + 多种交换器类型 + 消息确认机制 + 死信队列 + 延迟消息 + 集群配置 +
+
+
+ RabbitMQ架构图 +
+
+
+ + +
+

项目核心功能

+
+
+
+
+ +

基本消息队列

+
+
+

实现简单的生产者-消费者模型,学习RabbitMQ最基本的工作方式。

+
    +
  • 点对点通信
  • +
  • 消息持久化
  • +
  • 手动确认机制
  • +
+
+
+
+
+
+
+ +

交换器类型

+
+
+

探索RabbitMQ的四种交换器类型及其应用场景。

+
    +
  • Direct交换器
  • +
  • Fanout交换器
  • +
  • Topic交换器
  • +
  • Headers交换器
  • +
+
+
+
+
+
+
+ +

延迟队列

+
+
+

实现延迟消息处理,适用于定时任务和延迟执行场景。

+
    +
  • TTL设置
  • +
  • 死信队列应用
  • +
  • 插件实现方式
  • +
+
+
+
+
+
+
+ +

消息重试

+
+
+

处理消费失败的消息,实现可靠的消息处理机制。

+
    +
  • 重试策略
  • +
  • 最大重试次数
  • +
  • 死信处理
  • +
+
+
+
+
+
+
+ +

集群配置

+
+
+

搭建RabbitMQ集群,实现高可用和负载均衡。

+
    +
  • 镜像队列
  • +
  • 节点管理
  • +
  • HA策略
  • +
+
+
+
+
+
+
+ +

安全配置

+
+
+

实现RabbitMQ的安全访问控制。

+
    +
  • 用户权限管理
  • +
  • TLS加密
  • +
  • 防火墙配置
  • +
+
+
+
+
+
+ + +
+

代码示例

+ +
+
+

生产者配置

+
+
@Configuration
+public class RabbitMQConfig {
+
+    @Bean
+    public Queue helloQueue() {
+        return new Queue("hello", true);
+    }
+
+    @Bean
+    public DirectExchange directExchange() {
+        return new DirectExchange("direct.exchange");
+    }
+
+    @Bean
+    public Binding binding(Queue helloQueue,
+                         DirectExchange directExchange) {
+        return BindingBuilder.bind(helloQueue)
+               .to(directExchange)
+               .with("routing.key");
+    }
+}
+
+
+
+

消费者示例

+
+
@Component
+@RabbitListener(queues = "hello")
+public class MessageConsumer {
+
+    private static final Logger logger =
+        LoggerFactory.getLogger(MessageConsumer.class);
+
+    @RabbitHandler
+    public void receive(String message,
+                      Channel channel,
+                      @Header(AmqpHeaders.DELIVERY_TAG) long tag) {
+        try {
+            logger.info("Received message: {}", message);
+            // 业务处理逻辑
+            channel.basicAck(tag, false);
+        } catch (Exception e) {
+            channel.basicNack(tag, false, true);
+        }
+    }
+}
+
+
+
+ + +
+ + +
+

RabbitMQ架构概览

+
+ RabbitMQ架构图 +
+
+
+ +
生产者
+

发送消息到交换器

+
+
+
+
+ +
交换器
+

路由消息到队列

+
+
+
+
+ +
队列
+

存储待消费消息

+
+
+
+
+ +
消费者
+

从队列获取消息

+
+
+
+
+
+ + +
+

学习资源

+
+
+
+
+
推荐书籍
+
+
+
    +
  • + 《RabbitMQ实战指南》 + 中文 +
  • +
  • + 《RabbitMQ in Depth》 + 英文 +
  • +
  • + 《Spring Boot与RabbitMQ整合实践》 + 中文 +
  • +
+
+
+
+ +
+ +
+
+
视频教程
+
+
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+
+
+ + +
+
+
+

关于这个项目

+

这个RabbitMQ学习项目旨在通过实践帮助开发者掌握消息队列的核心概念和Spring Boot集成。

+

项目包含了从基础到高级的各种RabbitMQ特性实现,每个功能模块都有详细的文档说明和代码示例,适合学习和参考。

+ +
+

技术栈

+
+ Spring Boot 3.x + RabbitMQ 3.11 + Java 17 + Docker + Maven + Spring AMQP +
+
+ +
+

项目进度

+
+
基础功能 85% +
+
+
+
高级特性 70% +
+
+
+
环境配置 60% +
+
+
+
+
+
+ + +
+
+
+
+
RabbitMQ学习项目
+

一个使用Spring Boot和RabbitMQ构建的综合性学习项目,帮助开发者掌握消息队列的核心概念和实践应用。

+
+
+
快速链接
+ +
+
+
联系我
+

如有任何问题或建议,欢迎联系:

+
+ + + + +
+
+
+
+
+

© 2023 RabbitMQ学习项目. 保留所有权利.

+
+
+
+ + + + + \ No newline at end of file