(转)RabbitMQ消息队列(二):”Hello, World“

本文将使用Python(pika 0.9.8)实现从Producer到Consumer传递数据”Hello, World“。python

     首先复习一下上篇所学:RabbitMQ实现了AMQP定义的消息队列。它实现的功能”很是简单“:从Producer接收数据而后传递到Consumer。它能保证多并发,数据安全传递,可扩展。git

     和任何的Hello world同样,它们都不复杂。咱们将会设计两个程序,一个发送Hello world,另外一个接收这个数据而且打印到屏幕。
      总体的设计以下图:github



1. 环境配置

RabbitMQ 实现了AMQP。所以,咱们须要安装AMPQ的library。幸运的是对于多种编程语言都有实现。咱们可使用如下lib的任何一个:编程

在这里咱们将使用pika. 能够经过 pip 包管理工具来安装:安全

 

[plain]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. $ 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

第一个program send.py:发送Hello world 到queue。正如咱们在上篇文章提到的,你程序的第一句话就是创建链接,第二句话就是建立channel:工具

 

[python]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. #!/usr/bin/env python  
  2. import pika  
  3.   
  4. connection = pika.BlockingConnection(pika.ConnectionParameters(  
  5.                'localhost'))  
  6. channel = connection.channel()  

建立链接传入的参数就是RabbitMQ Server的ip或者name。

 

关于谁建立queue,上篇文章也讨论过:Producer和Consumer都应该去建立。

接下来咱们建立名字为hello的queue:

 

[cpp]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. channel.queue_declare(queue='hello')  

建立了channel,咱们能够经过相应的命令来list queue:

 

 

[plain]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. $ sudo rabbitmqctl list_queues  
  2. Listing queues ...  
  3. hello    0  
  4. ...done.  

如今咱们已经准备好了发送了。
从架构图能够看出,Producer只能发送到exchange,它是不能直接发送到queue的。如今咱们使用默认的exchange(名字是空字符)。这个默认的exchange容许咱们发送给指定的queue。routing_key就是指定的queue名字。

[python]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. channel.basic_publish(exchange='',  
  2.                       routing_key='hello',  
  3.                       body='Hello World!')  
  4. print " [x] Sent 'Hello World!'"  

退出前别忘了关闭connection。

[python]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. connection.close()  

 

 

3. Receiving

第二个program receive.py 将从queue中获取Message而且打印到屏幕。

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

 

[python]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. channel.queue_declare(queue='hello')  

 

接下来要subscribe了。在这以前,须要声明一个回调函数来处理接收到的数据。

 

[python]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. def callback(ch, method, properties, body):  
  2.     print " [x] Received %r" % (body,)  

subscribe:

 

 

[python]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. channel.basic_consume(callback,  
  2.                       queue='hello',  
  3.                       no_ack=True)  

最后,准备好无限循环监听吧:

 

 

[python]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. print ' [*] Waiting for messages. To exit press CTRL+C'  
  2. channel.start_consuming()  

 

 

4. 最终版本

send.py:

 

[python]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. #!/usr/bin/env python  
  2. import pika  
  3.   
  4. connection = pika.BlockingConnection(pika.ConnectionParameters(  
  5.         host='localhost'))  
  6. channel = connection.channel()  
  7.   
  8. channel.queue_declare(queue='hello')  
  9.   
  10. channel.basic_publish(exchange='',  
  11.                       routing_key='hello',  
  12.                       body='Hello World!')  
  13. print " [x] Sent 'Hello World!'"  
  14. connection.close()  

 

 receive.py:

 

[python]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. #!/usr/bin/env python  
  2. import pika  
  3.   
  4. connection = pika.BlockingConnection(pika.ConnectionParameters(  
  5.         host='localhost'))  
  6. channel = connection.channel()  
  7.   
  8. channel.queue_declare(queue='hello')  
  9.   
  10. print ' [*] Waiting for messages. To exit press CTRL+C'  
  11.   
  12. def callback(ch, method, properties, body):  
  13.     print " [x] Received %r" % (body,)  
  14.   
  15. channel.basic_consume(callback,  
  16.                       queue='hello',  
  17.                       no_ack=True)  
  18.   
  19. channel.start_consuming()  

 

 

5. 最终运行

先运行 send.py program:

 

[python]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. $ python send.py  
  2. [x] Sent 'Hello World!'  

send.py 每次运行完都会中止。注意:如今数据已经存到queue里了。接收它:

 

 

[python]  view plain copy 在CODE上查看代码片 派生到个人代码片
 
  1. $ python receive.py  
  2. [*] Waiting for messages. To exit press CTRL+C  
  3. [x] Received 'Hello World!'  

 

接下来,就要奉上更接近实际环境的例子。取决与个人课余时间啊。。

相关文章
相关标签/搜索