python使用rabbitMQ介绍五(话题模式)
Posted Stitch
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python使用rabbitMQ介绍五(话题模式)相关的知识,希望对你有一定的参考价值。
一、模式介绍
话题模式(Topic)基本思想和路由模式是一样的,只不过路由键支持模糊匹配,符号“#”匹配一个或多个词,符号“*”匹配不多不少一个词
话题模式相当于消息的模糊匹配,或者按照正则匹配。其中
# 是通配符,匹配一个或者多个单词
* 代表匹配一个单词
模式提醒:
当使用#来收消息时,会接收到所有的消息,这样和fanout模式一样了。
当routing key中不含*或者#时,则topic的模式退化为direct的一对一模式。
队列模型:
二、代码示例
发布者:
1 #!/usr/bin/env python 2 import pika 3 import sys 4 5 connection = pika.BlockingConnection(pika.ConnectionParameters(host=\'localhost\')) 6 channel = connection.channel() 7 8 channel.exchange_declare(exchange=\'topic_logs\', 9 exchange_type=\'topic\') 10 11 routing_key = [\'#\', "kern.critical", "A critical kernel error"] 12 for i in range(10): 13 message = \'{} msg at : routing key {}\'.format(i, routing_key[i % 3]) 14 channel.basic_publish(exchange=\'topic_logs\', 15 routing_key=routing_key[i % 3], 16 body=message) 17 print(" [x] Sent :%r" % (message)) 18 connection.close()
消费者:
1 import pika 2 import sys 3 4 connection = pika.BlockingConnection(pika.ConnectionParameters(host=\'localhost\')) 5 channel = connection.channel() 6 7 channel.exchange_declare(exchange=\'topic_logs\', 8 exchange_type=\'topic\') 9 10 result = channel.queue_declare(exclusive=True) 11 queue_name = result.method.queue 12 13 binding_key = "#" 14 15 channel.queue_bind(exchange=\'topic_logs\', 16 queue=queue_name, 17 routing_key=binding_key) 18 19 print(\' [*] Waiting for logs. To exit press CTRL+C\') 20 21 def callback(ch, method, properties, body): 22 print(" [x] %r:%r" % (method.routing_key, body)) 23 24 channel.basic_consume(callback, 25 queue=queue_name, 26 no_ack=True) 27 28 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介绍五(话题模式)的主要内容,如果未能解决你的问题,请参考以下文章