2026-02-11 16:50:46 +08:00
|
|
|
|
# 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分钟内有效,如非本人操作请忽略。
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 使用方式
|
|
|
|
|
|
|
2026-02-13 10:23:17 +08:00
|
|
|
|
### 使用Java客户端发送消息
|
2026-02-11 16:50:46 +08:00
|
|
|
|
|
|
|
|
|
|
参考 `MessageProducerTest.java` 中的测试方法:
|
|
|
|
|
|
|
|
|
|
|
|
```java
|
2026-02-13 10:23:17 +08:00
|
|
|
|
@Autowired
|
|
|
|
|
|
private MessageProducerService messageProducerService;
|
|
|
|
|
|
|
2026-02-11 16:50:46 +08:00
|
|
|
|
@Test
|
|
|
|
|
|
public void testSendSmsMessage() {
|
|
|
|
|
|
MessageDTO messageDTO = new MessageDTO();
|
|
|
|
|
|
messageDTO.setReceiver("13800138000");
|
|
|
|
|
|
messageDTO.setChannelType("SMS");
|
|
|
|
|
|
messageDTO.setMsgType("VERIFICATION");
|
|
|
|
|
|
messageDTO.setSourceSystem("MessageHub");
|
|
|
|
|
|
|
|
|
|
|
|
messageProducerService.sendSmsMessage(messageDTO);
|
|
|
|
|
|
}
|
2026-02-13 10:23:17 +08:00
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
public void testSendEmailMessage() {
|
|
|
|
|
|
MessageDTO messageDTO = new MessageDTO();
|
|
|
|
|
|
messageDTO.setReceiver("test@example.com");
|
|
|
|
|
|
messageDTO.setChannelType("EMAIL");
|
|
|
|
|
|
messageDTO.setMsgType("NOTIFICATION");
|
|
|
|
|
|
messageDTO.setSourceSystem("MessageHub");
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> params = new HashMap<>();
|
|
|
|
|
|
params.put("message", "这是一封测试邮件");
|
|
|
|
|
|
messageDTO.setTemplateParams(params);
|
|
|
|
|
|
|
|
|
|
|
|
messageProducerService.sendEmailMessage(messageDTO);
|
|
|
|
|
|
}
|
2026-02-11 16:50:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-02-13 10:23:17 +08:00
|
|
|
|
### 在业务代码中使用
|
2026-02-11 16:50:46 +08:00
|
|
|
|
|
2026-02-13 10:23:17 +08:00
|
|
|
|
```java
|
|
|
|
|
|
@Service
|
|
|
|
|
|
public class OrderService {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private MessageProducerService messageProducerService;
|
|
|
|
|
|
|
|
|
|
|
|
public void sendOrderNotification(String phone, String orderNo) {
|
|
|
|
|
|
MessageDTO messageDTO = new MessageDTO();
|
|
|
|
|
|
messageDTO.setReceiver(phone);
|
|
|
|
|
|
messageDTO.setChannelType("SMS");
|
|
|
|
|
|
messageDTO.setMsgType("NOTIFICATION");
|
|
|
|
|
|
messageDTO.setTemplateCode("ORDER_NOTIFY");
|
|
|
|
|
|
messageDTO.setSourceSystem("OrderSystem");
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> params = new HashMap<>();
|
|
|
|
|
|
params.put("orderNo", orderNo);
|
|
|
|
|
|
params.put("productName", "iPhone 15 Pro");
|
|
|
|
|
|
params.put("amount", "8999.00");
|
|
|
|
|
|
messageDTO.setTemplateParams(params);
|
|
|
|
|
|
|
|
|
|
|
|
messageProducerService.sendSmsMessage(messageDTO);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-11 16:50:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 队列配置
|
|
|
|
|
|
|
2026-02-13 10:23:17 +08:00
|
|
|
|
### 统一消息交换机
|
|
|
|
|
|
- **交换机名称**: messageExchange
|
|
|
|
|
|
- **交换机类型**: Topic
|
|
|
|
|
|
|
2026-02-11 16:50:46 +08:00
|
|
|
|
### 短信队列
|
2026-02-13 10:23:17 +08:00
|
|
|
|
- **队列名称**: smsQueue
|
|
|
|
|
|
- **路由键模式**: sms.# (匹配所有以sms.开头的routing key)
|
|
|
|
|
|
- **监听器**: SmsQueueListener
|
|
|
|
|
|
- **支持的routing key**:
|
|
|
|
|
|
- sms.verification(验证码短信)
|
|
|
|
|
|
- sms.notification(通知短信)
|
|
|
|
|
|
- sms.promotion(营销短信)
|
|
|
|
|
|
- sms.alert(告警短信)
|
2026-02-11 16:50:46 +08:00
|
|
|
|
|
|
|
|
|
|
### 邮件队列
|
2026-02-13 10:23:17 +08:00
|
|
|
|
- **队列名称**: emailQueue
|
|
|
|
|
|
- **路由键模式**: email.# (匹配所有以email.开头的routing key)
|
|
|
|
|
|
- **监听器**: EmailQueueListener
|
|
|
|
|
|
- **支持的routing key**:
|
|
|
|
|
|
- email.verification(验证码邮件)
|
|
|
|
|
|
- email.notification(通知邮件)
|
|
|
|
|
|
- email.promotion(营销邮件)
|
|
|
|
|
|
- email.alert(告警邮件)
|