Mico.Demo/MassTransit.Message/OrderInventoryNotificationR...

135 lines
3.8 KiB
C#
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.

using System;
namespace MassTransit.Message;
/// <summary>
/// ============================================================
/// Point-to-Point 通信模式示例 - 消息契约
/// ============================================================
///
/// Point-to-Point点对点通信模式特点
///
/// 1. 单向消息传递
/// - 发送方只发送消息,不等待响应
/// - 消息发送后立即返回,提高性能
/// - 适用于不需要立即知道处理结果的场景
///
/// 2. 消息只被一个消费者处理
/// - RabbitMQ 队列确保消息只被一个消费者消费
/// - 多个消费者会形成竞争关系,每个消息只被处理一次
/// - 适合负载均衡和水平扩展
///
/// 3. 高性能
/// - 无需等待响应,消息发送后立即返回
/// - 减少网络往返时间RTT
/// - 提高系统吞吐量
///
/// 4. 可靠性
/// - 消息持久化保证消息不丢失
/// - 死信队列DLQ处理失败的消息
/// - 重试机制处理临时性错误
///
/// 使用场景:
/// - 订单创建后异步通知库存服务扣减库存
/// - 用户注册后发送欢迎邮件(不等待邮件发送完成)
/// - 数据同步和事件通知
/// - 日志和监控数据收集
///
/// 与 Request-Response 模式的区别:
/// - Request-Response: 使用 IRequestClient.GetResponse<T>(),等待响应
/// - Point-to-Point: 使用 IBus.Send(),不等待响应
///
/// ============================================================
public record OrderInventoryNotificationRequest
{
/// <summary>
/// 消息唯一标识符
/// 用于消息去重和追踪
/// </summary>
public string MessageId { get; init; } = Guid.NewGuid().ToString("N");
/// <summary>
/// 关联ID用于关联相关消息
/// 例如订单ID、用户ID等业务标识
/// </summary>
public string CorrelationId { get; init; } = string.Empty;
/// <summary>
/// 通知类型
/// - OrderCreated: 订单创建
/// - OrderCancelled: 订单取消
/// </summary>
public string NotificationType { get; init; } = string.Empty;
/// <summary>
/// 产品信息列表
/// </summary>
public List<ProductInfo> Products { get; init; } = new();
/// <summary>
/// 消息创建时间
/// </summary>
public DateTime CreatedAt { get; init; } = DateTime.UtcNow;
/// <summary>
/// 无参构造函数(用于反序列化)
/// </summary>
public OrderInventoryNotificationRequest() { }
/// <summary>
/// 带参数构造函数
/// </summary>
public OrderInventoryNotificationRequest(
string correlationId,
string notificationType,
List<ProductInfo> products)
{
CorrelationId = correlationId;
NotificationType = notificationType;
Products = products;
}
}
/// <summary>
/// 产品信息
/// </summary>
public record ProductInfo
{
/// <summary>
/// 产品ID
/// </summary>
public string ProductId { get; init; } = string.Empty;
/// <summary>
/// 产品名称
/// </summary>
public string ProductName { get; init; } = string.Empty;
/// <summary>
/// 操作数量
/// 正数表示增加库存,负数表示减少库存
/// </summary>
public int Quantity { get; init; }
/// <summary>
/// 操作原因
/// </summary>
public string Reason { get; init; } = string.Empty;
/// <summary>
/// 无参构造函数(用于反序列化)
/// </summary>
public ProductInfo() { }
/// <summary>
/// 带参数构造函数
/// </summary>
public ProductInfo(string productId, string productName, int quantity, string reason)
{
ProductId = productId;
ProductName = productName;
Quantity = quantity;
Reason = reason;
}
}