RabbitMQ消息隊列(二):」Hello, World「

本文將使用Python(pika 0.9.8)實現從Producer到Consumer傳遞數據」Hello, World「。html

首先複習一下上篇所學:RabbitMQ實現了AMQP定義的消息隊列。它實現的功能」很是簡單「:從Producer接收數據而後傳遞到Consumer。它能保證多併發,數據安全傳遞,可擴展。python

和任何的Hello world同樣,它們都不復雜。咱們將會設計兩個程序,一個發送Hello world,另外一個接收這個數據而且打印到屏幕。
總體的設計以下圖:git

RabbitMQ消息隊列(二):」Hello, World「

1. 環境配置

RabbitMQ 實現了AMQP。所以,咱們須要安裝AMPQ的library。幸運的是對於多種編程語言都有實現。咱們可使用如下lib的任何一個:github

在這裏咱們將使用pika. 能夠經過
pip
包管理工具來安裝:編程

$ sudo pip install pika==0.9.8複製代碼

這個安裝依賴於pip和git-core。安全

  • On Ubuntu:
    $ sudo apt-get install python-pip git-core
    複製代碼
  • On Debian:
    $ sudo apt-get install python-setuptools git-core
    $ sudo easy_install pip
    複製代碼
  • On Windows:To install easy_install, run the MS Windows Installer for
    setuptools

    > easy_install pip
    > pip install pika==0.9.8
    複製代碼

2. Sending

RabbitMQ消息隊列(二):」Hello, World「

第一個program send.py:發送Hello world 到queue。正如咱們在上篇文章提到的,你程序的第一句話就是創建鏈接,第二句話就是建立channel:bash

#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
               'localhost'))
channel = connection.channel()複製代碼

建立鏈接傳入的參數就是RabbitMQ Server的ip或者name。架構

關於誰建立queue,上篇文章也討論過:Producer和Consumer都應該去建立。併發

接下來咱們建立名字爲hello的queue:編程語言

channel.queue_declare(queue='hello')複製代碼

建立了channel,咱們能夠經過相應的命令來list queue:

$ sudo rabbitmqctl list_queues
Listing queues ...
hello    0
...done.複製代碼

如今咱們已經準備好了發送了。
從架構圖能夠看出,Producer只能發送到exchange,它是不能直接發送到queue的。如今咱們使用默認的exchange(名字是空字符)。這個默認的exchange容許咱們發送給指定的queue。routing_key就是指定的queue名字。

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

退出前別忘了關閉connection。

connection.close()複製代碼

3. Receiving

RabbitMQ消息隊列(二):」Hello, World「

第二個program receive.py 將從queue中獲取Message而且打印到屏幕。

第一步仍是建立connection。第二步建立channel。第三步建立queue,name = hello:

channel.queue_declare(queue='hello')複製代碼

接下來要subscribe了。在這以前,須要聲明一個回調函數來處理接收到的數據。

def callback(ch, method, properties, body):
    print " [x] Received %r" % (body,)複製代碼

subscribe:

channel.basic_consume(callback,
                      queue='hello',
                      no_ack=True)複製代碼

最後,準備好無限循環監聽吧:

print ' [*] Waiting for messages. To exit press CTRL+C'
channel.start_consuming()複製代碼

4. 最終版本

send.py:

#!/usr/bin/env python
import pika

connection = 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()複製代碼

receive.py:

#!/usr/bin/env python
import pika

connection = 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()複製代碼

5. 最終運行

先運行 send.py program:

$ python send.py
 [x] Sent 'Hello World!'複製代碼

send.py 每次運行完都會中止。注意:如今數據已經存到queue裏了。接收它:

$ python receive.py
 [*] Waiting for messages. To exit press CTRL+C
 [x] Received 'Hello World!'複製代碼

接下來,就要奉上更接近實際環境的例子。取決與個人課餘時間啊。。。

參考文獻:

1. http://www.rabbitmq.com/tutorials/tutorial-one-python.html

相關文章
相關標籤/搜索