为了这个问题,我正在做一个项目,其中有两个微服务:
此外,还有一个RabbitMQ消息代理。
在OrderService中,我们使用了Axon框架进行事件源和CQRS。
现在,我们希望使用sagas来管理OrderService和InvoiceService之间的事务。
根据我所读到的,为了进行此更改,我们需要执行以下操作:
SimpleCommandBus -> DistributedCommandBus切换这是我们遇到麻烦的第四点:发票服务由一个不愿进行更改的单独团队维护。
在这种情况下,使用message而不是命令网关是否可行。例如,与其做这样的事情:
public class OrderManagementSaga {
private boolean paid = false;
private boolean delivered = false;
@Inject
private transient CommandGateway commandGateway;
@StartSaga
@SagaEventHandler(associationProperty = "orderId")
public void handle(OrderCreatedEvent event) {
// client generated identifiers
ShippingId shipmentId = createShipmentId();
InvoiceId invoiceId = createInvoiceId();
// associate the Saga with these values, before sending the commands
SagaLifecycle.associateWith("shipmentId", shipmentId);
SagaLifecycle.associateWith("invoiceId", invoiceId);
// send the commands
commandGateway.send(new PrepareShippingCommand(...));
commandGateway.send(new CreateInvoiceCommand(...));
}
@SagaEventHandler(associationProperty = "shipmentId")
public void handle(ShippingArrivedEvent event) {
delivered = true;
if (paid) { SagaLifecycle.end(); }
}
@SagaEventHandler(associationProperty = "invoiceId")
public void handle(InvoicePaidEvent event) {
paid = true;
if (delivered) { SagaLifecycle.end(); }
}
// ...
}我们会这样做:
public class OrderManagementSaga {
private boolean paid = false;
private boolean delivered = false;
@Inject
private transient RabbitTemplate rabbitTemplate;
@StartSaga
@SagaEventHandler(associationProperty = "orderId")
public void handle(OrderCreatedEvent event) {
// client generated identifiers
ShippingId shipmentId = createShipmentId();
InvoiceId invoiceId = createInvoiceId();
// associate the Saga with these values, before sending the commands
SagaLifecycle.associateWith("shipmentId", shipmentId);
SagaLifecycle.associateWith("invoiceId", invoiceId);
// send the commands
rabbitTemplate.convertAndSend(new PrepareShippingCommand(...));
rabbitTemplate.convertAndSend(new CreateInvoiceCommand(...));
}
@SagaEventHandler(associationProperty = "shipmentId")
public void handle(ShippingArrivedEvent event) {
delivered = true;
if (paid) { SagaLifecycle.end(); }
}
@SagaEventHandler(associationProperty = "invoiceId")
public void handle(InvoicePaidEvent event) {
paid = true;
if (delivered) { SagaLifecycle.end(); }
}
// ...
}在这种情况下,当我们在交换中接收到来自InvoiceService的消息时,我们将在事件网关上或使用SpringAMQPPublisher发布相应的事件。
问题
发布于 2020-06-29 09:35:59
首先,并不是完全针对您的问题量身定做的,您指的是支持分布式消息传递的Axon扩展。虽然这确实是一个选项,但要知道这将需要您为分布式命令、事件和事件存储配置几个单独的解决方案。对此使用统一的解决方案(如轴突服务器 )将确保用户不必采用三种(或更多)不同的方法来使其全部工作。相反,Axon Server被附加到Axon Framework应用程序,它为您完成所有的分发和事件存储。
因此,这意味着,如果要使用Axon,就没有必要使用DistributedCommandBus和SpringAMQPPublisher来实现您的目标。
这是FYI的一部分,它可以简化你的生活;当然,这绝不是必要的。那么让我们转到你的实际问题:
这是一个有效的方法吗?
我认为,在这种形式下,作为一个反腐败层,对于一个传奇来说是非常好的。一个佐贺声明它对事件和发送操作作出反应。这些操作是以命令的形式进行还是以其他第三方服务的形式进行,完全取决于您。
但是请注意,我认为AMQP更多地是分布式事件的解决方案(在广播方式下),而不是发送命令(直接处理程序)的一种方式。可以对其进行变形以满足您的需要,但我认为它对于命令分发来说是次优的,因为它需要调整。
最后,确保您的Saga能够处理通过RabbitMQ发送这些操作的异常。您不会希望发票服务在这条消息上失败,并让您的订单服务的Saga认为它在一个愉快的道路上与您的交易当然。
总之,在Saga中使用另一个消息代理确实是可行的。
在Axon中有处理这种场景的有文档记录的方法吗?
目前还没有记录在案的Axon处理这种场景的方法,因为目前还没有“一刀切”的解决方案。从纯粹的Axon方法来看,使用命令是可行的。但正如您所说,由于(不愿意?),这在您的领域内不是一个选项。另一队。因此,您可以追溯Saga的实际意图,而不考虑Axon,我将在下面对此进行总结:
这是我的两分钱,希望这能帮到你!
https://stackoverflow.com/questions/62557313
复制相似问题