在 .NET 中使用 RabbitMQ 发布/订阅示例

Publish/Subscribe samples with RabbitMQ in .NET(在 .NET 中使用 RabbitMQ 发布/订阅示例)

本文介绍了在 .NET 中使用 RabbitMQ 发布/订阅示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了这个示例:.net 中的 RabbitMQ 入门,但做了 2 个程序:

I've built this sample: Getting Started With RabbitMQ in .net, but made 2 programs:

  • 单一出版商
  • 一个订阅者

我使用 BasicPublish 发布和 BasicAck 来收听,如示例所示.如果我在来自发布者的每条发送消息"上运行一个发布者和几个订阅者,那么只有一个订阅者会收到它.因此,发布者向订阅者发送消息时存在某种顺序(当订阅者开始时),我想向所有订阅者发送一条消息.那个样本有什么问题?您是否可以通过 RabbitMq 提供发布者/订阅者消息交换的工作示例?

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?

推荐答案

您链接到的示例使用没有交换的简单队列,这确保只有一个消费者将处理消息.要在 RabbitMQ 中支持发布/订阅,您需要首先创建一个 Exchange,然后让每个订阅者在该 Exchange 上绑定一个队列.生产者然后将消息发送到 Exchange,Exchange 会将消息发布到已绑定的每个 Queue(至少使用简单的 Fanout 交换类型.可以通过 Direct 和 Topic 交换来实现路由.)

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.)

有关 Java 示例(可以很容易地转换为 C#),请参阅 这里.

For a Java sample (which could be converted to C# pretty easily) please see here.

更新的 .Net 版本可以在 这里

Updated .Net version can be found here

这篇关于在 .NET 中使用 RabbitMQ 发布/订阅示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 .NET 中使用 RabbitMQ 发布/订阅示例

基础教程推荐