# 后端学习 **Repository Path**: darling_da/backend-learning ## Basic Information - **Project Name**: 后端学习 - **Description**: 后端学习:专注于后端技术的开源仓库,涵盖Java、Python、Node.js等语言,提供项目示例、教程和最佳实践。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: rabbit-mq - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-05-14 - **Last Updated**: 2026-05-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # MQ-Demo RabbitMQ 消息队列演示项目,基于 Spring Boot 和 Spring AMQP。 ## 项目结构 ``` mq-demo/ ├── pom.xml # 父级 Maven 配置 ├── publisher/ # 消息生产者模块 │ ├── pom.xml │ └── src/ │ ├── main/java/ │ └── test/java/ # 测试类 └── consumer/ # 消息消费者模块 ├── pom.xml └── src/ └── main/java/ ``` ## 技术栈 - **Spring Boot**: 2.7.12 - **Spring AMQP**: RabbitMQ 消息队列 - **Java**: 8 - **构建工具**: Maven - **Lombok**: 简化代码 ## 消息队列 ### 1. simple.queue 简单队列,点对点消息传递模式。 ### 2. work.queue 工作队列,支持多个消费者竞争消费消息,实现负载均衡。 ### 3. star.fanout (交换机) Fanout 广播模式,向所有绑定的队列发送消息。 ## 配置说明 RabbitMQ 连接配置位于各模块的 `application-local.yml` 文件中: ```yaml spring: rabbitmq: host: your-rabbitmq-host port: 5672 virtual-host: /your-vhost username: your-username password: your-password listener: simple: prefetch: 1 # 每次最多消费 1 条消息 ``` > ⚠️ 请根据实际环境修改 RabbitMQ 连接信息。 ## 快速开始 ### 1. 安装依赖 ```bash mvn clean install ``` ### 2. 启动消费者 ```bash cd consumer mvn spring-boot:run ``` ### 3. 运行生产者测试 在 `publisher` 模块中运行 `SpringAmqpTest` 测试类: - `testSendMessage2Queue()`: 向 simple.queue 发送消息 - `testWorkQueue()`: 向 work.queue 发送 50 条消息 - `testSendFanout()`: 向 star.fanout 交换机发送广播消息 ## 模块说明 ### Publisher (生产者) - `PublisherApplication`: 启动类 - `SpringAmqpTest`: 消息发送测试 ### Consumer (消费者) - `ConsumerApplication`: 启动类 - `MqListener`: 消息监听器 - `listenSimpleQueue()`: 监听 simple.queue - `listenSimpleQueue1()` / `listenSimpleQueue2()`: 监听 work.queue(竞争消费) ## 依赖关系 ```xml org.springframework.boot spring-boot-starter-amqp org.projectlombok lombok org.springframework.boot spring-boot-starter-test ```