日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

RabbitMQ学习二

發(fā)布時間:2024/1/17 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 RabbitMQ学习二 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

RabbitMQ 是一個消息broker。它的主要概念就是接受和轉(zhuǎn)發(fā)消息。可以把它當(dāng)作一個郵局:當(dāng)向郵箱投遞一封郵件時,你確信郵差最終會將這封郵件投遞到收件人。使用這個比喻,RabbitMQ就是郵箱,郵局和郵差。

RabbitMQ和郵局最大的區(qū)別就是它不處理紙質(zhì)信件而是處理二進制數(shù)據(jù)--消息

RabbitMQ和其他消息系統(tǒng)通常都有以下幾個術(shù)語:

生產(chǎn)者 ? 發(fā)送消息

? ?

? ?

隊列



消費者




使用python驅(qū)動發(fā)送"Hello World!"




pip install pika



第一個測試程序send.py將會向這個隊列發(fā)送單個消息。首要做的是向RabbitMQ Server建立一個連接。




#!/usr/bin/env?python import?pika connection?=?pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel?=?connection.channel()



默認(rèn)情況下是不能使用guest用戶登錄的

In?[15]:?import?pikaIn?[16]:?credentials=pika.PlainCredentials('xxx','xxx')In?[17]:?parameters=pika.ConnectionParameters(host='172.28.10.71',credentials=credentials)





在發(fā)送消息之前需要確保收件人隊列存在,如果發(fā)送消息到不存在的地點,RabbitMQ將會直接丟棄這個消息。

創(chuàng)建一個用于投遞消息的隊列


channel.queue_declare(queue='hello')


使用RabbitMQ,不能直接發(fā)送一條消息到隊列。它需要經(jīng)過一個交換機exchange.


channel.basic_publish(exchange='',routing_key='hello',body='Hello?World!') print?"?[x]?Sent?'Hello?World!'"


在退出程序之前we need to make sure the network buffers were flushed and our message was actually delivered to RabbitMQ.只需要關(guān)閉連接就行。


connection.close()



完成的程序 send.py

#!/usr/bin/pythonimport?pikaconnection=pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) channel=connection.channel() channel.queue_declare(queue='hello') channel.basic_publish(exchange='',routing_key='hello',body='Hello?World!') print?"[x]?Sent?'Hello?World!'" connection.close()



...............[x]?Sent?'Hello?World!' [root@test71?rabbitmq]#?python?send.py? [x]?Sent?'Hello?World!' [root@test71?rabbitmq]#?python?send.py? [x]?Sent?'Hello?World!' [root@test71?rabbitmq]#?python?send.py? [x]?Sent?'Hello?World!' [root@test71?rabbitmq]#?python?send.py? [x]?Sent?'Hello?World!' [root@test71?rabbitmq]#?python?send.py? [x]?Sent?'Hello?World!' [root@test71?rabbitmq]#?python?send.py? [x]?Sent?'Hello?World!' [root@test71?rabbitmq]#?python?send.py? [x]?Sent?'Hello?World!' [root@test71?rabbitmq]#?python?send.py? [x]?Sent?'Hello?World!' [root@test71?rabbitmq]#?python?send.py? [x]?Sent?'Hello?World!' [root@test71?rabbitmq]#?python?send.py? [x]?Sent?'Hello?World!' [root@test71?rabbitmq]#?rabbitmqctl?list_queues Listing?queues?... hello 28


每執(zhí)行一次就往名為hello的隊列中添加一條消息,使用rabbitmqctl list_queues可以查看隊列長度



第二個程序receive.py 將會從這個隊列中接收消息并打印它們

首先需要連接RabbitMQ,然后需要確保隊列存在

channel.queue_declare(queue='hello')


從隊列中接受消息要復(fù)雜得多,它的工作方式是向這個隊列訂閱一個callback函數(shù)。任何時候,接收到一條消息,callback函數(shù)會被pika庫調(diào)用


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


然后需要告訴RabbitMQ這個特別的函數(shù)是需要從hello隊列接收消息

channel.basic_consume(callback,queue='hello',no_ack=True)


最后,引入一個永不結(jié)束的循環(huán)等待接收數(shù)據(jù)并運行callback函數(shù)


print?'?[*]?Waiting?for?messages.?To?exit?press?CTRL+C'channel.start_consuming()



receive.py

#!/usr/bin/pythonimport?pikaconnection=pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) channel=connection.channel() channel.queue_declare(queue='hello')print?'[*]?Waiting?for?messages.To?exit?press?CTRL+c'def?callback(ch,method,properties,body):print?"[x]?Received?%r"?%(body,)channel.basic_consume(callback,queue='hello',no_ack=True) channel.start_consuming()



然后測試:

[root@test71?rabbitmq]#?python?send.py? [x]?Sent?'Hello?World!' [root@test71?rabbitmq]#?python?send.py? [x]?Sent?'Hello?World!' [root@test71?rabbitmq]#?python?send.py? [x]?Sent?'Hello?World!' [root@test71?rabbitmq]#?python?send.py? [x]?Sent?'Hello?World!' [root@test71?rabbitmq]#?python?send.py? [x]?Sent?'Hello?World!' [root@test71?rabbitmq]#?python?send.py? [x]?Sent?'Hello?World!' [root@test71?rabbitmq]#?python?send.py? [x]?Sent?'Hello?World!' [root@test71?rabbitmq]#?python?send.py? [x]?Sent?'Hello?World!' [root@test71?rabbitmq]#?python?send.py? [x]?Sent?'Hello?World!'


[root@test71?rabbitmq]#?python?receive.py? [*]?Waiting?for?messages.To?exit?press?CTRL+c [x]?Received?'Hello?World!' [x]?Received?'Hello?World!' [x]?Received?'Hello?World!' [x]?Received?'Hello?World!' [x]?Received?'Hello?World!' [x]?Received?'Hello?World!' [x]?Received?'Hello?World!' [x]?Received?'Hello?World!' [x]?Received?'Hello?World!' ^CTraceback?(most?recent?call?last):File?"receive.py",?line?16,?in?<module>channel.start_consuming()File?"/usr/lib/python2.6/site-packages/pika/adapters/blocking_connection.py",?line?955,?in?start_consumingself.connection.process_data_events()File?"/usr/lib/python2.6/site-packages/pika/adapters/blocking_connection.py",?line?240,?in?process_data_eventsif?self._handle_read():File?"/usr/lib/python2.6/site-packages/pika/adapters/blocking_connection.py",?line?347,?in?_handle_readif?self._read_poller.ready():File?"/usr/lib/python2.6/site-packages/pika/adapters/blocking_connection.py",?line?43,?in?innerreturn?f(*args,?**kwargs)File?"/usr/lib/python2.6/site-packages/pika/adapters/blocking_connection.py",?line?85,?in?readyevents?=?self.poller.poll(self.poll_timeout) KeyboardInterrupt








參考資料:

http://previous.rabbitmq.com/v3_3_x/tutorials/tutorial-one-python.html


轉(zhuǎn)載于:https://blog.51cto.com/john88wang/1670904

總結(jié)

以上是生活随笔為你收集整理的RabbitMQ学习二的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。