时间:2021-05-22
最近在研究redis做消息队列时,顺便看了一下RabbitMQ做消息队列的实现。以下是总结的RabbitMQ中三种exchange模式的实现,分别是fanout, direct和topic。
base.py:
import pika# 获取认证对象,参数是用户名、密码。远程连接时需要认证credentials = pika.PlainCredentials("admin", "admin")# BlockingConnection(): 实例化连接对象# ConnectionParameters(): 实例化链接参数对象connection = pika.BlockingConnection(pika.ConnectionParameters( "192.168.0.102", 5672, "/", credentials))# 创建新的channel(通道)channel = connection.channel()fanout模式:向绑定到指定exchange的queue中发送消息,消费者从queue中取出数据,类似于广播模式、发布订阅模式。
绑定方式: 在接收端channel.queue_bind(exchange="logs", queue=queue_name)
代码:
publisher.py:
from base import channel, connection# 声明exchange, 不声明queuechannel.exchange_declare(exchange="logs", exchange_type="fanout") # 广播message = "hello fanout"channel.basic_publish( exchange="logs", routing_key="", body=message)connection.close()consumer.py:
from base import channel, connection # 声明exchangechannel.exchange_declare(exchange="logs", exchange_type="fanout")# 不指定queue名字, rabbitmq会随机分配一个名字, 消息处理完成后queue会自动删除result = channel.queue_declare(exclusive=True) # 获取queue名字queue_name = result.method.queue# 绑定exchange和queuechannel.queue_bind(exchange="logs", queue=queue_name)def callback(ch, method, properties, body): print("body:%s" % body)channel.basic_consume( callback, queue=queue_name)channel.start_consuming()direct模式:发送端绑定一个routing_key1, queue中绑定若干个routing_key2, 若key1与key2相等,或者key1在key2中,则消息就会发送到这个queue中,再由相应的消费者去queue中取数据。
publisher.py:
from base import channel, connectionchannel.exchange_declare(exchange="direct_test", exchange_type="direct")message = "hello"channel.basic_publish( exchange="direct_test", routing_key="info", # 绑定key body=message)connection.close()consumer01.py:
from base import channel, connection channel.exchange_declare(exchange="direct_test", exchange_type="direct")result = channel.queue_declare(exclusive=True)queue_name = result.method.queuechannel.queue_bind( exchange="direct_test", queue=queue_name, # 绑定的key,与publisher中的相同 routing_key="info" )def callback(ch, method, properties, body): print("body:%s" % body)channel.basic_consume( callback, queue=queue_name)channel.start_consuming()consumer02.py:
from base import channel, connectionchannel.exchange_declare(exchange="direct_test", exchange_type="direct")result = channel.queue_declare(exclusive=True)queue_name = result.method.queuechannel.queue_bind( exchange="direct_test", queue=queue_name, # 绑定的key routing_key="error" )def callback(ch, method, properties, bosy): print("body:%s" % body)channel.basic_consume( callback, queue=queue_name)channel.start_consuming()consumer03.py:
from base import channel, connection channel.exchange_declare(exchange="direct_test", exchange_type="direct")result = channel.queue_declare(exclusive=True)queue_name = result.method.queuekey_list = ["info", "warning"]for key in key_list: channel.queue_bind( exchange="direct_test", queue=queue_name, # 一个queue同时绑定多个key,有一个key满足条件时就可以收到数据 routing_key=key )def callback(ch, method, properties, body): print("body:%s" % body)channel.basic_consume( callback, queue=queue_name)channel.start_consuming()执行:
python producer.pypython consumer01.pypython consumer02.pypython consumer03.py结果:
consumer01.py: body:b'hello'
consumer02.py没收到结果
consumer03.py: body:b'hello'
topic模式不是太好理解,我的理解如下:
对于发送端绑定的routing_key1,queue绑定若干个routing_key2;若routing_key1满足任意一个routing_key2,则该消息就会通过exchange发送到这个queue中,然后由接收端从queue中取出其实就是direct模式的扩展。
绑定方式:
发送端绑定:
channel.basic_publish( exchange="topic_logs", routing_key=routing_key, body=message )接收端绑定:
channel.queue_bind( exchange="topic_logs", queue=queue_name, routing_key=binding_key )publisher.py:
import sysfrom base import channel, connection# 声明exchangechannel.exchange_declare(exchange="topic_test", exchange_type="topic")# 待发送消息message = " ".join(sys.argv[1:]) or "hello topic"# 发布消息channel.basic_publish( exchange="topic_test", routing_key="mysql.error", # 绑定的routing_key body=message)connection.close()consumer01.py:
from base import channel, connection channel.exchange_declare(exchange="topic_test", exchange_type="topic")result = channel.queue_declare(exclusive=True)queue_name = result.method.queuechannel.queue_bind( exchange="topic_test", queue=queue_name, routing_key="*.error" # 绑定的routing_key)def callback(ch, method, properties, body): print("body:%s" % body)channel.basic_consume( callback, queue=queue_name, no_ack=True)channel.start_consuming()consumer02.py:
from base import channel, connection channel.exchange_declare(exchange="topic_test", exchange_type="topic")result = channel.queue_declare(exclusive=True)queue_name = result.method.queuechannel.queue_bind( exchange="topic_test", queue=queue_name, routing_key="mysql.*" # 绑定的routing_key)def callback(ch, method, properties, body): print("body:%s" % body)channel.basic_consume( callback, queue=queue_name, no_ack=True)channel.start_consuming()执行:
python publisher02.py "this is a topic test"python consumer01.pypython consumer02.py结果:
consumer01.py的结果: body:b'this is a topic test'
consumer02.py的结果: body:b'this is a topic test'
说明通过绑定相应的routing_key,两个消费者都收到了消息
将publisher.py的routing_key改成"mysql.info"
再此执行:
python publisher02.py "this is a topic test"python consumer01.pypython consumer02.py结果:
consumer01.py没收到结果
consumer02.py的结果: body:b'this is a topic test'
通过这个例子我们就能明白topic的运行方式了。
参考自: https://www.jb51.net/article/150386.htm
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
导语RabbitMQ想必大家都有了解,不做多介绍来。这里实现的是用RabbitMQ作为Larvel队列的驱动,替代Redis。下面以Laradock中安装示例。
Python使用Pika库(安装:sudopipinstallpika)可以操作RabbitMQ消息队列服务器(安装:sudoapt-getinstallrab
1:RabbitMQ是个啥?(专业术语参考自网络) RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件)。 Rabb
目前公司用阿里云+redis的方式实现的消息队列。了解了目前几种主流的消息组件(主要包括rabbitmq、kafka、)的优缺点后,这里为了深入学习rabbit
RabbitMQ消息队列PYthreadingQueue进程Queue父进程与子进程,或同一父进程下的多个子进程进行交互缺点:两个不同Python文件不能通过上