将 NServiceBus 消息路由到特定客户端的最佳方法是什么?
假设我有一条 ClientRequestMessage
消息,其中包含对特定 Client
的请求。 Web 应用程序将生成这些请求,并且需要将它们发送到正确的客户端
进行处理。我可以为此想到一些选择。
- 我可以有一个所有消息都发送到的单个队列,并且特定的客户端处理程序检查一个属性(例如
ClientId
)来决定他们是否关心它。但对我来说,这在很多层面上都是错误的。 - 我可以向所有客户发布一条消息,他们可以决定在处理过程中是否关心它。这看起来流量太大,并且浪费了每个客户的时间来处理他们本来就不应该关心的消息。
- 我可以有客户端特定的队列,这些消息也可以被路由。这对我来说是最好的,但我不确定如何去做。我想保持简单并避免客户端特定的消息类型,但我不确定如何告诉 NServiceBus“对于客户端 A 将其发送到客户端 A 的队列,对于客户端 B 将其发送到客户端 B 的队列”。
所以我的问题是,设置此功能的最佳(最有效?最容易管理?)方法是什么?我很确定我需要使用经销商,但不是很肯定,所以我想我会问。
额外问题:
假设每个客户端都有多个处理程序。我如何确保只有其中一个处理给定的消息?我需要为每个客户指定一个经销商吗?
Let's say I have a ClientRequestMessage
message that contains a request for a specific Client
. A web application will generate these requests and they need to be sent to the correct Client
for handling. I can think of a few options for this.
- I could have a single queue that all messages go to and specific client handlers check a property (like
ClientId
) to decide whether they care about it. This feels wrong on many levels to me though. - I could publish a message to all of the clients and they could decide whether or not they care about it during handling. This seems like too much traffic and wastes each client's time handling messages they shouldn't care about in the first place though.
- I could have client specific queues that these messages get routed too. This one feels the best to me, but I am unsure of how to do it. I'd like to keep it simple and avoid client specific message types, but I am not sure how to tell NServiceBus "for client A send it to client A's queue and for client B send it to client B's queue".
So my question is, what is the best (most efficient? easiest to manage?) way to set this up? I am pretty sure I need to use the distributor, but not positive so thought I would ask.
BONUS QUESTION:
Let's say each client has multiple handlers. How can I make sure only one of them handles a given message? Would I need a distributor per client?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您真正想要的是允许您只有一条消息的解决方案,您可以根据 clientId 在消息上放置特定的过滤器,并且仅在与客户端相关时将消息路由到客户端,那么我会使用 PServiceBus(< a href="http://pservicebus.codeplex.com" rel="nofollow">pservicebus.codeplex.com)。它将使您更轻松地为每个客户端指定一组订阅,其中它们的消息均按 clientId 过滤到特定队列或可用的传输中。下面的示例显示了通过 UserName 属性过滤 ChatTopic,并且当发布的消息 UserName 属性不是 TJ 时,订阅者仅在指定传输上接收消息。您还可以在执行诸如 GreaterThan("MyComplexProperty.Blah.ID", 5) 之类的操作时使用复杂过滤器
If what you really want is the solution that allows you to have just a single message where you can place a specific filter on the message based on clientId and only route the message to the client when it relates to them then I would use PServiceBus(pservicebus.codeplex.com). It will make it easier for you specific a set of subscriptions for each of your client where their messages are all filtered by clientId into a specific queue or what transport you have available. The below example shows filtering a ChatTopic by the UserName Property and the subscriber only receives the message at the specified transport when the message been published UserName property is not TJ. You are also allowed to use complex filter where you do thing such as GreaterThan("MyComplexProperty.Blah.ID", 5)
您可以使用 MessageEndpointMappings 配置部分告诉 NSB 将消息放置在何处。您可以将特定消息类型或整个程序集映射到队列。如果您不想创建特定的消息类型并映射它们,那么我会推荐发布方法。从队列中删除消息的开销非常小。
如果您的“客户端”有许多 NSB 实例来接收消息,那么您将需要使用分发器。查看分布式 Pub/Sub 文档。
You can tell NSB where to put messages by using the MessageEndpointMappings configuration section. You can map a specific message type or a whole assembly to a queue. If you don't want to create specific message types and map them, then I would recommend the publish approach. The overhead of removing a message from the queue is pretty minimal.
If your "client" has many instances of NSB to pick up messages then you will need to use a Distributor. Check out the distributed Pub/Sub documentation.