在 .NET 中使用 RabbitMQ 发布/订阅示例
我已经构建了此示例:入门RabbitMQ in .net,但制作了 2 个程序:
- 一个发布者
- 一个订阅者
我使用 BasicPublish
来发布,使用 BasicAck
来监听,如示例中所示。如果我运行一个发布者和多个订阅者(对于来自发布者的每条“发送消息”),只有一个订阅者收到它。因此,发布者按照某种顺序(当订阅者启动时)向订阅者发送消息,并且我想向所有订阅者发送一条消息。该样本有什么问题?也许您可以提供通过 RabbitMq 发布者/订阅者消息交换的工作示例?
I've built this sample: Getting Started With RabbitMQ in .net, but made 2 programs:
- one-publisher
- one-subscriber
I'm using BasicPublish
to publish and BasicAck
to listen as in example. If I run one publisher and several subscribers-on every "send message" from publisher- only one subscriber gets it. So that there is some order (as subscribers were started) in which publisher sends message to subscribers, and I want to send one message to all subscribers. What is wrong with that sample? May be you can provide working sample of publisher/subscribers message exchange via RabbitMq?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您链接到的示例使用不带交换的简单排队,这确保只有一个消费者将处理该消息。为了在 RabbitMQ 中支持 pub/sub,您需要首先创建一个 Exchange,然后让每个订阅者在该 Exchange 上绑定一个队列。然后,生产者将消息发送到 Exchange,Exchange 会将消息发布到已绑定到它的每个 Queue(至少使用简单的 Fanout 交换类型。可以通过 Direct 和 Topic 交换来实现路由。)
对于 Java 示例(其中可以很容易地转换为 C#)请参阅此处。
编辑:
更新的 .Net 版本可以在此处找到
The example you link to uses simple queueing without an exchange, which ensures that only a single consumer will handle the message. To support pub/sub in RabbitMQ, you need to first create an Exchange, and then have each subscriber bind a Queue on that Exchange. The producer then sends messages to the Exchange, which will publish the message to each Queue that has been bound to it (at least with the simple Fanout exchange type. Routing can be achieved with Direct and Topic exchanges.)
For a Java sample (which could be converted to C# pretty easily) please see here.
Edit:
Updated .Net version can be found here
我添加了关于此的新教程 .net 中的 RabbitMQ 入门
I've added a new tutorial about this Getting Started With RabbitMQ in .net
现在还有一些官方来源。
There are also some official sources now.