一.环境准备python
由于rabbitMQ是基于erlang语言开发,所以须要erlang的环境。
二.下载vim
wget http://erlang.org/download/otp_src_18.3.tar.gz tar zxvf otp_src_18.3.tar.gz
三.安装测试
./configure --prefix=/home/jerrylou/erlang make && make install
四.测试erlangunix
进入/home/jerrylou/erlang,启动erl测试erlang是否安装成功。
五.配置erlang环境变量code
修改/etc/profile文件,增长下面的环境变量:(vim profile i插入 编辑完毕ESC退出 wq!强制修改) #set erlang environment export PATH=$PATH:/usr/erlang/bin:$PATH source profile使得文件生效
六.rabbitMq安装配置server
wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.5/rabbitmq-server-generic-unix-3.6.5.tar.xz xz -d rabbitmq-server-generic-unix-3.6.5.tar.xz tar xvf rabbitmq-server-generic-unix-3.6.5.tar
解压放入usr下rabbitmq
修改/etc/profile,添加环境变量 #set rabbitmq environment export PATH=$PATH://home/jerrylou/Downloads/rabbitmq_server-3.6.5/sbin source profile使得文件生效
七.python测试例子开发
因为使用了pika库链接操做rabbitmq,所以须要安装pika库。 发送端send.pyget
#!/usr/bin/env python import time import pika credentials = pika.PlainCredentials('guest', 'guest') parameters = pika.ConnectionParameters('localhost', 5672, '/', credentials) connection = pika.BlockingConnection(parameters) channel = connection.channel() channel.queue_declare(queue='hello') for num in range(0, 1000): body = 'hello world:%s' % num channel.basic_publish(exchange='', routing_key='hello', body=body) time.sleep(0.01) print " [x] Sent %s" % body connection.close()
接收端receice.pyit
#!/usr/bin/env python import pika credentials = pika.PlainCredentials('guest', 'guest') parameters = pika.ConnectionParameters('localhost', 5672, '/', credentials) connection = pika.BlockingConnection(parameters) 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()