简体   繁体   English

使用 AWS Lambdas 而不是微服务时,如何在事件溯源中实现聚合之间的通信

[英]How to achieve a communication between aggregates in event sourcing when working with AWS Lambdas rather than micro-services

So I have a background in working with event sourcing and microservices.所以我有使用事件溯源和微服务的背景。 And usually the best way to enforce bounded context yet be able to make your aggregate communicate is to have either some kind of Choreography or some kind of Orchestration.通常,强制执行有界上下文但能够使您的聚合通信的最佳方法是拥有某种编排或某种编排。

In a Choreography Aggregate A will raise eventA that Aggregate B will listen to and handle and then after doing whatever needed to be done, it will raise eventB and A will listen and handle it and proceed.在编排中,聚合 A 将引发聚合 B 将侦听和处理的 eventA,然后在完成任何需要完成的操作后,它将引发 eventB,A 将侦听并处理它并继续。 It's effective, respects event-sourcing and DDD rules.它是有效的,尊重事件溯源和 DDD 规则。

In an Orchestration, Aggregate A will raise eventA that the orchestrator O will listen and handle and then issue a command B to Aggregate B which in return will run what's needed and raise Event B, orchestrator O will handle that event and issue a command A and so on... It adds a level of complexity but it's great for an added level of segregation, also this way Aggregate A and B are not listening/handling each other events.在编排中,聚合 A 将引发编排器 O 将侦听和处理的事件 A,然后向聚合 B 发出命令 B,聚合 B 作为回报将运行所需的内容并引发事件 B,编排器 O 将处理该事件并发出命令 A 和依此类推...它增加了一定程度的复杂性,但它非常适合增加隔离级别,这样聚合 A 和 B 也不会互相侦听/处理彼此的事件。

Obvs these 2 methods have their own pros and cons, but both work perfectly in a microservice context. Obvs 这两种方法各有优缺点,但都可以在微服务环境中完美运行。

The issue I'm facing is that for me there is no context.我面临的问题是对我来说没有上下文。 I'm working with AWS lambdas, whenever an event is pushed to the store I will have a lambda listening to db(event store) changes and then do something.我正在使用 AWS lambdas,每当一个事件被推送到商店时,我都会有一个 lambda 来监听 db(event store) 的变化,然后做一些事情。 It was working perfectly until I needed to add a second aggregate.在我需要添加第二个聚合之前,它运行良好。

And now to achieve a choreography or an orchestration, I either need a context(which is not a thing for lambdas) and an event bus, or I need to add a lambda for every event, that would lead for total chaos.现在要实现编排或编排,我需要一个上下文(这不是 lambdas 的东西)和一个事件总线,或者我需要为每个事件添加一个 lambda,这将导致完全混乱。

Like if Aggregate A needs something from Agg B before continuing its flow it will push an event to the event store and I will have to handle the event with a new lambda so for every type of interaction between Agg A and Agg B, I will need 2 lambdas.就像如果聚合 A 在继续其流程之前需要来自 Agg B 的东西,它会将事件推送到事件存储,我将不得不使用新的 lambda 处理事件,因此对于 Agg A 和 Agg B 之间的每种类型的交互,我需要2个拉姆达。

Maybe I'm missing something, after all I'm new in AWS lambdas and more used to working with microservices.也许我错过了一些东西,毕竟我是 AWS lambdas 的新手,而且更习惯于使用微服务。

Perhaps what you're after is a Process Manager:也许您所追求的是流程管理器:

(…) a process manager is a class that coordinates the behavior of the aggregates in the domain. (...) 进程管理器是一个 class,它协调域中聚合的行为。 A process manager subscribes to the events that the aggregates raise, and then follow a simple set of rules to determine which command or commands to send.流程管理器订阅聚合引发的事件,然后遵循一组简单的规则来确定要发送的命令。 The process manager does not contain any business logic;流程管理器不包含任何业务逻辑; it simply contains logic to determine the next command to send.它仅包含确定要发送的下一个命令的逻辑。 The process manager is implemented as a state machine, so when it responds to an event, it can change its internal state in addition to sending a new command.进程管理器实现为 state 机器,因此当它响应事件时,除了发送新命令外,它还可以更改其内部 state。

The above definition can be found in Microsoft's Exploring CQRS and Event Sourcing .上面的定义可以在微软的Exploring CQRS and Event Sourcing中找到。 This is a way of having orchestration in an event-driven system.这是在事件驱动系统中进行编排的一种方式。 The original definition, AFAIK, can be found in Gregor Hohpe's Enterprise Integration Patterns .原始定义 AFAIK 可以在 Gregor Hohpe 的Enterprise Integration Patterns中找到。

In AWS land, you'd have your lambda(s) reacting to those events and firing off commands (either via a command bus, if you have such concept in your system, or by directly invoking other lambdas).在 AWS 领域,您将让您的 lambda(s) 对这些事件做出反应并触发命令(通过命令总线,如果您的系统中有这样的概念,或者通过直接调用其他 lambdas)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM