国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現RabbitMQ的消息隊列的示例代碼

2020-02-15 23:34:07
字體:
來源:轉載
供稿:網友

最近在研究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()            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 徐州市| 许昌市| 江城| 武定县| 乌鲁木齐市| 邻水| 措勤县| 夹江县| 伊金霍洛旗| 双鸭山市| 桃园县| 油尖旺区| 广宗县| 合肥市| 安吉县| 永新县| 虞城县| 宣汉县| 丹寨县| 南华县| 扬中市| 石渠县| 桃园县| 康保县| 南川市| 芒康县| 四会市| 青河县| 四川省| 焦作市| 连平县| 黑龙江省| 临泽县| 诏安县| 曲靖市| 抚松县| 巴中市| 防城港市| 新乐市| 新乐市| 兴城市|