Files
JayuCloud/MessageHub/MESSAGE_EXAMPLES.md
T
xiaowang a41985582b 注释优化
短信发送队列优化
邮件发送队列实现
2026-02-11 16:50:46 +08:00

282 lines
6.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# MessageHub 消息队列示例消息体
## 短信消息示例
### 1. 普通短信消息(无模板)
```json
{
"receiver": "13800138000",
"channelType": "SMS",
"msgType": "NOTIFICATION",
"templateCode": "",
"templateParams": {
"message": "您的订单已发货,请注意查收"
},
"sourceSystem": "OrderSystem",
"uniqueId": "msg_20250211_001"
}
```
### 2. 验证码短信(使用模板)
```json
{
"receiver": "13800138000",
"channelType": "SMS",
"msgType": "VERIFICATION",
"templateCode": "VERIFY_CODE",
"templateParams": {
"code": "123456",
"minutes": "10"
},
"sourceSystem": "UserCenter",
"uniqueId": "msg_20250211_002"
}
```
### 3. 通知短信(使用模板)
```json
{
"receiver": "13800138000",
"channelType": "SMS",
"msgType": "NOTIFICATION",
"templateCode": "ORDER_NOTIFY",
"templateParams": {
"orderNo": "20250211001",
"productName": "iPhone 15 Pro",
"amount": "8999.00"
},
"sourceSystem": "OrderSystem",
"uniqueId": "msg_20250211_003"
}
```
### 4. 营销短信(使用模板)
```json
{
"receiver": "13800138000",
"channelType": "SMS",
"msgType": "PROMOTION",
"templateCode": "PROMOTION_NOTIFY",
"templateParams": {
"productName": "春季大促",
"discount": "8折",
"endDate": "2025-02-20"
},
"sourceSystem": "MarketingSystem",
"uniqueId": "msg_20250211_004"
}
```
### 5. 告警短信(使用模板)
```json
{
"receiver": "13800138000",
"channelType": "SMS",
"msgType": "ALERT",
"templateCode": "SYSTEM_ALERT",
"templateParams": {
"serverName": "web-server-01",
"alertType": "CPU使用率过高",
"value": "95%"
},
"sourceSystem": "MonitorSystem",
"uniqueId": "msg_20250211_005"
}
```
## 邮件消息示例
### 1. 普通邮件消息(无模板)
```json
{
"receiver": "user@example.com",
"channelType": "EMAIL",
"msgType": "NOTIFICATION",
"templateCode": "",
"templateParams": {
"message": "您的账户已成功注册,欢迎加入我们!"
},
"sourceSystem": "UserCenter",
"uniqueId": "msg_20250211_006"
}
```
### 2. 验证码邮件(使用模板)
```json
{
"receiver": "user@example.com",
"channelType": "EMAIL",
"msgType": "VERIFICATION",
"templateCode": "EMAIL_VERIFY",
"templateParams": {
"code": "123456",
"minutes": "10"
},
"sourceSystem": "UserCenter",
"uniqueId": "msg_20250211_007"
}
```
### 3. 订单通知邮件(使用模板)
```json
{
"receiver": "user@example.com",
"channelType": "EMAIL",
"msgType": "NOTIFICATION",
"templateCode": "ORDER_EMAIL",
"templateParams": {
"orderNo": "20250211001",
"productName": "iPhone 15 Pro",
"amount": "8999.00",
"address": "北京市朝阳区xxx街道xxx号"
},
"sourceSystem": "OrderSystem",
"uniqueId": "msg_20250211_008"
}
```
### 4. 营销邮件(使用模板)
```json
{
"receiver": "user@example.com",
"channelType": "EMAIL",
"msgType": "PROMOTION",
"templateCode": "PROMOTION_EMAIL",
"templateParams": {
"userName": "张三",
"productName": "春季大促",
"discount": "8折",
"link": "https://example.com/promotion"
},
"sourceSystem": "MarketingSystem",
"uniqueId": "msg_20250211_009"
}
```
### 5. 告警邮件(使用模板)
```json
{
"receiver": "admin@example.com",
"channelType": "EMAIL",
"msgType": "ALERT",
"templateCode": "SYSTEM_ALERT_EMAIL",
"templateParams": {
"serverName": "web-server-01",
"alertType": "CPU使用率过高",
"value": "95%",
"time": "2025-02-11 16:30:00"
},
"sourceSystem": "MonitorSystem",
"uniqueId": "msg_20250211_010"
}
```
## 字段说明
| 字段名 | 类型 | 必填 | 说明 |
|--------|------|------|------|
| receiver | String | 是 | 接收者,短信为手机号,邮件为邮箱地址 |
| channelType | String | 是 | 渠道类型:SMS(短信)、EMAIL(邮件)、PUSH(推送)、WECHAT(微信) |
| msgType | String | 是 | 消息类型:NOTIFICATION(通知)、VERIFICATION(验证码)、PROMOTION(营销)、ALERT(告警) |
| templateCode | String | 否 | 模板编码,如果使用模板消息则必填 |
| templateParams | Map | 否 | 模板参数,key-value形式,用于模板占位符替换 |
| sourceSystem | String | 是 | 来源系统,标识消息来源 |
| uniqueId | String | 是 | 唯一标识,用于消息去重和追踪 |
## 消息类型说明
### channelType(渠道类型)
- **SMS**: 短信消息
- **EMAIL**: 邮件消息
- **PUSH**: 推送消息
- **WECHAT**: 微信消息
### msgType(消息类型)
- **NOTIFICATION**: 通知类消息,如订单通知、系统通知等
- **VERIFICATION**: 验证码类消息,如注册验证码、登录验证码等
- **PROMOTION**: 营销类消息,如促销活动、产品推荐等
- **ALERT**: 告警类消息,如系统告警、异常通知等
## 模板占位符说明
模板消息使用 `${key}` 格式的占位符,例如:
模板内容:
```
您的验证码是${code},${minutes}分钟内有效,如非本人操作请忽略。
```
对应的 templateParams:
```json
{
"code": "123456",
"minutes": "10"
}
```
替换后的内容:
```
您的验证码是123456,10分钟内有效,如非本人操作请忽略。
```
## 使用方式
### 方式一:使用测试类发送消息
参考 `MessageProducerTest.java` 中的测试方法:
```java
@Test
public void testSendSmsMessage() {
MessageDTO messageDTO = new MessageDTO();
messageDTO.setReceiver("13800138000");
messageDTO.setChannelType("SMS");
messageDTO.setMsgType("VERIFICATION");
messageDTO.setSourceSystem("MessageHub");
messageProducerService.sendSmsMessage(messageDTO);
}
```
### 方式二:使用RabbitMQ管理界面发送消息
1. 登录RabbitMQ管理界面:http://192.168.5.15:15672
2. 进入Queues页面
3. 选择对应的队列(smsQueue 或 emailQueue)
4. 点击"Publish message"
5. 将JSON消息体粘贴到Payload中
6. 点击"Publish message"发送
### 方式三:使用RabbitMQ命令行工具发送消息
```bash
# 发送短信消息
rabbitmqadmin publish exchange=smsExchange routing_key=sms.send payload='{"receiver":"13800138000","channelType":"SMS","msgType":"VERIFICATION","sourceSystem":"TestSystem","uniqueId":"test_001"}'
# 发送邮件消息
rabbitmqadmin publish exchange=emailExchange routing_key=email.send payload='{"receiver":"test@example.com","channelType":"EMAIL","msgType":"NOTIFICATION","sourceSystem":"TestSystem","uniqueId":"test_002"}'
```
## 队列配置
### 短信队列
- 队列名称:smsQueue
- 交换机名称:smsExchange
- 路由键:sms.send
- 监听器:SmsQueueListener
### 邮件队列
- 队列名称:emailQueue
- 交换机名称:emailExchange
- 路由键:email.send
- 监听器:EmailQueueListener