(using the pika 0.10.0 Python client)

In the previous tutorial we built a simple logging system. We were able to broadcast log messages to many receivers.

In this tutorial we're going to add a feature to it - we're going to make it possible to subscribe only to a subset of the messages. For example, we will be able to direct only critical error messages to the log file (to save disk space), while still being able to print all of the log messages on the console.

# 以上均是客套话,哥自己看的懂就算了,不给翻译了,节省时间,哈哈。

Bindings

In previous examples we were already creating bindings. You may recall code like:

channel.queue_bind(exchange=exchange_name,queue=queue_name)

A binding is a relationship between an exchange and a queue. This can be simply read as: the queue is interested in messages from this exchange.

Bindings can take an extra routing_key parameter. To avoid the confusion with a basic_publishparameter we're going to call it a binding key. This is how we could create a binding with a key:

# 简单的翻译下:绑定是exchange和队列之间的一种关系,这可以简单的理解为: 这个队列对来自于exchange的信息非常感兴趣,绑定需要采取一个额外的参数routing_key , 为了避免这个有关于basic_publish 这个参数的困惑,我们准备称呼它为binding key,这就是我们如何用一个钥匙创建一个绑定

channel.queue_bind(exchange=exchange_name,queue=queue_name,routing_key='black')

The meaning of a binding key depends on the exchange type. The fanout exchanges, which we used previously, simply ignored its value.

#translate: 这个意思是说 绑定钥匙取决于exchange的类型,这个扇出exchange,是我们以前使用过的,简单的忽略其值

Direct exchange

Our logging system from the previous tutorial broadcasts all messages to all consumers. We want to extend that to allow filtering messages based on their severity. For example we may want the script which is writing log messages to the disk to only receive critical errors, and not waste disk space on warning or info log messages.

We were using a fanout exchange, which doesn't give us too much flexibility - it's only capable of mindless broadcasting.

We will use a direct exchange instead. The routing algorithm behind a direct exchange is simple - a message goes to the queues whose binding key exactly matches the routing keyof the message.

# translate:我们的日志系统从前面的教程里面可知会广播信息到所有的消费者,我们想扩展它,允许基于信息的严重性来过滤一些信息,例如我们可以想要这个脚本只把接收到的极重要的错误信息写入磁盘,不想把磁盘的空间浪费在警告或者提示信息里面

我们使用扇出交换器,这个扇出exchange不会给我们太多的灵活性,它,它只能怪盲目的广播。

我们也会使用直连交换器去替代扇出exchange,这个直连exchange背后的路由算法更加简单,一条信息能准确匹配到到绑定了key的队列里面。

To illustrate that, consider the following setup:

In this setup, we can see the direct exchange X with two queues bound to it. The first queue is bound with binding key orange, and the second has two bindings, one with binding key blackand the other one with green.

In such a setup a message published to the exchange with a routing key orange will be routed to queue Q1. Messages with a routing key of black or green will go to Q2. All other messages will be discarded.

# translate: 在这一步,我们能够看到这个直连交换器x被两个队列所束缚住,这个第一个队列是束缚住了orange这个key,第二个有两个绑定,一个绑定的key是blackend,另一个key是green

Emitting logs

We'll use this model for our logging system. Instead of fanout we'll send messages to a directexchange. We will supply the log severity as a routing key. That way the receiving script will be able to select the severity it wants to receive. Let's focus on emitting logs first.

# translate: 我们为我们的日志系统使用这个模型,我们发送信息到直连exchange上而不是扇出exchange,我们能够提供这个日志严重程度对于routing key,在这种方式下接收脚本能够去选择他想要接受的严重性,首先让我们集中精力在这发送日志脚本上

Like always we need to create an exchange first:

channel.exchange_declare(exchange='direct_logs',type='direct')

 And we're ready to send a message:

channel.basic_publish(exchange='direct_logs',routing_key=severity,body=message)

To simplify things we will assume that 'severity' can be one of 'info', 'warning', 'error'.

Subscribing

Receiving messages will work just like in the previous tutorial, with one exception - we're going to create a new binding for each severity we're interested in.

result=channel.queue_declare(exclusive=True)queue_name=result.method.queueforseverityinseverities:channel.queue_bind(exchange='direct_logs',queue=queue_name,routing_key=severity)

 

Putting it all together

Rabbitmq -Routeing模式- python编码实现的更多相关文章

  1. Rabbitmq -Publish_Subscribe模式- python编码实现

    what is Exchanges ?? Let's quickly go over what we covered in the previous tutorials: A producer is ...

  2. 转--python 编码规范

    编程规范 1.1. 命名规范 1.1.1. [强制] 命名不能以下划线或美元符号开始和结尾 反例: name / __name / $Object / name / name$ / Object$ 1 ...

  3. Python编码(encode)和解码(Decode)常见的两个错误

    项目地址:https://git.io/pytips 0x07 和 0x08 分别介绍了 Python 中的字符串类型(str)和字节类型(byte),以及 Python 编码中最常见也是最顽固的两个 ...

  4. 使用rabbitmq rpc 模式

        服务器端     安装 ubuntu 16.04 server     安装 rabbitmq-server     设置 apt 源 curl -s https://packagecloud ...

  5. VS2013+PTVS,python编码问题

    1.调试,input('中文'),乱码2.调试,print('中文'),正常3.不调试,input('中文'),正常4.不调试,print('中文'),正常 页面编码方式已经加了"# -- ...

  6. (转载) 浅谈python编码处理

    最近业务中需要用 Python 写一些脚本.尽管脚本的交互只是命令行 + 日志输出,但是为了让界面友好些,我还是决定用中文输出日志信息. 很快,我就遇到了异常: UnicodeEncodeError: ...

  7. Python 编码简单说

    先说说什么是编码. 编码(encoding)就是把一个字符映射到计算机底层使用的二进制码.编码方案(encoding scheme)规定了字符串是如何编码的. python编码,其实就是对python ...

  8. Python之路3【知识点】白话Python编码和文件操作

    Python文件头部模板 先说个小知识点:如何在创建文件的时候自动添加文件的头部信息! 通过:file--settings 每次都通过file--setings打开设置页面太麻烦了!可以通过:View ...

  9. python编码规范

    python编码规范 文件及目录规范 文件保存为 utf-8 格式. 程序首行必须为编码声明:# -*- coding:utf-8 -*- 文件名全部小写. 代码风格 空格 设置用空格符替换TAB符. ...

随机推荐

  1. 抛开react,如何理解virtual dom和immutability

    去年以来,React的出现为前端框架设计和编程模式吹来了一阵春风.很多概念,无论是原本已有的.还是由React首先提出的,都因为React的流行而倍受关注,成为大家研究和学习的热点.本篇分享主要就聚焦 ...

  2. GitHub 上一份很受欢迎的前端代码优化指南-强烈推荐收藏

    看到一份很受欢迎的前端代码指南,根据自己的理解进行了翻译,但能力有限,对一些JS代码理解不了,如有错误,望斧正. HTML 语义化标签 HTML5 提供了很多语义化元素,更好地帮助描述内容.希望你能从 ...

  3. 微信公众平台SDK

    微信公众平台网址:https://mp.weixin.qq.com/ 服务号说明:给企业和组织提供更强大的业务服务与用户管理能力,帮助企业快速实现全新的公众号服务平台. .NETSDK: Loogn. ...

  4. java 常用的一些关键字

    1.关键字extends 1.继承作用 优化代码,减少代码的重复使用. 2.继承使用时机 两个类之间必须要满足is a的关系 ,才能够拥有继承关系,不是任 何 情况下都允许继承 3.继承的使用注意事项 ...

  5. 一键系统优化15项脚本,适用于Centos6.x

    #!/bin/sh ################################################ #Author:nulige # qqinfo:1034611705 # Date ...

  6. Learning jQuery, 4th Edition 勘误表

    来源于:http://book.learningjquery.com/3145/errata/ Chapter 1 page 14 The CSS snippet is correct, but it ...

  7. Java学习之ConcurrentHashMap实现一个本地缓存

    ConcurrentHashMap融合了Hashtable和HashMap二者的优势. Hashtable是做了线程同步,HashMap未考虑同步.所以HashMap在单线程下效率较高,Hashtab ...

  8. 利用反射,泛型,静态方法快速获取表单值到Model

    在项目中经常需要处理表单,给model赋值,很烦人的一些重复代码.如下边的代码: News news = new News(); news.Id = int.Parse(Request.Form[&q ...

  9. MySql_SQLyog快捷键

    1. SQL格式化 F12 格式化当前行所在的SQL Ctrl+F12    格式化选中的SQL Shift+F12   格式化所有SQL 2. 窗口操作 Ctrl+T 打开一个新的查询窗口 Alt+ ...

  10. 51nod 1076强连通

    Tarjan算法来解这题.无向图可以转化为有向图来解决. #include<map> #include<queue> #include<stack> #includ ...