简体   繁体   English

关于消息总线/命令调度程序模式的混淆

[英]Confusion about Message Bus / Command Dispatcher patterns

Recently I've been reading a lot about distributed messaging and associated patterns. 最近我一直在阅读很多有关分布式消息传递和相关模式的内容。 I used some of them supported by the tools like for exemple NServiceBus . 我使用了一些工具支持的例子,比如例如NServiceBus

Many of those patterns are described on internet. 许多这些模式都在互联网上描述。 Some of them I recently read was : 我最近读到的其中一些是:

If using such tooling as NService bus alows to do a lot without thinking to much about infrastructure problems, some questions have araised when I tried to implement a basic Message Bus and command handler. 如果使用像NService bus这样的工具来做很多工作而不考虑基础设施问题,那么当我尝试实现基本的Message Bus和命令处理程序时,一些问题已经得到了解决。 In fact when it comes to these patterns I can't see many differences between them. 事实上,当谈到这些模式时,我看不出它们之间存在很多差异。

I won't paste code because it's to long but I found two blog posts that quite describe the idea of implementation I would like to talk about. 我不会粘贴代码,因为它很长,但我发现了两篇博文,很好地描述了我想谈的实现的想法。

The idea is simple, the message bus tracks the subscribers and dispatches the messages to different subscribers if they are interested in. 这个想法很简单,消息总线跟踪订阅者并在他们感兴趣的情况下将消息发送给不同的订阅者。

It's quite similar to message bus. 它与消息总线非常相似。 The command bus invokes the command handlers for a given command type. 命令总线为给定的命令类型调用命令处理程序。

So in both cases there are similarities. 所以在这两种情况下都有相似之处。

What are the real differences and benefits using one pattern than another (I'm not talking about supporting tooling). 使用一种模式比另一种模式有什么真正的差异和好处(我不是在谈论支持工具)。 What I'm missing ? 我错过了什么?

The second question is. 第二个问题是。 Does the message bus is valuable without the supporting tooling ? 没有支持工具,消息总线是否有价值? I don't see myself to impelment the support for all tenents on my own. 我不认为自己会为自己的所有权利提供支持。

I'm sorry for a long and confusing question but don't hesitate to ask for more details. 对于一个冗长而令人困惑的问题我很抱歉,但请不要犹豫,询问更多细节。

Woah, it'll be tough to give an answer more thorough or more credible than the MSDN you linked to, so lets go for more concise. 哇,要给出比你链接的MSDN更彻底或更可信的答案是很难的,所以让我们更简洁。

A Message Bus is concerned with communication . 消息总线与通信有关。 It doesn't even require that the communication be delivered is a command or not. 它甚至不要求传递通信是否是命令。 It also doesn't care what the payload is. 它也不关心有效载荷是什么。 It is "type agnostic". 它是“类型不可知的”。 The primary concern of the message bus is simply to keep track of who should get each piece of communication (pub/sub). 消息总线的主要关注点是跟踪谁应该获得每个通信(pub / sub)。 A benefit of this model is that it will support future expansion that you don't yet have the specs for. 这种模式的一个好处是,它将支持您尚未拥有规范的未来扩展。 You might add in a new message type down the road and this model will be happy to deliver it. 您可以在路上添加新的消息类型,此模型将很乐意提供它。 A message bus is more likely to be distributed outside your application and perhaps even outside your machine (say distributed between a cluster of 10 servers). 消息总线更可能分布在应用程序之外,甚至可能在您的机器外部(例如,在10台服务器的集群之间分发)。

A command handler model is concerned with separating the actions from the execution of a command. 命令处理程序模型涉及将动作与命令的执行分开。 Traditionally (at least in the languages I use) commands were very tightly associated with UI controls and their events and the UI thread. 传统上(至少在我使用的语言中)命令与UI控件及其事件和UI线程紧密相关。 With this old model, it was also difficult to customize or extend the range of available commands in your application (say with an extension DLL). 使用这个旧模型,在应用程序中定制或扩展可用命令的范围也很困难(比如使用扩展DLL)。 The command handler model separates those concerns of UI and command execution. 命令处理程序模型将UI和命令执行的这些问题分开。 You now have the flexibility to easily add more command handlers and to execute commands without a UI event ( Unit test-able ). 您现在可以灵活地轻松添加更多命令处理程序并在没有UI事件的情况下执行命令(可单元测试 )。 This makes for cleaner, more modular and testable code. 这样可以实现更清晰,更模块化和可测试的代码。 The command handler is more likely to be part of your application internally. 命令处理程序更有可能在内部成为应用程序的一部分。 Any extensions to your commands collection are likely intended to affect your one application and not multiple applications. 命令集的任何扩展都可能会影响您的一个应用程序而不是多个应用程序。

A Message/Command Broker is concerned with connecting incompatible or differently designed independent systems. 消息/命令代理关注连接不兼容或设计不同的独立系统。 This is the use case where you want one application to interface with another and don't have the source code to one or both applications. 这是您希望一个应用程序与另一个应用程序交互并且没有一个或两个应用程序的源代码的用例。 So you create a broker which receives information from one side and provides this information on the other side taking into account any transformations necessary for these two apps to communicate. 因此,您创建了一个代理,该代理从一方接收信息,并在另一方提供此信息,同时考虑到这两个应用程序进行通信所需的任何转换。 The example on MSDN is an ecommerce website which might need to talk to a payment processor, a shipping company, and an accounting system. MSDN上的示例是一个电子商务网站,可能需要与支付处理器,运输公司和会计系统进行通信。 You may not have the ability to change the source code for any of these apps (including the ecommerce system). 您可能无法更改任何这些应用程序(包括电子商务系统)的源代码。 Maybe the ecommerce system requires an IExamplePaymentGateway interface and your payment provider requires a IDifferentPaymentAPI interface. 也许电子商务系统需要IExamplePaymentGateway接口,而您的支付提供商需要IDifferentPaymentAPI接口。 Maybe one API is implemented in XML and the other in JSON? 也许一个API用XML实现,另一个用JSON实现? Whatever the differences, your broker is responsible to make the connection possible. 无论存在何种差异,您的经纪人都有责任使连接成为可能。

As you can see they all involve communicating in one way or another. 正如您所看到的,它们都涉及以某种方式进行沟通。 The lines between them can be blurry and you may even use a combination of several of these patterns to achieve your particular use case. 它们之间的界限可能很模糊,您甚至可以使用其中几种模式的组合来实现您的特定用例。

While I've never used NServiceBus, most of these type of libraries simply try to wrap up the abstract/academic models into more concrete language specific implementations. 虽然我从未使用过NServiceBus,但这些类型的大多数库只是试图将抽象/学术模型包含在更具体的语言特定实现中。 Sometimes this saves you time, sometimes you get stuck with a poor implementation from an unknown open source contributor. 有时这可以节省您的时间,有时您会遇到来自未知开源贡献者的糟糕实现。 You'll need to evaluate your own use case and the suitability of the tools available in your preferred development language. 您需要评估自己的用例以及首选开发语言中可用工具的适用性。

Generally, a message bus (or a standard event dispatcher) can have many subscribers for different types of messages / events. 通常,消息总线(或标准事件调度程序)可以具有用于不同类型的消息/事件的许多订户。

A command bus usually dispatches commands to exactly 1 handler, similar to a router resolving routes to controllers. 命令总线通常将命令分配给1个处理程序,类似于路由器解析到控制器的路由。

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

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