python使用rabbitMQ介绍五(话题模式)

一、模式介绍

话题模式(Topic)基本思想和路由模式是一样的,只不过路由键支持模糊匹配,符号“#”匹配一个或多个词,符号“*”匹配不多不少一个词

话题模式相当于消息的模糊匹配,或者按照正则匹配。其中

# 是通配符,匹配一个或者多个单词

* 代表匹配一个单词

模式提醒:

当使用#来收消息时,会接收到所有的消息,这样和fanout模式一样了。

当routing key中不含*或者#时,则topic的模式退化为direct的一对一模式。

队列模型:

python使用rabbitMQ介绍五(话题模式)

二、代码示例

发布者:

 #!/usr/bin/env python
import pika
import sys connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel() channel.exchange_declare(exchange='topic_logs',
exchange_type='topic') routing_key = ['#', "kern.critical", "A critical kernel error"]
for i in range(10):
message = '{} msg at : routing key {}'.format(i, routing_key[i % 3])
channel.basic_publish(exchange='topic_logs',
routing_key=routing_key[i % 3],
body=message)
print(" [x] Sent :%r" % (message))
connection.close()

消费者:

 import pika
import sys connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel() channel.exchange_declare(exchange='topic_logs',
exchange_type='topic') result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue binding_key = "#" channel.queue_bind(exchange='topic_logs',
queue=queue_name,
routing_key=binding_key) print(' [*] Waiting for logs. To exit press CTRL+C') def callback(ch, method, properties, body):
print(" [x] %r:%r" % (method.routing_key, body)) channel.basic_consume(callback,
queue=queue_name,
no_ack=True) channel.start_consuming()

发布者输出:

[x] Sent :'0 msg at : routing key #'
[x] Sent :'1 msg at : routing key kern.critical'
[x] Sent :'2 msg at : routing key error.critical'
[x] Sent :'3 msg at : routing key #'
[x] Sent :'4 msg at : routing key kern.critical'
[x] Sent :'5 msg at : routing key error.critical'
[x] Sent :'6 msg at : routing key #'
[x] Sent :'7 msg at : routing key kern.critical'
[x] Sent :'8 msg at : routing key error.critical'
[x] Sent :'9 msg at : routing key #'

消费者输出:

all:可以看到接收到所有的消息

[*] Waiting for logs. To exit press CTRL+C
[x] '#':b'0 msg at : routing key #'
[x] 'kern.critical':b'1 msg at : routing key kern.critical'
[x] 'error.critical':b'2 msg at : routing key error.critical'
[x] '#':b'3 msg at : routing key #'
[x] 'kern.critical':b'4 msg at : routing key kern.critical'
[x] 'error.critical':b'5 msg at : routing key error.critical'
[x] '#':b'6 msg at : routing key #'
[x] 'kern.critical':b'7 msg at : routing key kern.critical'
[x] 'error.critical':b'8 msg at : routing key error.critical'
[x] '#':b'9 msg at : routing key #'

消费者“critical”,只接收含critical的消息

[*] Waiting for logs. To exit press CTRL+C
[x] 'kern.critical':b'1 msg at : routing key kern.critical'
[x] 'error.critical':b'2 msg at : routing key error.critical'
[x] 'kern.critical':b'4 msg at : routing key kern.critical'
[x] 'error.critical':b'5 msg at : routing key error.critical'
[x] 'kern.critical':b'7 msg at : routing key kern.critical'
[x] 'error.critical':b'8 msg at : routing key error.critical'

消费者“kern”,只接收含kern的消息

[*] Waiting for logs. To exit press CTRL+C
[x] 'kern.critical':b'1 msg at : routing key kern.critical'
[x] 'kern.critical':b'4 msg at : routing key kern.critical'
[x] 'kern.critical':b'7 msg at : routing key kern.critical'

三、队列信息

在管理页面上可以看到三个队列信息,routing key对应着三个队列的配置

python使用rabbitMQ介绍五(话题模式)

上一篇:JAVA本地环境启动OOM问题排查


下一篇:其他信息: 未能加载文件或程序集“file:///C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb_adoplus.dll”或它的某一个依赖