上週一直在研究zeromq,並且也實現了了zeromq在python和ruby之間的通信,但是如果是一個大型的企業級應用,對消息中間件的要求比較高,比如消息的持久化機制以及系統崩潰恢復等等需求,這個時候zeromq就顯得有點雞肋了,特別是消息持久化是他的的硬傷,那麼怎麼找一個比較適合的中間件呢?
目前市場上主流的中間件除了zeromq,還有rabbitmq,activemq等等,這兩週都比較有名,一個是基於erlang,一個是基於jms,rabbitmq是AMQP(高級消息隊列協議)的標準實現,對於python,ruby等語言都有比較好的支持。
好吧,那就選擇他了,首先我們需要先安裝它:
1$ sudo apt-get install rabbitmq-server -- 一鍵安裝rabbitmq-server
2$ sudo apt-get install python-pip git-core -- 安裝pip
3$ sudo pip install pika==0.9.8 --- 安裝pika就這幾步就裝好啦~!~
裝好了以後首先就是測試下
send.py
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()revecive.py
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()運行下:
$ python send.py [x] Sent 'Hello World!'
$ python receive.py [*] Waiting for messages. To exit press CTRL+C [x] Received 'Hello World!'好了測試完畢了,接下來官方網站有很多例子,大家可以找找看看,一共有6中模式,掌握這6種模式基本上算是入門會用了。