一.环境准备
因为rabbitMQ是基于erlang语言开发,因此需要erlang的环境。
二.下载
wget http://erlang.org/download/otp_src_18.3.tar.gztar zxvf otp_src_18.3.tar.gz
三.安装
./configure --prefix=/home/jerrylou/erlangmake && make install
四.测试erlang
进入/home/jerrylou/erlang,启动erl测试erlang是否安装成功。
五.配置erlang环境变量
修改/etc/profile文件,增加下面的环境变量:(vim profile i插入 编辑完毕ESC退出 wq!强制修改)#set erlang environmentexport PATH=$PATH:/usr/erlang/bin:$PATHsource profile使得文件生效
六.rabbitMq安装配置
wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.5/rabbitmq-server-generic-unix-3.6.5.tar.xzxz -d rabbitmq-server-generic-unix-3.6.5.tar.xztar xvf rabbitmq-server-generic-unix-3.6.5.tar
解压放入usr下
修改/etc/profile,添加环境变量#set rabbitmq environmentexport PATH=$PATH://home/jerrylou/Downloads/rabbitmq_server-3.6.5/sbinsource profile使得文件生效
七.python测试例子
由于使用了pika库连接操作rabbitmq,因此需要安装pika库。 发送端send.py
#!/usr/bin/env pythonimport timeimport pikacredentials = 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" % bodyconnection.close()
接收端receice.py
#!/usr/bin/env pythonimport pikacredentials = 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()