(四)MMA(事件通知/模块间数据状态迁移/Meterialized Views/Saga Pattern)


文章目录

  • 项目地址
  • 一、事件通知
  • 二、数据状态迁移
    • 2.1 UserRegisteredIntegrationEvent
      • 1. 创建UserRegisteredIntegrationEvent(Users)
      • 2. UserRegisteredIntegrationEvent进行发布(Users)
      • 3. UserRegisteredIntegrationEventHandler(消费者模块)
      • 4. 测试
      • 5. 总结
  • 三、Meterialized Views
    • 3.1 添加Meterialized Views
      • 1. 创建实体
      • 2. 创建queryhandler
      • 3. 测试
  • 四、Saga Pattern
    • 4.1 创建CancelEventSaga(Events)
      • 1. CancelEventState
      • 2. 创建Saga 状态机
      • 3. EventCancellationStartedIntegrationEventHandler
    • 4.2 使用redis 作为Saga
      • 1. 安装Redis
      • 2. 给Saga配置redis


项目地址

  • 教程作者:
  • 教程地址:
  • 代码仓库地址:
  • 所用到的框架和插件:
dbt 
airflow

一、事件通知

二、数据状态迁移

在这里插入图片描述

  • 模块下的IntegrationEvents这个模块是专门用来负责数据状态迁移,一般是用来,同步到CRM系统或是邮件系统

2.1 UserRegisteredIntegrationEvent

1. 创建UserRegisteredIntegrationEvent(Users)

  • 这个类是用来定义该行为的对象
namespace Evently.Modules.Users.IntegrationEvents;
public sealed class UserRegisteredIntegrationEvent
 : IntegrationEvent
{
   
    public UserRegisteredIntegrationEvent(
        Guid id,
        DateTime occurredOnUtc,
        Guid userId,
        string email,
        string firstName,
        string lastName)
        : base(id, occurredOnUtc)
    {
   
        UserId = userId;
        Email = email;
        FirstName = firstName;
        LastName = lastName;
    }
    public Guid UserId {
    get; init; }
    public string Email {
    get; init; }
    public string FirstName {
    get; init; }
    public string LastName {
    get; init; }
}

2. UserRegisteredIntegrationEvent进行发布(Users)

  • 这里发布的是上面的UserRegisteredIntegrationEvent
    (四)MMA(事件通知/模块间数据状态迁移/Meterialized Views/Saga Pattern)_第1张图片

3. UserRegisteredIntegrationEventHandler(消费者模块)

  • 所有需要用到该数据的模块,都需要创建UserRegisteredIntegrationEventHandler,该系统中Ticketing和
namespace Evently.Modules.Ticketing.Presentation.Customers;
internal sealed class UserRegisteredIntegrationEventHandler(ISender sender)
    : IntegrationEventHandler<UserRegisteredIntegrationEvent>
{
   
    public override async Task Handle(
        UserRegisteredIntegrationEvent integrationEvent,
        CancellationToken cancellationToken = default)
    {
   
        Result result = await sender.Send(
            new CreateCustomerCommand(
                integrationEvent.UserId,
                integrationEvent.Email,
                integrationEvent.FirstName,
                integrationEvent.LastName),
            cancellationToken);
        if (result.IsFailure)
        {
   
            throw new EventlyException(nameof(CreateCustomerCommand), result.Error);
        }
    }
}
  • Attendance 模块
namespace Evently.Modules.Attendance.Presentation.Attendees;
internal sealed class UserRegisteredIntegrationEventHandler(ISender sender)
    : IntegrationEventHandler<UserRegisteredIntegrationEvent

你可能感兴趣的:(模块化单体,java,android,servlet)