HTML5技术

《RabbitMQ Tutorial》译文 第 5 章 主题 - 溪边静禅

字号+ 作者:H5之家 来源:H5之家 2017-12-21 10:40 我要评论( )

《RabbitMQ Tutorial》译文 第 5 章 主题 原文来自 RabbitMQ 英文官网的教程(5.Topics),其示例代码采用了 .NET C# 语言。 In the previous tutorial we improved our logging system. Instead of using a fanout exchange only capable of dummy broadcas

《RabbitMQ Tutorial》译文 第 5 章 主题

原文来自 RabbitMQ 英文官网的教程(5.Topics),其示例代码采用了 .NET C# 语言。

Markdown

In the previous tutorial we improved our logging system. Instead of using a fanout exchange only capable of dummy broadcasting, we used a direct one, and gained a possibility of selectively receiving the logs.

在之前的教程中,我们改进了日志系统。比起只能够单一广播的 fanout 型交换机,我们现在采用了 direct 型,从而获得了选择性接收日志的可能性。

Although using the direct exchange improved our system, it still has limitations - it can't do routing based on multiple criteria.

尽管使用 direct 型交换机对系统有所改进,但仍然有其局限性 - 即在多重条件下没办法进行路由。

In our logging system we might want to subscribe to not only logs based on severity, but also based on the source which emitted the log. You might know this concept from the syslog unix tool, which routes logs based on both severity (info/warn/crit...) and facility (auth/cron/kern...).

在该日志系统中,我们可能希望不仅仅订阅基于严重性(这一项指标)的日志,也希望能基于产生日志的源头。你大概会知道这个概念来自于 syslog unix 工具,它可以针对严重性(info/warn/crit...)和设备(auth/cron/kern...)进行路由。

That would give us a lot of flexibility - we may want to listen to just critical errors coming from 'cron' but also all logs from 'kern'.

这便给了我们许多灵活性 - 我们可以做到监听“cron”类型的严重错误,以及所有来自于“kern”设备的日志。

To implement that in our logging system we need to learn about a more complex topic exchange.

为了在我们的日志系统中实现上述功能,我们需要学习更为复杂的话题(Topic)交换机。

Topic exchange 话题交换机

Messages sent to a topic exchange can't have an arbitrary routing_key - it must be a list of words, delimited by dots. The words can be anything, but usually they specify some features connected to the message. A few valid routing key examples: "stock.usd.nyse", "nyse.vmw", "quick.orange.rabbit". There can be as many words in the routing key as you like, up to the limit of 255 bytes.

发送至 topic 型交换机的消息已不能携带任意的 routing_key - 而必须是一个字符串列表,并基于小数点来分隔。

The binding key must also be in the same form. The logic behind the topic exchange is similar to a direct one - a message sent with a particular routing key will be delivered to all the queues that are bound with a matching binding key. However there are two important special cases for binding keys:

  • * (star) can substitute for exactly one word.
  • # (hash) can substitute for zero or more words.
  • “绑定键”也必须是相同的格式。topic 型交换机背后的逻辑与 direct 型交换机相似 - 采用指定 routing key 所发送的消息将会被递送到与 binding key 相匹配的队列中,然而针对 binding key 有两个非常重要且特殊的情形:

  • * (星号)可以替代一个明确的字符串。
  • # (哈希,井号)可以替代 0 至多个字符串。
  • It's easiest to explain this in an example:

    在示例中可以很容易的对其解释:

    Markdown

    In this example, we're going to send messages which all describe animals. The messages will be sent with a routing key that consists of three words (two dots). The first word in the routing key will describe speed, second a colour and third a species: "..".

    在这个示例中,我们打算发送用来描述动物的消息,它是基于三个字符串(包含两个小数点)组成的 routing key,其第一个字符串将用来描述速度,第二个是颜色,第三个是物种,如下:“..”。

    We created three bindings: Q1 is bound with binding key ".orange." and Q2 with "..rabbit" and "lazy.#".

    我们创建了三个绑定,队列 Q1 将关联到绑定键“*.orange.*”,队列 Q2 则关联到“*.*.rabbit”和“lazy.#”。

    These bindings can be summarised as:

  • Q1 is interested in all the orange animals.
  • Q2 wants to hear everything about rabbits, and everything about lazy animals.
  • 这些绑定可以概括为:

  • 队列 Q1 对所有桔黄色(orange)的动物感兴趣。
  • 队列 Q2 期望监听到所有 rabbit 后缀,以及所有 lazy 前缀的动物。
  • A message with a routing key set to "quick.orange.rabbit" will be delivered to both queues. Message "lazy.orange.elephant" also will go to both of them. On the other hand "quick.orange.fox" will only go to the first queue, and "lazy.brown.fox" only to the second. "lazy.pink.rabbit" will be delivered to the second queue only once, even though it matches two bindings. "quick.brown.fox" doesn't match any binding so it will be discarded.

    routing key 设置为"quick.orange.rabbit"的消息将会被递送到两个队列。消息"lazy.orange.elephant"同样也会去往两个队列。另一方面,"quick.orange.fox"将只会去往第一个队列,"lazy.brown.fox"则只会去第二个队列。"lazy.pink.rabbit",尽管它匹配了两个绑定,但只会被递送到第二个队列一次(而不会是两次)。"quick.brown.fox"则不匹配任何绑定,所以将会被丢弃。

     

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

    相关文章
    • 《RabbitMQ Tutorial》译文 第 3 章 发布和订阅 - 溪边静禅

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

      2017-12-14 16:00

    • hexo多主题切换 - 罗道义

      hexo多主题切换 - 罗道义

      2017-04-23 11:04

    • html5学习笔记(3)--主题结构元素-1 - tuohaibei

      html5学习笔记(3)--主题结构元素-1 - tuohaibei

      2016-08-08 13:00

    • 游戏界元老Brian Fargo出任主题演讲嘉宾!_HTML5中文网

      游戏界元老Brian Fargo出任主题演讲嘉宾!_HTML5中文网

      2014-11-17 18:29

    网友点评
    d