ubuntu 12.04安裝配置rabbitmq

ubuntu 12.04安裝配置rabbitmq

在安裝rabbitmq以前,須要先安裝一些依賴關係,好比erlang,下面介紹安裝方法:python

一. 安裝erlang

1.安裝依賴關係包web

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.下載源碼包shell

wget https://packages.erlang-solutions.com/erlang/esl-erlang/FLAVOUR_1_general/esl-erlang_19.1-2~ubuntu~precise_i386.deb

3.安裝ubuntu

sudo dpkg -i esl-erlang_19.1-2~ubuntu~precise_i386.deb

4.驗證安裝成功async

erl

  輸出以下信息:ide

Erlang /OTP19 (erts-8.1) [source-77fb4f8] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V8.1 (abort with ^G)

1>

二. 安裝rabbitmq-server

1.下載安裝包oop

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服務ui

sudo /etc/init.d/rabbitmq-server restart

4.查看rabbitmq服務this

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
相關文章
相關標籤/搜索