在上一篇文章中,我讨论了将PostgreSQL发件箱迁移到Brighter V10的过程。本文将专门聚焦于MySQL发件箱的迁移流程。
要求
.NET 8或更高版本
一个包含以下NuGet包的.NET项目
- Paramore.Brighter.Outbox.Hosting - 为Brighter应用程序提供实现发件箱模式的基础架构
- Paramore.Brighter.Outbox.MySql - 专门为MySQL数据库实现的发件箱存储
- Paramore.Brighter.MessagingGateway.Kafka - 提供Kafka消息传递功能
- Paramore.Brighter.ServiceActivator.Extensions.DependencyInjection - 将Brighter与Microsoft的依赖注入框架集成
- Paramore.Brighter.ServiceActivator.Extensions.Hosting - 支持与Microsoft的通用主机基础设施集成
- Serilog.AspNetCore - 为ASP.NET Core应用程序提供结构化日志记录功能
Brighter回顾
在继续讨论MySQL发件箱配置之前,让我们回顾一下关于Brighter的基本知识。
请求(命令/事件)
使用IRequest定义消息:
public class OrderPlaced() : Event(Id.Random())
{
public string OrderId { get; set; } = string.Empty;
public decimal Value { get; set; }
}
- 命令:单接收者操作(例如,SendEmail)
- 事件:广播通知(例如,OrderShipped)
消息映射器(可选)
在Brighter消息和应用程序对象之间进行转换,默认情况下Brighter将使用JSON序列化器
public class OrderPlacedMapper : IAmAMessageMapper<OrderPlaced>, IAmAMessageMapperAsync<OrderPlaced>
{ ... }
请求处理器
处理传入的消息:
public class OrderPlaceHandler(ILogger<OrderPlaceHandler> logger) : RequestHandler<OrderPlaced>
{
public override Greeting Handle(Greeting command)
{
logger.LogInformation("{OrderId} placed with value {OrderValue}", command.OrderId, command.Value);
return base.Handle(command);
}
}
在Brighter中使用发件箱
在配置发件箱之前,先了解如何与它交互:
通过发件箱发布消息
Brighter提供了`DepositPostAsync`方法将消息存储在发件箱中
await commandProcessor.DepositPostAsync(
new OrderPlaced { OrderId = "ORD-123", Value = 99.99m },
cancellationToken: cancellationToken);
这会将消息存储在您的MySQL发件箱表中,供后续处理。

最低0.47元/天 解锁文章
843

被折叠的 条评论
为什么被折叠?



