和任何的Hello world一样,它们都不复杂。我们将会设计两个程序,一个发送Hello world,另一个接收这个数据并且打印到屏幕。
整体的设计如下图:
1. 环境配置
RabbitMQ 实现了AMQP。因此,我们需要安装AMPQ的library。幸运的是对于多种编程语言都有实现。我们可以使用以下lib的任何一个:
在这里我们将使用pika. 可以通过 pip 包管理工具来安装:
[plain]
view plain
copy
print
?
- $ sudo pip install pika==0.9.8
<span style="font-size:18px;">$ sudo pip install pika==0.9.8</span>
这个安装依赖于pip和Git-core。
2. Sending
第一个program send.py:发送Hello world 到queue。正如我们在上篇文章提到的,你程序的第一句话就是建立连接,第二句话就是创建channel:
[python]
view plain
copy
print
?
-
- import pika
-
- connection = pika.BlockingConnection(pika.ConnectionParameters(
- 'localhost'))
- channel = connection.channel()
<span style="font-size:18px;">#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost'))
channel = connection.channel()</span>
创建连接传入的参数就是RabbitMQ Server的ip或者name。
关于谁创建queue,上篇文章也讨论过:Producer和Consumer都应该去创建。
接下来我们创建名字为hello的queue:
[cpp]
view plain
copy
print
?
- channel.queue_declare(queue='hello')
<span style="font-size:18px;">channel.queue_declare(queue='hello')</span>
创建了channel,我们可以通过相应的命令来list queue:
[plain]
view plain
copy
print
?
- $ sudo rabbitmqctl list_queues
- Listing queues ...
- hello 0
- ...done.
<span style="font-size:18px;">$ sudo rabbitmqctl list_queues
Listing queues ...
hello 0
...done.</span>
现在我们已经准备好了发送了。
从架构图可以看出,Producer只能发送到exchange,它是不能直接发送到queue的。现在我们使用默认的exchange(名字是空字符)。这个默认的exchange允许我们发送给指定的queue。routing_key就是指定的queue名字。
[python]
view plain
copy
print
?
- channel.basic_publish(exchange='',
- routing_key='hello',
- body='Hello World!')
- print " [x] Sent 'Hello World!'"
<span style="font-size:18px;">channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print " [x] Sent 'Hello World!'"
</span>
退出前别忘了关闭connection。
[python]
view plain
copy
print
?
- connection.close()
<span style="font-size:18px;">connection.close()</span>
3. Receiving
第二个program receive.py 将从queue中获取Message并且打印到屏幕。
第一步还是创建connection。第二步创建channel。第三步创建queue,name = hello:
[python]
view plain
copy
print
?
- channel.queue_declare(queue='hello')
<span style="font-size:18px;">channel.queue_declare(queue='hello')</span>
接下来要subscribe了。在这之前,需要声明一个回调函数来处理接收到的数据。
[python]
view plain
copy
print
?
- def callback(ch, method, properties, body):
- print " [x] Received %r" % (body,)
<span style="font-size:18px;">def callback(ch, method, properties, body):
print " [x] Received %r" % (body,)</span>
subscribe:
[python]
view plain
copy
print
?
- channel.basic_consume(callback,
- queue='hello',
- no_ack=True)
<span style="font-size:18px;">channel.basic_consume(callback,
queue='hello',
no_ack=True)</span>
最后,准备好无限循环监听吧:
[python]
view plain
copy
print
?
- print ' [*] Waiting for messages. To exit press CTRL+C'
- channel.start_consuming()
<span style="font-size:18px;">print ' [*] Waiting for messages. To exit press CTRL+C'
channel.start_consuming()</span>
4. 最终版本
send.py:
[python]
view plain
copy
print
?
-
- 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()
<span style="font-size:18px;">#!/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()</span>
receive.py:
[python]
view plain
copy
print
?
-
- 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()
<span style="font-size:18px;">#!/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()</span>
5. 最终运行
先运行 send.py program:
[python]
view plain
copy
print
?
- $ python send.py
- [x] Sent 'Hello World!'
<span style="font-size:18px;"> $ python send.py
[x] Sent 'Hello World!'</span>
send.py 每次运行完都会停止。注意:现在数据已经存到queue里了。接收它:
[python]
view plain
copy
print
?
- $ python receive.py
- [*] Waiting for messages. To exit press CTRL+C
- [x] Received 'Hello World!'