180 lines
4.1 KiB
Markdown
180 lines
4.1 KiB
Markdown
# MassTransit Demo 测试说明
|
||
|
||
## 项目结构
|
||
|
||
```
|
||
MassTransit.Demo/
|
||
├── MassTransitDemo.Contracts/ # 消息契约项目
|
||
│ ├── PublishSubscribeMessage.cs # 发布/订阅消息契约
|
||
│ ├── RequestMessage.cs # 请求消息契约
|
||
│ └── ResponseMessage.cs # 响应消息契约
|
||
├── MassTransitDemo.Publisher/ # 消息发布者API
|
||
│ ├── Controllers/
|
||
│ │ └── MessageController.cs # 消息发布控制器
|
||
│ ├── Program.cs # 服务配置
|
||
│ └── appsettings.json # 配置文件
|
||
└── MassTransitDemo.Consumer/ # 消息消费者API
|
||
├── Consumers/
|
||
│ ├── PublishSubscribeConsumer1.cs # 消费者1
|
||
│ ├── PublishSubscribeConsumer2.cs # 消费者2
|
||
│ └── RequestResponseConsumer.cs # 请求/响应消费者
|
||
├── Program.cs # 服务配置
|
||
└── appsettings.json # 配置文件
|
||
```
|
||
|
||
## 前置条件
|
||
|
||
1. 确保RabbitMQ服务器已启动并运行在:192.168.2.7:5672
|
||
2. 确保使用用户名:rabbit,密码:qwe123!@# 可以正常连接
|
||
3. 安装.NET 10 SDK
|
||
|
||
## 运行步骤
|
||
|
||
### 1. 还原依赖
|
||
|
||
```bash
|
||
dotnet restore
|
||
```
|
||
|
||
### 2. 构建解决方案
|
||
|
||
```bash
|
||
dotnet build
|
||
```
|
||
|
||
### 3. 启动Consumer项目
|
||
|
||
打开一个终端,进入Consumer项目目录:
|
||
|
||
```bash
|
||
cd MassTransitDemo.Consumer
|
||
dotnet run
|
||
```
|
||
|
||
Consumer项目将监听RabbitMQ队列并等待消息。
|
||
|
||
### 4. 启动Publisher项目
|
||
|
||
打开另一个终端,进入Publisher项目目录:
|
||
|
||
```bash
|
||
cd MassTransitDemo.Publisher
|
||
dotnet run
|
||
```
|
||
|
||
Publisher项目将提供API接口用于发送消息。
|
||
|
||
## API测试
|
||
|
||
### 发布/订阅模式测试
|
||
|
||
使用Postman或curl发送POST请求:
|
||
|
||
**URL:** `POST /api/message/publish`
|
||
|
||
**请求体:**
|
||
```json
|
||
{
|
||
"title": "测试消息",
|
||
"content": "这是一条发布/订阅模式的测试消息",
|
||
"priority": 1
|
||
}
|
||
```
|
||
|
||
**curl示例:**
|
||
```bash
|
||
curl -X POST "https://localhost:<port>/api/message/publish" \
|
||
-H "Content-Type: application/json" \
|
||
-d '{
|
||
"title": "测试消息",
|
||
"content": "这是一条发布/订阅模式的测试消息",
|
||
"priority": 1
|
||
}'
|
||
```
|
||
|
||
**预期结果:**
|
||
- 响应包含成功状态和MessageId
|
||
- Consumer项目的日志中会看到【消费者1】和【消费者2】都接收到了消息
|
||
|
||
### 请求/响应模式测试
|
||
|
||
**URL:** `POST /api/message/request`
|
||
|
||
**支持的操作类型:**
|
||
- `echo`: 原样返回参数
|
||
- `uppercase`: 将参数转换为大写
|
||
- `lowercase`: 将参数转换为小写
|
||
- `reverse`: 反转参数字符串
|
||
- `length`: 返回参数长度
|
||
|
||
**请求体示例 (echo):**
|
||
```json
|
||
{
|
||
"operation": "echo",
|
||
"parameters": "Hello, MassTransit!"
|
||
}
|
||
```
|
||
|
||
**请求体示例 (uppercase):**
|
||
```json
|
||
{
|
||
"operation": "uppercase",
|
||
"parameters": "Hello, MassTransit!"
|
||
}
|
||
```
|
||
|
||
**curl示例:**
|
||
```bash
|
||
curl -X POST "https://localhost:<port>/api/message/request" \
|
||
-H "Content-Type: application/json" \
|
||
-d '{
|
||
"operation": "uppercase",
|
||
"parameters": "Hello, MassTransit!"
|
||
}'
|
||
```
|
||
|
||
**预期结果:**
|
||
- 响应包含成功状态、RequestId和处理后的数据
|
||
- Consumer项目的日志中会看到【请求/响应消费者】处理请求的过程
|
||
|
||
## RabbitMQ配置说明
|
||
|
||
配置文件位置:`appsettings.json`
|
||
|
||
```json
|
||
{
|
||
"RabbitMQ": {
|
||
"Host": "192.168.2.7",
|
||
"Port": 5672,
|
||
"Username": "rabbit",
|
||
"Password": "qwe123!@#",
|
||
"VirtualHost": "/"
|
||
}
|
||
}
|
||
```
|
||
|
||
如需修改RabbitMQ连接信息,请更新Publisher和Consumer项目的`appsettings.json`文件。
|
||
|
||
## 消息队列说明
|
||
|
||
- `publish-subscribe-queue-1`: 消费者1的队列
|
||
- `publish-subscribe-queue-2`: 消费者2的队列
|
||
- `request-response-queue`: 请求/响应队列
|
||
|
||
## 故障排查
|
||
|
||
1. **连接RabbitMQ失败**
|
||
- 检查RabbitMQ服务器是否启动
|
||
- 检查IP地址、端口、用户名和密码是否正确
|
||
- 检查网络连接是否正常
|
||
|
||
2. **消息没有被消费**
|
||
- 确保Consumer项目正在运行
|
||
- 检查RabbitMQ管理界面,确认队列存在且有绑定
|
||
- 查看Consumer项目的日志输出
|
||
|
||
3. **请求超时**
|
||
- 确保Consumer项目正在运行
|
||
- 检查网络延迟
|
||
- 确认请求队列配置正确
|