ubuntu 12.04安装配置rabbitmq
在安装rabbitmq之前,需要先安装一些依赖关系,比如erlang,下面介绍安装方法:
一. 安装erlang
1.安装依赖关系包
sudo apt-get install build-essential libncurses5-dev m4 libssl-dev unixodbc unixodbc-dev libc6 freeglut3-dev libwxgtk2.8-dev xsltproc fop g++ build-essential
2.下载源码包
wget https://packages.erlang-solutions.com/erlang/esl-erlang/FLAVOUR_1_general/esl-erlang_19.1-2~ubuntu~precise_i386.deb
3.安装
sudo dpkg -i esl-erlang_19.1-2~ubuntu~precise_i386.deb
4.验证安装成功
erl
输出如下信息:
Erlang /OTP19 (erts-8.1) [source-77fb4f8] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V8.1 (abort with ^G)
1>
二. 安装rabbitmq-server
1.下载安装包
wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.4.3/rabbitmq-server_3.4.3-1_all.deb
2.安装
sudo dpkg -i rabbitmq-server_3.4.3-1_all.deb
3.重启rabbitmq-server服务
sudo /etc/init.d/rabbitmq-server restart
4.查看rabbitmq服务
ps -ef |grep rabbitmq
5.rabbitmq web管理页面插件安装
rabbitmq-plugins enable rabbitmq_management
安装完成后即可通过http://localhost:15672/ 进行访问。
三. 安装setuptools
1.下载安装包
wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz
2.解压
tar -zxvf setuptools-0.6c11.tar.gz
3.进入setuptools-0.6c11源目录
cd setuptools-0.6c11
4.编译
python setup.py build
5.安装
python setup.py install
四. 安装AMQP python客户端pika
1.下载安装包
wget https://pypi.python.org/packages/source/p/pika/pika-0.9.14.tar.gz#md5=b99aad4b88961d3c7e4876b8327fc97c
2.解压
tar zxvf pika-0.9.14.tar.gz
3.安装
python setup.py install
五. 测试实例
采用最简单的情况作为实例,发送消息,接收并打印出来即可。如下图所示:
send.py
#!/usr/bin/env python
import pika
# 1. Establish a connection with RabbitMQ server.
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost'))
channel = connection.channel()
# 2. Create a queue to which the message will be delivered, let's name it 'hello'
channel.queue_declare(queue='hello')
# 3. Use a default exchange identified by an empty string, which allows us to specify
# exactly to which queue the message should go. The queue name needs to be specified
# in the routing_key parameter:
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print " [x] Sent 'Hello World!'"
# 4. Close the connection
connection.close()
recv.py
#!/usr/bin/env python
import pika
# 1. Establish a connection with RabbitMQ server
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
# 2. Make sure that the queue exists,run the command as many times as we like, and only one will be created.
channel.queue_declare(queue='hello')
print ' [*] Waiting for messages. To exit press CTRL+C'
# 3. Define a callback function.Whenever we receive a message,
# this callback function is called by the Pika library.
def callback(ch, method, properties, body):
print " [x] Received %r" % (body,)
# 4. Subscribe the callback function to a queue.
# Tell RabbitMQ that this particular callback function should receive messages from our hello queue.
channel.basic_consume(callback,
queue='hello',
no_ack=True)
# 5. Enter a never-ending loop that waits for data and runs callbacks whenever necessary.
channel.start_consuming()
测试结果
python send.py
——>[x] Sent 'Hello World!'
python recv.py
——>[*] Waiting for messages. To exit press CTRL+C
[x] Received 'Hello World!'
[x] Done