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