聆听多个事件 [英] Listening on multiple events

查看:85
本文介绍了聆听多个事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何处理事件驱动架构中的相关事件?具体而言,如果必须触发多个事件以执行某些操作该怎么办.例如,我有一个微服务,它侦听两个事件foobar,并且仅在两个事件都到达并且具有相同的相关ID时才执行操作.

How to deal with correlated events in an Event Driven Architecture? Concretely, what if multiple events must be triggered in order for some action to be performed. For example, I have a microservice that listens to two events foo and bar and only performs an action when both of the events arrive and have the same correlation id.

一种方法是在做记账的微服务内部保留一个内部数据结构,当满足所有条件时,将触发适当的操作.但是,这种方法的问题在于微服务不再是不变的.

One way would be to keep an internal data structure inside the microservice that does the book keeping and when everything is satisfied an appropriate action is triggered. However, the problem with this approach is that the microservice is not immutable anymore.

有更好的方法吗?

推荐答案

一个经典的示例是在销售中接到订单并发布事件.财经和航运都订阅了该事件,但航运也订阅了来自金融的事件.

A classic example is where an order comes in at sales and an event is published. Both Finance and Shipping are subscribed to the event, but shipping is also subscribed to the event coming from finance.

有趣的是,您对消息到达的顺序一无所知.来自销售的事件可能会导致技术错误,因为数据库处于脱机状态.它可能会再次排队,或者最终进入错误队列以供操作重试.同时,来自金融的事件可能会到来.所以从理论上讲 来自销售的事件应该首先到达,然后是财务事件,但是实际上,情况可能相反.

The funny thing is that you have no idea on the order in which the messages arrive. The event from sales might cause a technical error, because the database is offline. It might get queued again or end up in an error queue for operations to retry it. In the meantime the event from finance might arrive. So theoretically the event from sales should arrive first and then the finance event, but in practice it can be the other way around.

这里有很多解决方案,但是我从不喜欢图形解决方案.作为.NET开发人员,我过去曾使用过K2和Windows Workflow Foundation,但是最灵活的解决方案是通过代码创建的,而不是通过图形界面创建的.

There are a number of solutions here, but I've never liked the graphical ones. As a .NET developer I've used K2 and Windows Workflow Foundation in the past, but the solutions most flexible are created in code, not via a graphical interface.

我目前将为此使用NServiceBus或MassTransit.在旁注中,我目前在Particular Software工作,我们生产NServiceBus. NServiceBus为此类工作提供了Sagas(文档),您也可以在我的Weblog上阅读关于演示,包括GitHub上的代码.

I currently would use NServiceBus or MassTransit for this. On a sidenote, I currently work at Particular Software and we make NServiceBus. NServiceBus has Sagas for this kind of work (documentation) and you can also read on my weblog about a presentation, incl. code on GitHub.

术语saga有点加载,但是它基本上处理长时间运行的(业务)流程. Gregor Hohpe称之为Process Manager(链接).

The term saga is kind of loaded, but it basically handles long running (business) processes. Gregor Hohpe calls it a Process Manager (link).

总结一下sagas的作用:它们由传入消息实例化并具有状态.传入的消息基于相关ID(例如customer idorder id)绑定/调度到特定的传奇实例.处理完消息(事件)后,状态将一直存储,直到收到新消息为止,或者直到代码将传奇标记为完成并将状态从存储中删除为止.

To summarize what sagas do : they are instantiated by incoming messages and have state. Incoming messages are bound/dispatched to a specific saga instance based on a correlationid, for example a customer id or order id. Once the message (event) is processed, state is stored until a new message arrives, or until the code marks the saga as completed and the state is removed from storage.

如前所述,在.NET世界中,MassTransit和NServiceBus支持此功能,但在其他环境中最可能有替代方法.

As said, in the .NET world MassTransit and NServiceBus support this, but there are most likely alternatives in other environments.

这篇关于聆听多个事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆