一.環境準備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()