操作系统:CentOS 6.0
系统内核:linux  2.6.32
Rabbit版本:rabbitmq-3.0.4
 
参考:http://my.oschina.net/alexwu/blog/71159

1. 安装

(1). 安装erlang 语言环境
安装依赖文件
#yum install ncurses-devel
进入 http://www.erlang.org/download.html 选择源文件下载
wget http://www.erlang.org/download/otp_src_R16B.tar.gz
tar zxvf otp_src_R16B.tar.gz
cd otp_src_R16B
阅读HOTO/INSTALL.md文件
#./configure
#make && make install 
安装完成以后,执行erl看是否能打开eshell,用’halt().’退出,注意后面的点号,那是erlang的结束符。
[lijian@localhost ~]$ erl
Erlang R15B01 (erts-5.9.1) [source] [smp:2:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.9.1 (abort with ^G)
1> 2+5.
7
2> halt().
(2).安装RabbitMQ
安装依赖:
yum install xmlto
进入http://www.rabbitmq.com/download.html选择最新的源码包
#wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.0.4/rabbitmq-server-3.0.4.tar.gz
#tar zxvf rabbitmq-server-3.0.4.tar.gz
#cd rabbitmq-server-3.0.4
#make TARGET_DIR=/usr/local/rabbitmq SBIN_DIR=/usr/local/rabbitmq/sbin MAN_DIR=/usr/local/rabbitmq/man
#make TARGET_DIR=/usr/local/rabbitmq SBIN_DIR=/usr/local/rabbitmq/sbin MAN_DIR=/usr/local/rabbitmq/man install 
如果修改过hostname,比如测试机的是centos6.local就要修改/etc/hosts,内容:
127.0.0.1 localhost centos6
192.168.1.21 centos6.local
运行rabbitmq:
# /usr/local/rabbitmq/sbin/rabbitmq-server  -detached
或者:
#  /sbin/service rabbitmq-server stop/start/etc.
查看rabbitmq状态:
# /usr/local/rabbitmq/sbin/rabbitmqctl status
关闭rabbitmq:
	
# /usr/local/rabbitmq/sbin/rabbitmqctl stop

2.python的开发环境搭建

1.安装pika
安装setuptools
https://pypi.python.org/pypi/setuptools#files选择对应系统版本的setuptools安装包,这里使用setuptools-0.6c11-py2.6.egg (md5)
curl -O https://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11-py2.6.egg#md5=bfa92100bd772d5a213eedd356d64086
sh setuptools-0.6c11-py2.6.egg
安装pip:
https://pypi.python.org/packages/source/p/pip/选择最新的版本,这里用pip-1.3.tar.gz
#curl -O https://pypi.python.org/packages/source/p/pip/pip-1.3.tar.gz
#tar zxvf pip-1.3.tar.gz
#cd pip-1.3
#python setup.py install
安装pika:
#pip install pika
或者:
#easy_install pika

3.测试开始

准备下个测试目录,如rabbitmq_app
$ cd 
$ mkdir -p temp/rabbitmq_app
$ cd temp/rabbitmq_app/
$ mkdir tut1 tut2 tut3 tut4 tut5 tut6

2,实例一:来个hello world程序

1
2
3
$ cd tut1
$ vim send.py (代码如下)
$ vim receive.py (代码如下)

首先是消息发送程序: send.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters( 'localhost' ))
channel = connection.channel()
channel.queue_declare(queue = 'hello' )
if len (sys.argv) < 2 :
     print 'message is empty!'
     sys.exit( 0 )
message = sys.argv[ 1 ]
channel.basic_publish(exchange = ' ', routing_key=' hello', body = message)
print " [x] sent: '" + message + "' \n"
connection.close()

跑一下send.py发送一个消息

1
2
3
4
5
6
$ python send.py 'Hello World!'
$ python send.py '你好伍哥'
$ /usr/local/rabbitmq/sbin/rabbitmqctl list_queues
Listing queues ...
hello   2
... done .

如果你也看到hello队列里面有一个消息的话,就证明可以发消息了。
然后写一个接收消息脚本:receive.py

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters( '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 body
 
channel.basic_consume(callback, queue = 'hello' , no_ack = True )
channel.start_consuming()

其中第12行的 no_ack=True 表示消费完了这个消息以后不主动把完成状态通知rabbitmq。
然后开另外一个shell,执行一下receive.py

1
2
3
4
$ python receive.py
[*] Waiting for messages. To exit press CTRL+C
Hello World!
你好伍哥
posted on 2013-04-23 18:18  hustlijian  阅读(745)  评论(2编辑  收藏  举报

无觅相关文章插件,快速提升流量