nservicebus sagas - 试图理解目的和好处

nservicebus sagas - stuck trying to understand the purpose and benefit

我已经多次阅读网站上的文档。我一遍又一遍地阅读相同的文章,但我无法理解他们试图通过 sagas 实现什么。此外,互联网上几乎没有与此主题相关的资源。

但我完全无法理解定义所谓的 sagas 的目的和好处。我了解处理程序 (IHandleMessages) - 这些是拦截器。但是我不明白Saga是干什么用的。文档中的语言假设我应该知道一些特别的东西来理解这个想法,但我不知道。

有人可以用简单的语言向我解释,希望是现实生活中的例子,我必须或应该定义 Saga 的情况,这样做有什么好处?我创建了一个具有多个端点和 Saga 定义的应用程序,如示例所示,它可以工作(我猜)但我不明白这些 sagas 的定义是为了什么......在许多示例中,他们在 Saga class。为什么,为什么有人要故意造成超时?我不想在这里放任何代码片段,因为它不相关,我需要理解为什么我要使用 "Sagas" 什么意思?

谢谢。

NServiceBus Saga 是 Process Manager described in the Enterprise Integration Patterns book 的变体。

要了解何时使用 Saga,必须需要它。假设您仅使用常规消息处理程序来实现新用户注册过程。在某个时间点,您发现只有 40% 的 brand-new 注册人确认了他们的电子邮件地址并成为活跃用户帐户。有两件事您想解决。

  1. 通过发送提醒提醒新注册者在注册后 24 小时后确认他们的电子邮件。
  2. 在 48 小时内从数据存储中删除注册人信息(例如电子邮件)以符合 GDPR

现在如何使用常规消息处理程序来做到这一点?处理程序将收到初始请求(第一条消息,m1),通过生成一封带有确认 link 的电子邮件来启动注册,仅此而已。一旦处理程序完成,它就永远完成了。但是你的 process 还没有完成。这是一个 long-running 逻辑过程,必须跨越 48 小时才能完成。此时不再只是单一的消息处理,而是一个工作流。具有多个检查点的工作流程。类似于状态机。要从一种状态移动到另一种状态,必须满足特定条件。在 NServiceBus 的情况下,这些将是消息。 24 小时后发送提醒的消息(我们称之为 m2)不会由任何用户操作触发。这是一条 "system" 消息。应自动启动的定时消息。指示系统删除注册人信息的消息也是如此 if 验证 link 未激活。可以观察到主题:以后需要将消息安排到re-hydrate工作流,并从上次离开的状态继续。

这就是超时。这些是从上次在某个特定时间点离开的点到 re-hydrate/continue saga/workflow 的请求 - 分钟、小时、天、月、年。

这就是这种工作流程在传奇中的样子(过于简单,没有考虑所有边缘情况)。

class RegistrationWorkflow : 
  Saga<WorkflowState>,
  IAmStartedByMessages<RegisterUser>,
  IHandleMessages<ActivationReceived>,
  IHandleTimeouts<NoResponseFor24Hours>,
  IHandleTimeouts<NoResponseFor48Hours>
{
  protected override void ConfigureHowToFindSaga(SagaPropertyMapper<WorkflowState> mapper)
  {
    // omitted for simplicity, see message correlation
    // https://docs.particular.net/nservicebus/sagas/message-correlation
  }

  public async Task Handle(RegisterUser message, IMessageHandlerContext context)
  {
    Data.RegistrationId = message.RegistrationEmail;

    await RequestTimeout<NoResponseFor24Hours>(context, TimeSpan.FromHours(24));
  }

  public async Task Handle(ActivationReceived message, IMessageHandlerContext context)
  {
    Data.ConfirmationReceived = true;

    // email was confirmed and account was activated
    await context.Send(new PromoteCandidateToUser 
    { 
      CandidateEmail = Data.RegistrationEmail 
    });

    MarkAsComplete()
  }

  public async Task Timeout(NoResponseFor24Hours timeout, IMessageHandlerContext context)
  {
    if (Data.ConfirmationReceived)
    {
      return;
    }

    await context.Send(new SendReminderEmailToActivateAccount { Email = Data.RegistrationEmail });        

    await RequestTimeout(context, TimeSpan.FromHours(24), new NoResponseFor48Hours());
  }

  public async Task Timeout(NoResponseFor48Hours timeout, IMessageHandlerContext context)
  {
    if (Data.ConfirmationReceived)
    {
      return;
    }

    context.Send(new CleanupRegistrationInformationForGDPRCompliancy 
    { 
      RegistrationEmail = Data.RegistrationEmail 
    });

    MarkAsComplete();
  }
}

因为这是一个状态机,状态在 Saga 调用之间持久化。调用可能由 saga 可以处理的消息(RegisterUserActivationReceived)或到期的超时(NoResponseFor24HoursNoResponseFor48Hours)引起。对于这个特定的传奇,状态由以下 POCO 定义:

class WorkflowState : ContainSagaData
{
  public string RegistrationEmail { get; set; }
  public bool ConfirmationReceived { get; set; }
}

超时只不过是被推迟的普通 IMessage。此示例中使用的超时为

class NoResponseFor24Hours : IMessage {}
class NoResponseFor48Hours : IMessage {}

希望这能阐明 Sagas in general, what Timeouts are and how they are used. I did not go into Message Correlation, Saga Concurrency 的概念,以及其他一些细节,这些细节可以在您提到的文档站点上找到。这将我们带到下一点。

I have read multiple times the documentation on their website. It is absolutely terrible. I am reading again and again the same articles and I cannot comprehend what they are trying to achieve.

该站点具有您绝对应该提供的反馈机制。

Besides there almost no resources in internet related to this subject.

希望看到您就此主题发表博客(或一系列文章)。通过这样做,您将做出积极的贡献。

完全免责声明:我在 NServiceBus 上工作