最近在研究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, 不声明queue channel.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 # 声明exchange channel.exchange_declare(exchange="logs", exchange_type="fanout") # 不指定queue名字, rabbitmq会随机分配一个名字, 消息处理完成后queue会自动删除 result = channel.queue_declare(exclusive=True) # 获取queue名字 queue_name = result.method.queue # 绑定exchange和queue channel.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, connection channel.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.queue channel.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, connection channel.exchange_declare(exchange="direct_test", exchange_type="direct") result = channel.queue_declare(exclusive=True) queue_name = result.method.queue channel.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.queue key_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.py python consumer01.py python consumer02.py python 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 sys from base import channel, connection # 声明exchange channel.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.queue channel.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.queue channel.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.py python 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.py python consumer02.py
结果:
consumer01.py没收到结果
consumer02.py的结果: body:b'this is a topic test'
通过这个例子我们就能明白topic的运行方式了。
参考自: https://www.jb51.net/article/150386.htm
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]