中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

rabbitmq_消息持久化_消息公平分發_消息廣播

發布時間:2020-07-20 14:47:50 來源:網絡 閱讀:472 作者:leiwenbin627 欄目:編程語言

生產者_procudure_send_消息持久化
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
   
'localhost'))  # rabbit默認端口5672 建立一個基本的 socket連接
channel = connection.channel()  # 聲明一個管道 在管道里面發消息

#
聲明queue
channel.queue_declare(queue='hello',durable=True)#durable=True隊列持久化

# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
channel.basic_publish(exchange='',
                     
routing_key='hello'# queue名字
                     
body='Hello World!'# body 發送的消息
                     
properties=pika.BasicProperties(delivery_mode=2),#消息持久化
                         
)
print(" [x] Sent 'Hello World!'")
connection.close()

 

消費者_consumer_recive_消息公平分發
# _*_coding:utf-8_*_
__author__ = 'Alex Li'
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
   
'localhost')) #rabbit默認端口5672 建立一個基本的 socket連接
channel = connection.channel()#聲明一個管道 在管道里面收消息

# You may ask why we declare the queue again ? we have already declared it in our previous code.
# We could avoid that if we were sure that the queue already exists. For example if send.py program
# was run before. But we're not yet sure which program to run first. In such cases it's a good
# practice to repeat declaring the queue in both programs.
channel.queue_declare(queue='hello1')#聲明queue 消費持久化的消息


def callback(ch, method, properties, body):#處理消息
   
print("---->",ch,method,properties)#ch 管道內存對象地址 method:發給queue的信息
   
print(" [x] Received %r" % body)
    ch.basic_ack(
delivery_tag=method.delivery_tag)# 手動確認消息是否接收

channel.basic_qos(prefetch_count=1)#根據權重發消息 一對一發
channel.basic_consume(#消費消息
          
callback,#如果收到消息,就調用CALLBACK函數來處理消息
          
queue='hello1',#從哪個隊列里收消息
           #no_ack=True,
           
)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
#啟動 開始收消息 一直收,沒有就卡主

 

生產者_send_廣播模式
import pika
import sys

connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()

channel.exchange_declare(
exchange='logging',exchange_type='fanout')

messages =
' '.join(sys.argv[1:]) or "info: Hello World!"
#messages="info: Hello World!"
channel.basic_publish(exchange='yyyy',
                     
routing_key='',
                      
body=messages)
print(" [x] Sent %r" % messages)
connection.close()

 

消費者_recive_廣播模式
# _*_coding:utf-8_*_
__author__ = 'Alex Li'
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()

channel.exchange_declare(
exchange='yyyy',
                        
exchange_type='fanout')

result = channel.queue_declare(
exclusive=True# 不指定queue名字,rabbit會隨機分配一個名字,exclusive=True會在使用此queue的消費者斷開后,自動將queue刪除
queue_name = result.method.queue # 隨機分配的queue
print("random queue name:",queue_name)

channel.queue_bind(
exchange='yyyy',
                  
queue=queue_name)#綁定轉發器

print(' [*] Waiting for logs. To exit press CTRL+C')


def callback(ch, method, properties, body):
   
print(" [x] %r" % body)


channel.basic_consume(callback,
                     
queue=queue_name,
                     
no_ack=True)

channel.start_consuming()
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

曲阳县| 赣州市| 通化县| 湖南省| 新竹县| 尤溪县| 炎陵县| 山东省| 克山县| 温泉县| 库车县| 于都县| 双城市| 三原县| 陈巴尔虎旗| 达孜县| 横峰县| 郸城县| 博湖县| 塔城市| 平果县| 汝南县| 普定县| 江西省| 铜鼓县| 田阳县| 上栗县| 长宁区| 游戏| 施秉县| 耒阳市| 兴化市| 平山县| 麻阳| 博爱县| 浙江省| 安龙县| 霍林郭勒市| 邯郸市| 句容市| 西林县|