(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. 关于Task的线程窃取

    示例代码: static void Main(string[] args) { ThreadPool.SetMaxThreads(, ); object locker = new object(); ...

  2. 安装.NET Framework后程序无法启动的错误处理

    最近发现一直在使用的Database.NET软件无法正常使用了,表现为当尝试进行Sql Server的连接创建时,直接报错 在事件查看器具体错误信息为: 日志名称:          Applicat ...

  3. 将Table表格导出到Excel

    1.导出当前页 效果如下: 前台代码: @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta nam ...

  4. 东大OJ-麦森数

    1064: 麦森数 时间限制: 1 Sec  内存限制: 128 MB 提交: 52  解决: 9 [提交][状态][讨论版] 题目描述 形如2P-1的素数称为麦森数,这时P一定也是个素数.但反过来不 ...

  5. memcached安装配置

    简述: memcached,开源的分布式缓存数据系统.高性能的NOSQL . Linux 一.环境配置与安装 01.编译准备环境 yum install -y gcc make cmake autoc ...

  6. 使用jsp/servlet简单实现文件上传与下载

    使用JSP/Servlet简单实现文件上传与下载    通过学习黑马jsp教学视频,我学会了使用jsp与servlet简单地实现web的文件的上传与下载,首先感谢黑马.好了,下面来简单了解如何通过使用 ...

  7. mysql case when then end学习

    表 vtiger_acctive,字段 id,name. 1. 查询中使用 # 查询如果name的值为 hello1 时输出 6666,当值为 hello2 时,输出 333333 select ca ...

  8. 18位身份证验证--java实现,正则表达式

    简单的正则表达式: (1)preg_match("/^(\d{18,18}|\d{15,15}|\d{17,17}x)$/",$id_card)(2)preg_match(&quo ...

  9. [转]JS中对象与字符串的互相转换

    原文地址:http://www.cnblogs.com/luminji/p/3617160.html 在使用 JSON2.JS 文件的 JSON.parse(data) 方法时候,碰到了问题: thr ...

  10. linux 下远程连接windows

    安装软件 sudo apt-get install rdesktop 连接windows 然后进入windows登陆界面 输入应户名密码后就进入windows了 注意的是 参数-f是全屏的意思  然后 ...