HTML5技术

《RabbitMQ Tutorial》译文 第 3 章 发布和订阅 - 溪边静禅(2)

字号+ 作者:H5之家 来源:H5之家 2017-12-14 16:00 我要评论( )

The first parameter is the the name of the exchange. The empty string denotes the default or nameless exchange: messages are routed to the queue with the name specified by routingKey, if it exists.

The first parameter is the the name of the exchange. The empty string denotes the default or nameless exchange: messages are routed to the queue with the name specified by routingKey, if it exists.

第一个参数就是交换机的名字。空字符串表示默认的或者匿名的交换机:根据明确的路由键(routingKey)将消息路由到已存在的队列。

Now, we can publish to our named exchange instead:

现在,我们用具名(自定义命名)的交换机来进行发布:

var message = GetMessage(args); var body = Encoding.UTF8.GetBytes(message); channel.BasicPublish(exchange: "logs", routingKey: "", basicProperties: null, body: body); Temporary queues 临时队列

As you may remember previously we were using queues which had a specified name (remember hello and task_queue?). Being able to name a queue was crucial for us -- we needed to point the workers to the same queue. Giving a queue a name is important when you want to share the queue between producers and consumers.

你可能还记得之前我们使用过指定名称的队列(记得好像是 hello 和 task_queue?)。能够为队列命名对我们来说是至关重要的 -- 我们需要将工作单元指向相同的队列。同样,当你想在生产者和消费者之间共享队列时,为队列赋予一个名字也是非常重要的。

But that's not the case for our logger. We want to hear about all log messages, not just a subset of them. We're also interested only in currently flowing messages not in the old ones. To solve that we need two things.

但是,以上并不是我们日志系统的案例。我们想要监听所有的日志消息,而不仅仅是它们的一部分。我们只对当前正在流动的消息感兴趣,而不是旧的消息,为解决这个问题我需要做好两件事。

Firstly, whenever we connect to Rabbit we need a fresh, empty queue. To do this we could create a queue with a random name, or, even better - let the server choose a random queue name for us.

首先,无论我们何时连接到 Rabbit,都需要一个崭新的、空的队列,为做到这一点我们可以使用随机名称来创建一个队列,当然更好的做法是,让服务端为我们选择一个随机名称。

Secondly, once we disconnect the consumer the queue should be automatically deleted.

其次,一旦我们与消费者断开连接,相关的队列也应当能自动删除。

In the .NET client, when we supply no parameters to queueDeclare() we create a non-durable, exclusive, autodelete queue with a generated name:

在 .NET 客户端中,当我们调用 queueDeclare 方法而并未提供任何参数时,实际上就是创建了一个非持久化、独享,且自动删除的具名队列。

var queueName = channel.QueueDeclare().QueueName;

At that point queueName contains a random queue name. For example it may look like amq.gen-JzTY20BRgKO-HjmUJj0wLg.

在此处 queueName 包含的是一个随机队列名称,比如它看起来可能像 amq.gen-JzTY20BRgKO-HjmUJj0wLg。

Bindings 绑定

We've already created a fanout exchange and a queue. Now we need to tell the exchange to send messages to our queue. That relationship between exchange and a queue is called a binding.

我们已经创建了一个 fanout 型交换机和队列,现在我们需要告诉交换机把消息发送到队列,那么,交换机和队列之间的关系就被称作绑定。

channel.QueueBind(queue: queueName, exchange: "logs", routingKey: "");

From now on the logs exchange will append messages to our queue.

从现在开始,日志交换机将会把消息追加到队列中。

Listing bindings 列举绑定

You can list existing bindings using, you guessed it:

你可以列举出现有的绑定,(所使用的命令)你该可以猜到:

rabbitmqctl list_bindings Putting it all together 融合一起

Markdown

The producer program, which emits log messages, doesn't look much different from the previous tutorial. The most important change is that we now want to publish messages to our logs exchange instead of the nameless one. We need to supply a routingKey when sending, but its value is ignored for fanout exchanges. Here goes the code for EmitLog.cs file:

发出日志消息的生产者程序,与之前教程看起来并无太大不同。现在,最重要的变化莫过于我们想把消息发布到名为 logs 的交换器中,而非匿名。在发送消息时我们需要提供一个路由键(routingKey),只不过它的值在 fanout 型交换机中被忽略了,针对 EmitLog.cs 文件的代码如下:

using System; using RabbitMQ.Client; using System.Text; class EmitLog { public static void Main(string[] args) { var factory = new ConnectionFactory() { HostName = "localhost" }; using(var connection = factory.CreateConnection()) using(var channel = connection.CreateModel()) { channel.ExchangeDeclare(exchange: "logs", type: "fanout"); var message = GetMessage(args); var body = Encoding.UTF8.GetBytes(message); channel.BasicPublish(exchange: "logs", routingKey: "", basicProperties: null, body: body); Console.WriteLine(" [x] Sent {0}", message); } Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); } private static string GetMessage(string[] args) { return ((args.Length > 0) ? string.Join(" ", args) : "info: Hello World!"); } }

(EmitLog.cs source)

As you see, after establishing the connection we declared the exchange. This step is necessary as publishing to a non-existing exchange is forbidden.

如同你所见,在建立好连接之后我们声明了交换机。这一步非常有必要,因为禁止向一个不存在的交换机发布消息。

The messages will be lost if no queue is bound to the exchange yet, but that's okay for us; if no consumer is listening yet we can safely discard the message.

如果尚没有任何队列绑定到交换机,消息将会丢失,但这对我们来说并没有什么问题;如果没有任何消费者正在监听,我们可以将消息安全地删除。

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 关于博客园首页发布规则的若干猜测 - DOM哥

    关于博客园首页发布规则的若干猜测 - DOM哥

    2017-12-05 16:08

  • 腾讯发布 Omix 1.0 - 用 JSX 或 hyperscript 创建用户界面 - 【当耐特】

    腾讯发布 Omix 1.0 - 用 JSX 或 hyperscript 创建用户界面 - 【当耐

    2017-08-08 17:00

  • 腾讯 AlloyCrop 1.0 发布 - 【当耐特】

    腾讯 AlloyCrop 1.0 发布 - 【当耐特】

    2017-08-02 13:00

  • .NET Core 2.0 Preview2 发布汇总 - Savorboard

    .NET Core 2.0 Preview2 发布汇总 - Savorboard

    2017-07-01 11:01

网友点评
b