欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python使用pika庫調用rabbitmq交換機模式詳解

 更新時間:2022年08月31日 16:12:14   作者:IT之一小佬  
這篇文章主要介紹了python使用pika庫調用rabbitmq交換機模式詳解,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下

前言:

交換機模式主要包括:交換機之發(fā)布訂閱、交換機之關鍵字和交換機之通配符。

1、交換機之發(fā)布訂閱

 發(fā)布訂閱和簡單的消息隊列區(qū)別在于,發(fā)布訂閱會將消息發(fā)送給所有的訂閱者,而消息隊列中的數據被消費一次便消失。所以,RabbitMQ實現發(fā)布和訂閱時,會為每一個訂閱者創(chuàng)建一個隊列,而發(fā)布者發(fā)布消息時,會將消息放置在所有相關隊列中。

生產者模式:

示例代碼:

import pika
# 1.連接rabbit
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.124.104'))
channel = connection.channel()
 
# 2.聲明一個名為logs類型的fanout的交換機
channel.exchange_declare(exchange='logs', exchange_type='fanout')  # 發(fā)布訂閱模式參數
 
# 3.向logs交換機中插入數據:"Hello world"
message = 'info:Hello World!'
channel.basic_publish(exchange='logs',
                      routing_key='',
                      body=message,
                      properties=pika.BasicProperties(
                          delivery_mode=2,  # make message persistent
                      )
                      )
print(" [x] Sent 'Hello World!'")

運行結果:

消費者模式:

示例代碼:

import pika
 
# 1.連接rabbit
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.124.104'))
channel = connection.channel()
 
# 2.聲明一個名為logs類型的fanout的交換機
channel.exchange_declare(exchange='logs', exchange_type='fanout')
 
# 3.創(chuàng)建隊列
result = channel.queue_declare("", exclusive=True)  # 隨機生成一個隊列名
queue_name = result.method.queue
print(queue_name)
 
# 4.將指定隊列綁定到交換機上
channel.queue_bind(exchange='logs', queue=queue_name)
 
# 5.確定回調函數
def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    ch.basic_ack(delivery_tag=method.delivery_tag)

# 6.確定監(jiān)聽隊列參數
channel.basic_consume(queue=queue_name,  # 指定隊列
                      auto_ack=False,  # 手動應答方式
                      on_message_callback=callback)
print(' [*] Waiting for messages. To exit press CTRL+C')
# 7.正式監(jiān)聽
channel.start_consuming()

運行結果:【將程序重復執(zhí)行三次,三個消費者都收到了同樣的消息】

2、交換機之關鍵字

生產者模式:

示例代碼:  【將info分別改為warning、error運行】

import pika
 
# 1.連接rabbit
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.124.104'))
channel = connection.channel()
 
# 2.聲明一個名為logs類型的direct的交換機
channel.exchange_declare(exchange='logs2', exchange_type='direct')  # 發(fā)布訂閱模式參數
 
# 3.向logs交換機中插入數據:"Hello world"
message = 'info:Hello World!'
channel.basic_publish(exchange='logs2',
                      routing_key='info',  # info信息
                      body=message,
                      properties=pika.BasicProperties(
                          delivery_mode=2,  # make message persistent
                      )
                      )
print(" [x] Sent 'Hello World!'")

運行結果:

消費者模式:

示例代碼1:

import pika
 
# 1.連接rabbit
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.124.104'))
channel = connection.channel()
 
# 2.聲明一個名為logs類型的direct的交換機
channel.exchange_declare(exchange='logs2', exchange_type='direct')
 
# 3.創(chuàng)建隊列
result = channel.queue_declare("", exclusive=True)  # 隨機生成一個隊列名
queue_name = result.method.queue
print(queue_name)
 
# 4.將指定隊列綁定到交換機上
channel.queue_bind(exchange='logs2', queue=queue_name, routing_key='info')
channel.queue_bind(exchange='logs2', queue=queue_name, routing_key='waring')
channel.queue_bind(exchange='logs2', queue=queue_name, routing_key='error')
 
# # 使用for循環(huán)將指定隊列綁定到交換機上
# for key in ['info', 'waring', 'error']:
#     channel.queue_bind(exchange='logs2', queue=queue_name, routing_key=key)
 
# 5.確定回調函數
def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    ch.basic_ack(delivery_tag=method.delivery_tag)
 
# 6.確定監(jiān)聽隊列參數
channel.basic_consume(queue=queue_name,  # 指定隊列
                      auto_ack=False,  # 手動應答方式
                      on_message_callback=callback)
print(' [*] Waiting for messages. To exit press CTRL+C')
# 7.正式監(jiān)聽
channel.start_consuming()

運行結果:

示例代碼2:

import pika
 
# 1.連接rabbit
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.124.104'))
channel = connection.channel()
 
# 2.聲明一個名為logs類型的direct的交換機
channel.exchange_declare(exchange='logs2', exchange_type='direct')
 
# 3.創(chuàng)建隊列
result = channel.queue_declare("", exclusive=True)  # 隨機生成一個隊列名
queue_name = result.method.queue
print(queue_name)
 
# 4.將指定隊列綁定到交換機上
channel.queue_bind(exchange='logs2', queue=queue_name, routing_key='info')
 
# # 使用for循環(huán)將指定隊列綁定到交換機上
# for key in ['info', 'waring', 'error']:
#     channel.queue_bind(exchange='logs2', queue=queue_name, routing_key=key)
# 5.確定回調函數
def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    ch.basic_ack(delivery_tag=method.delivery_tag)

# 6.確定監(jiān)聽隊列參數
channel.basic_consume(queue=queue_name,  # 指定隊列
                      auto_ack=False,  # 手動應答方式
                      on_message_callback=callback)
print(' [*] Waiting for messages. To exit press CTRL+C')
# 7.正式監(jiān)聽
channel.start_consuming()

運行結果:

示例代碼3:

import pika
 
# 1.連接rabbit
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.124.104'))
channel = connection.channel()
 
# 2.聲明一個名為logs類型的direct的交換機
channel.exchange_declare(exchange='logs2', exchange_type='direct')
 
# 3.創(chuàng)建隊列
result = channel.queue_declare("", exclusive=True)  # 隨機生成一個隊列名
queue_name = result.method.queue
print(queue_name)
 
# 4.將指定隊列綁定到交換機上
channel.queue_bind(exchange='logs2', queue=queue_name, routing_key='error')
 
# # 使用for循環(huán)將指定隊列綁定到交換機上
# for key in ['info', 'waring', 'error']:
#     channel.queue_bind(exchange='logs2', queue=queue_name, routing_key=key)
 
# 5.確定回調函數
def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    ch.basic_ack(delivery_tag=method.delivery_tag)
 
# 6.確定監(jiān)聽隊列參數
channel.basic_consume(queue=queue_name,  # 指定隊列
                      auto_ack=False,  # 手動應答方式
                      on_message_callback=callback)
print(' [*] Waiting for messages. To exit press CTRL+C')
# 7.正式監(jiān)聽
channel.start_consuming()

運行結果:

3、交換機之通配符

通配符交換機”與之前的路由模式相比,它將信息的傳輸類型的key更加細化,以“key1.key2.keyN....”的模式來指定信息傳輸的key的大類型和大類型下面的小類型,讓消費者可以更加精細的確認自己想要獲取的信息類型。而在消費者一段,不用精確的指定具體到哪一個大類型下的小類型的key,而是可以使用類似正則表達式(但與正則表達式規(guī)則完全不同)的通配符在指定一定范圍或符合某一個字符串匹配規(guī)則的key,來獲取想要的信息。

“通配符交換機”(Topic Exchange)將路由鍵和某模式進行匹配。此時隊列需要綁定在一個模式上。符號“#”匹配一個或多個詞,符號“*”僅匹配一個詞。因此“audit.#”能夠匹配到“audit.irs.corporate”,但是“audit.*”只會匹配到“audit.irs”。(這里與一般的正則表達式的“*”和“#”剛好相反,這里我們需要注意一下。)

生產者模式:

示例代碼:  【分別將routing_key改為usa.news、news.usa和usa.weather執(zhí)行一遍】

import pika
 
# 1.連接rabbit
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.124.104'))
channel = connection.channel()
 
# 2.聲明一個名為logs類型的topic的交換機
channel.exchange_declare(exchange='logs3', exchange_type='topic')  # 發(fā)布訂閱模式參數
 
# 3.向logs交換機中插入數據:"Hello world"
message = 'usa.news---------'
channel.basic_publish(exchange='logs3',
                      routing_key='usa.news',  # usa.news
                      body=message,
                      properties=pika.BasicProperties(
                          delivery_mode=2,  # make message persistent
                      )
                      )
print(" [x] Sent 'Hello World!'")

運行結果:

消費者模式:

示例代碼1:

import pika
 
# 1.連接rabbit
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.124.104'))
channel = connection.channel()
 
# 2.聲明一個名為logs類型的topic的交換機
channel.exchange_declare(exchange='logs3', exchange_type='topic')
 
# 3.創(chuàng)建隊列
result = channel.queue_declare("", exclusive=True)  # 隨機生成一個隊列名
queue_name = result.method.queue
print(queue_name)
 
# 4.將指定隊列綁定到交換機上
channel.queue_bind(exchange='logs3', queue=queue_name, routing_key='news.#')
 
# # 使用for循環(huán)將指定隊列綁定到交換機上
# for key in ['info.#', 'waring.#', 'error.#']:
#     channel.queue_bind(exchange='logs2', queue=queue_name, routing_key=key)
# 5.確定回調函數
def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    ch.basic_ack(delivery_tag=method.delivery_tag)
 
 
# 6.確定監(jiān)聽隊列參數
channel.basic_consume(queue=queue_name,  # 指定隊列
                      auto_ack=False,  # 手動應答方式
                      on_message_callback=callback)
 
print(' [*] Waiting for messages. To exit press CTRL+C')
# 7.正式監(jiān)聽
channel.start_consuming()

運行結果:

示例代碼2:

import pika
 
# 1.連接rabbit
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.124.104'))
channel = connection.channel()
 
# 2.聲明一個名為logs類型的topic的交換機
channel.exchange_declare(exchange='logs3', exchange_type='topic')
 
# 3.創(chuàng)建隊列
result = channel.queue_declare("", exclusive=True)  # 隨機生成一個隊列名
queue_name = result.method.queue
print(queue_name)
 
# 4.將指定隊列綁定到交換機上
channel.queue_bind(exchange='logs3', queue=queue_name, routing_key='#.news')
 
# # 使用for循環(huán)將指定隊列綁定到交換機上
# for key in ['info.#', 'waring.#', 'error.#']:
#     channel.queue_bind(exchange='logs2', queue=queue_name, routing_key=key)

# 5.確定回調函數
def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    ch.basic_ack(delivery_tag=method.delivery_tag)
# 6.確定監(jiān)聽隊列參數
channel.basic_consume(queue=queue_name,  # 指定隊列
                      auto_ack=False,  # 手動應答方式
                      on_message_callback=callback)
print(' [*] Waiting for messages. To exit press CTRL+C')
# 7.正式監(jiān)聽
channel.start_consuming()

運行結果:

示例代碼3:

import pika
 
# 1.連接rabbit
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.124.104'))
channel = connection.channel()
 
# 2.聲明一個名為logs類型的topic的交換機
channel.exchange_declare(exchange='logs3', exchange_type='topic')
 
# 3.創(chuàng)建隊列
result = channel.queue_declare("", exclusive=True)  # 隨機生成一個隊列名
queue_name = result.method.queue
print(queue_name)
 
# 4.將指定隊列綁定到交換機上
channel.queue_bind(exchange='logs3', queue=queue_name, routing_key='#.weather')
 
# # 使用for循環(huán)將指定隊列綁定到交換機上
# for key in ['info.#', 'waring.#', 'error.#']:
#     channel.queue_bind(exchange='logs2', queue=queue_name, routing_key=key)

# 5.確定回調函數
def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    ch.basic_ack(delivery_tag=method.delivery_tag)
 
 
# 6.確定監(jiān)聽隊列參數
channel.basic_consume(queue=queue_name,  # 指定隊列
                      auto_ack=False,  # 手動應答方式
                      on_message_callback=callback)
print(' [*] Waiting for messages. To exit press CTRL+C')
# 7.正式監(jiān)聽
channel.start_consuming()

運行結果:

到此這篇關于python使用pika庫調用rabbitmq交換機模式詳解的文章就介紹到這了,更多相關python rabbitmq交換機模式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • python批量提交沙箱問題實例

    python批量提交沙箱問題實例

    這篇文章主要介紹了python批量提交沙箱問題實例,針對批量提交沙箱出現的問題進行了針對性的分析與實例講解,具有不錯的參考借鑒價值,需要的朋友可以參考下
    2014-10-10
  • python實現俄羅斯方塊小游戲

    python實現俄羅斯方塊小游戲

    這篇文章主要為大家詳細介紹了python實現俄羅斯方塊小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • Python中操作mysql的pymysql模塊詳解

    Python中操作mysql的pymysql模塊詳解

    這篇文章給大家演示了如何安裝以及使用Python中操作mysql的pymysql模塊,本文介紹的很詳細,對大家學習Python具有一定參考借鑒價值,有需要的朋友們一起來看看吧。
    2016-09-09
  • Python中的struct.unpack示例詳解

    Python中的struct.unpack示例詳解

    在Python中,struct.unpack是一個函數,用于將字節(jié)串轉換為元組,這個函數在處理二進制數據時非常有用,這篇文章主要介紹了Python中的struct.unpack示例詳解,需要的朋友可以參考下
    2023-12-12
  • 一篇文章帶你了解python元組基礎

    一篇文章帶你了解python元組基礎

    今天小編就為大家分享一篇關于Python中的元組介紹,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2021-08-08
  • Python實現的隨機森林算法與簡單總結

    Python實現的隨機森林算法與簡單總結

    這篇文章主要介紹了Python實現的隨機森林算法,結合實例形式詳細分析了隨機森林算法的概念、原理、實現技巧與相關注意事項,需要的朋友可以參考下
    2018-01-01
  • Python制作動態(tài)字符圖的實例

    Python制作動態(tài)字符圖的實例

    今天小編就為大家分享一篇關于Python制作動態(tài)字符圖的實例,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • 你需要學會的8個Python列表技巧

    你需要學會的8個Python列表技巧

    這篇文章主要介紹了8個常用的Python列表技巧,文中講解非常詳細,幫助大家更好的學習Python,感興趣的朋友可以了解下
    2020-06-06
  • 15個Pythonic的代碼示例(值得收藏)

    15個Pythonic的代碼示例(值得收藏)

    這篇文章主要介紹了15個Pythonic的代碼示例(值得收藏),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • Python基礎實戰(zhàn)總結

    Python基礎實戰(zhàn)總結

    今天要給大家介紹的是Python基礎實戰(zhàn),本文主要以舉例說明講解:問題的關鍵點就是在于構造姓名,學號和成績,之后以字典的形式進行寫入文件。這里準備兩個列表,一個姓,一個名,之后使用random庫進行隨機字符串拼接,得到姓名,需要的朋友可以參考一下
    2021-10-10

最新評論