Rabbitmq -Routeing模式- python编码实现
(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.
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编码实现的更多相关文章
- Rabbitmq -Publish_Subscribe模式- python编码实现
what is Exchanges ?? Let's quickly go over what we covered in the previous tutorials: A producer is ...
- 转--python 编码规范
编程规范 1.1. 命名规范 1.1.1. [强制] 命名不能以下划线或美元符号开始和结尾 反例: name / __name / $Object / name / name$ / Object$ 1 ...
- Python编码(encode)和解码(Decode)常见的两个错误
项目地址:https://git.io/pytips 0x07 和 0x08 分别介绍了 Python 中的字符串类型(str)和字节类型(byte),以及 Python 编码中最常见也是最顽固的两个 ...
- 使用rabbitmq rpc 模式
服务器端 安装 ubuntu 16.04 server 安装 rabbitmq-server 设置 apt 源 curl -s https://packagecloud ...
- VS2013+PTVS,python编码问题
1.调试,input('中文'),乱码2.调试,print('中文'),正常3.不调试,input('中文'),正常4.不调试,print('中文'),正常 页面编码方式已经加了"# -- ...
- (转载) 浅谈python编码处理
最近业务中需要用 Python 写一些脚本.尽管脚本的交互只是命令行 + 日志输出,但是为了让界面友好些,我还是决定用中文输出日志信息. 很快,我就遇到了异常: UnicodeEncodeError: ...
- Python 编码简单说
先说说什么是编码. 编码(encoding)就是把一个字符映射到计算机底层使用的二进制码.编码方案(encoding scheme)规定了字符串是如何编码的. python编码,其实就是对python ...
- Python之路3【知识点】白话Python编码和文件操作
Python文件头部模板 先说个小知识点:如何在创建文件的时候自动添加文件的头部信息! 通过:file--settings 每次都通过file--setings打开设置页面太麻烦了!可以通过:View ...
- python编码规范
python编码规范 文件及目录规范 文件保存为 utf-8 格式. 程序首行必须为编码声明:# -*- coding:utf-8 -*- 文件名全部小写. 代码风格 空格 设置用空格符替换TAB符. ...
随机推荐
- 安装.NET Framework后程序无法启动的错误处理
最近发现一直在使用的Database.NET软件无法正常使用了,表现为当尝试进行Sql Server的连接创建时,直接报错 在事件查看器具体错误信息为: 日志名称: Applicat ...
- Android开发环境部署
引言 在windows系统中安装Android的开发环境,将分为五个步骤来完成: 第一步:安装JDK 第二步:配置Windows上JDK的变量环境 第三步: 下载安装Eclipse 第四步:下载安 ...
- C 语言学习的第 03 课:你的 idea 是怎么变成能够执行的程序的
在上一篇文章中,我们说到,C 语言系统应该由程序开发环境,C 语言本身和 C 语言的库组成.且同时说了程序开发环境做了“编写”,“预处理”,“编译”和“链接”这几件事情.但是细节并没有一一呈现.不知道 ...
- springMVC学习--RESTful支持
简介 RESTful架构,就是目前最流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便,所以正得到越来越多网站的采用.RESTful(即Representational State T ...
- fio 2种画图方法 fio_generate_plots 和 gfio
fio 安装fio apt-get install fio 可以把fio的输出数据自动画图的插件gnuplot apt-get install gnuplot 1.输出bw,lat和iops数据并画图 ...
- Ubuntu14.04下安装tomcat
1.官方网站下载最新的tomcat:http://tomcat.apache.org/download-80.cgi在ubuntu上,我们下载zip和tar.gz.Ubuntu14.04安装和配置To ...
- Jenkins的maven工程打包的时候怎么指定不同环境的配置文件
http://outofmemory.cn/code-snippet/6643/maven-profile-define-enviroment-package 在打包的时候我们添加上 这里我们指定配置 ...
- [转]javascript Date format(js日期格式化)
方法一:这个很不错,好像是 csdn 的 Meizz 写的: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) ...
- 概率 light oj 1104
t个数据 n天一年 至少2个人在同一天生日的概率>=0.5 问至少多少人 显然要从反面考虑 设365天 都在不同一天的概率 p(num)=1*364/365*363/365...; =(day ...
- jquery 设置焦点
function CheckForm() { var classLevel = $("#classLevel").val(); var re = /^[1-9][0-9]*$/; ...