1. 程式人生 > >rabbitmq原始碼安裝和配置

rabbitmq原始碼安裝和配置

1. 安裝rabbitmq 之前首先

(1)安裝erlang.:

下載erlang:

  1. 我的是Mac OSX系統 所以我直接在 http://www.erlang-solutions.com/section/132/download-erlang-otp下載的對應版本的安裝包,省的自己配置和安裝了
  2. 然後解壓下載的gz包 tar zxcf *.tar.gz
  3. cd 進入解壓出來的資料夾
  4. 執行./configure --prefix=/opt/erlang 就會開始編譯安裝 會編譯到 /opt/erlang 下 然後執行
  5. make 和 make install
  6. 編譯完成以後,進入/opt/erlang,輸入erl測試erlang是否安裝成功。
  7. 修改/etc/profile檔案,增加下面的環境變數:
  8. #set erlang environment
  9. export PATH=$PATH:/opt/erlang/bin
  10. source profile使得檔案生效
或者:

yum install erlang

(2).安裝python

yum install python -y

(3) 安裝simplejson

yum -y install xmlto yum -y install python-simplejson (4)安裝rabbitmq
1.下載:
 # wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.4.2/rabbitmq-server-generic-unix-3.4.2.tar.gz
2.解壓縮:
 # tar -zxf rabbitmq-server-generic-unix-3.4.2.tar.gz

3.移動這個目錄到 /usr/local下並且重新命名為rabbitmq:
 # mv rabbitmq-server-generic-unix-3.4.2 /usr/local/rabbitmq
4.開啟/etc/profile檔案,在檔案最後添如下兩行環境變數
   #set rabbitmq environment
   export PATH=$PATH:/usr/local/rabbitmq/sbin
5.使環境變數生效:
 # source /etc/profile
6.安裝rabbitmq網頁管理外掛:
 # cd /usr/local/rabbitmq/sbin/
 #./rabbitmq-plugin enable rabbitmq-management
7.啟動rabbitmq:
 # cd /usr/local/rabbitmq/sbin
 # ./rabbitmq-server -detached (可以實現後臺執行)
8.檢視啟動是否成功: # netstat -tunlp | grep beam
 tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 3308/beam.smp
 tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 3308/beam.smp
 tcp 0 0 :::5672 :::* LISTEN 3308/beam.smp
可以看到啟動成功: 15672是rabbimq網頁管理監聽埠,5672是php客戶端使用的埠,在瀏覽器中輸入localhost:15672,可以看到如下頁面:

輸入使用者名稱guest和密碼guest即可通過網頁管理rabbitmq。
 9. 關閉rabbitmq : # cd /usr/local/rabbitmq/sbin 然後:#./rabbitmqctl stop

(5)php客戶端使用rabbitmq

composer是PHP用來管理依賴(dependency)關係的工具。你可以在自己的專案中宣告所依賴的外部工具庫(libraries),Composer 會幫你安裝這些依賴的庫檔案,具體可參考composer權威網站:http://www.phpcomposer.com/

1)下載 Composer 的可執行檔案# curl -sS https://getcomposer.org/installer | php# chmod u+x composer.phar# mv composer.phar /usr/local/bin/composer2)宣告php依賴關係 在php專案下建立 composer.json 檔案,描述了專案的依賴關係。

{
"reqire": {
"videlalvaro/php-amqplib": "v2.1.0"
}
}

3.使用 Composer

composer.json檔案相同的目錄下執行如下程式碼:

# composer install

4.時候在composer.json相同的目錄下生成vendor目錄,這目錄中是composer.json檔案指定的php依賴庫下載後的放置位置。
# ls
amqp                 index.php           receive_logs.php         vendor
Application          new_task.php        receive_logs_topic.php   worker.php
composer.json        phpinfo.php         send.php
emit_log_direct.php  Public              test.php
emit_log.php         README.md           ThinkPHP

-----------------------------------------------------------------------------------------------------------------------
以上就搭建好了php和rabbitmq的使用環境,下面是怎麼使用php作為客戶端和rabbitmq通訊。

先讀這篇文章http://blog.csdn.net/sun305355024sun/article/details/41913105
再讀這幾篇文章:http://blog.csdn.net/wind_324/article/category/2165209


在官網介紹hello world這篇文章時候send.php和receive.php程式碼可能有錯誤,這時候,可以從剛才根據composer.json檔案生成的依賴庫vendor/videlalvaro/php-amqplib/demo目錄下找到amqp_publisher.php和amqp_consumer.php這兩個demo檔案,複製到/usr/local/apache/htdocs目錄下不要使用官網上的send.php和receive.php檔案。
開啟 amqp_publisher.php檔案進行修改,修改後的程式碼如下:
<?php
include(__DIR__ . '/vendor/autoload.php');
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;
define('HOST', '127.0.0.1');
define('PORT', 5672);
define('USER', 'guest');
define('PASS', 'guest');
define('VHOST', '/');
$exchange = 'router';
$queue = 'msgs';
$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$ch = $conn->channel();
/*
    The following code is the same both in the consumer and the producer.
    In this way we are sure we always have a queue to consume from and an
        exchange where to publish messages.
*/
/*
    name: $queue
    passive: false
    durable: true // the queue will survive server restarts
    exclusive: false // the queue can be accessed in other channels
    auto_delete: false //the queue won't be deleted once the channel is closed.
*/
$ch->queue_declare($queue, false, true, false, false);
/*
    name: $exchange
    type: direct
    passive: false
    durable: true // the exchange will survive server restarts
    auto_delete: false //the exchange won't be deleted once the channel is closed.
*/
$ch->exchange_declare($exchange, 'direct', false, true, false);
$ch->queue_bind($queue, $exchange);
$msg_body = implode(' ', array_slice($argv, 1));
$msg = new AMQPMessage($msg_body, array('content_type' => 'text/plain', 'delivery_mode' => 2));
$ch->basic_publish($msg, $exchange);
$ch->close();
$conn->close();

amqp_consumer.php進行修改後的程式碼:
<?php
include(__DIR__ . '/vendor/autoload.php');
use PhpAmqpLib\Connection\AMQPConnection;
define('HOST', '127.0.0.1');
define('PORT', 5672);
define('USER', 'guest');
define('PASS', 'guest');
define('VHOST', '/');
$exchange = 'router';
$queue = 'msgs';
$consumer_tag = 'consumer';
$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$ch = $conn->channel();
/*
    The following code is the same both in the consumer and the producer.
    In this way we are sure we always have a queue to consume from and an
        exchange where to publish messages.
*/
/*
    name: $queue
    passive: false
    durable: true // the queue will survive server restarts
    exclusive: false // the queue can be accessed in other channels
    auto_delete: false //the queue won't be deleted once the channel is closed.
*/
$ch->queue_declare($queue, false, true, false, false);

/*
    name: $exchange
    type: direct
    passive: false
    durable: true // the exchange will survive server restarts
    auto_delete: false //the exchange won't be deleted once the channel is closed.
*/
$ch->exchange_declare($exchange, 'direct', false, true, false);
$ch->queue_bind($queue, $exchange);
function process_message($msg)
{
    echo "\n--------\n";
    echo $msg->body;
    echo "\n--------\n";

<pre name="code" class="php">    $msg->delivery_info['channel']->
        basic_ack($msg->delivery_info['delivery_tag']);

    // Send a message with the string "quit" to cancel the consumer.
    if ($msg->body === 'quit') {
        $msg->delivery_info['channel']->
            basic_cancel($msg->delivery_info['consumer_tag']);
    }
}

/*
    queue: Queue from where to get the messages
    consumer_tag: Consumer identifier
    no_local: Don't receive messages published by this consumer.
    no_ack: Tells the server if the consumer will acknowledge the messages.
    exclusive: Request exclusive consumer access, meaning only this consumer can access the queue
    nowait:
    callback: A PHP Callback
*/

$ch->basic_consume($queue, $consumer_tag, false, false, false, false, 'process_message');

function shutdown($ch, $conn)
{
    $ch->close();
    $conn->close();
}
register_shutdown_function('shutdown', $ch, $conn);

// Loop as long as the channel has callbacks registered

while (count($ch->callbacks)) {
    $ch->wait();
}


 
測試一下:

開啟消費者監聽:
[[email protected] htdocs]# php amqp_consumer.php

釋出者釋出訊息:
[[email protected] htdocs]# php amqp_publisher.php 'lsi';
[[email protected] htdocs]# php amqp_publisher.php 'zhangsan'

可以看到監聽者打印出這兩個訊息
[[email protected] htdocs]# php amqp_consumer.php

--------
lsi
--------

--------
zhangsan
--------

在localhost:15672 網頁管理系統中可以看到msgs的佇列(剛才建立的),並且在messages中看到total2因為我們剛才釋出的兩條訊息,所以在這個佇列中2個訊息。






相關推薦

rabbitmq原始碼安裝配置

1. 安裝rabbitmq 之前首先 (1)安裝erlang.: 下載erlang: 我的是Mac OSX系統 所以我直接在 http://www.erlang-solutions.com/section/132/download-erlang-otp下載的對應版本的安裝包

rabbitMQ安裝配置

連接數 term 運行 gen ++ ln -s strong prot profile erlang是一門面向並發的編程語言,流行的消息隊列rabbitMQ是基於erlang環境運行的; 系統環境 操作系統:oracle-linux7.3 erlang版本:otp_

Linux-CentOS 6.7 RabbitMQ安裝配置

    RabbitMQ訊息中介軟體,主要用於分散式事物傳遞,公司用的Mybatis + Spring-boot + Shiro + RabbitMQ +(nginx,haproxy)+ mysql進行開發。對linux環境不太熟悉,所以記錄一下:一、RabbitMQ安裝  

RabbitMQ第一篇:RabbitMQ安裝配置

在Windows下進行rabbitMQ的安裝 第一步:軟體安裝 如果安裝rabbitMQ首先安裝基於erlang語言支援的OTP軟體,然後在下載rabbitMQ軟體進行安裝(安裝過程都是下一步,在此不在說了) 第二步:環境變數配置 如果上面完成安

CentOS6.7 RabbitMQ安裝配置

我這裡以3.5.6為例 一:RabbitMQ的安裝 首先,下載RabbitMQ wget https://github.com/rabbitmq/rabbitmq-server/releases/download/rabbitmq_v3_5_6/rabbitmq-serv

CentOS6.8中RabbitMQ安裝配置&俺們這些逗比遇到的坑好難填

安裝系統CentOS6.8 1.erlang的安裝erlang的版本:erlang-18.2.1 erlang的依賴環境: (1)首先安裝GCC GCC-C++ Openssl等模組: yum -y install make gcc gcc-c++ kernel-devel

Squid3.1.7原始碼安裝配置筆記

Squid是一個非常優秀的代理伺服器,最近在辦公室自己搭了一個來用,把筆記貼出來方便查詢。(只介紹步驟思路,不做詳細解釋) 1、到squid官方網站http://www.squid-cache.org/ 下載最新版 2、解壓   tar zxvf squid-3

RabbitMQ安裝配置

www download div -s 令行 con web管理 plugins 下載 Window下安裝 安裝 下載並安裝Erlang for windows; 下載並安裝 Python 3.x / 2.7.x / 2.6.x; 下載並安裝 rabbitmq-serve

linux centos6.5 安裝配置rabbitmq

(1) 安裝erlang otp (本環境用otp_17.4) (2)安裝依賴庫 檢視 python 是否安裝,如果沒有則安裝python yum install libxslt-devel yum install python-simplejson -y yum inst

windows安裝配置rabbitmq

2.環境變數配置 設定erlang環境變數: ERLANG_HOME=C:\Program Files\erl9.3 然後新增到PATH: %ERLANG_HOME%\bin; 設定RABBITMQ環境變數: RABBITMQ_SERVER=D:

阿里雲CentOS安裝配置rabbitmq

下載rpm並安裝: rpm安裝官網:http://www.rabbitmq.com/install-rpm.html CentOs: wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.1/rabbitmq

CloudStack+XenServer詳細部署方案 CloudStack管理節點的安裝配置

cloudstack+xenserver詳細部署方案 cloudstack管理節點的安裝和配置CloudStack+XenServer詳細部署方案 CloudStack管理節點的安裝和配置本文將根據設計文檔, 安裝和配置CloudStack管理節點。本文只對配置流程和結果進行舉例說明, 具體 細節和配置操作請

centos7 mysql數據庫安裝配置

web 大小 images etc 安裝mysql 安裝 commands 0 rows type 一、系統環境 yum update升級以後的系統版本為 [[email protected]/* */ yl]# cat /etc/redhat-relea

redis學習教程一《Redis的安裝配置

遠程服務 name 工具 列表 端口號 裏的 redis服務器 映射 tin redis學習教程一《Redis的安裝和配置》 Redis的優點 以下是Redis的一些優點。 異常快 - Redis非常快,每秒可執行大約110000次的設置(SET)操作,每秒大約可執

linux下jdk的安裝配置

rac -s watermark img 使用 etc 文件拷貝 安裝 com 一、首先依據自己的系統位數在網上下載對應的jdk安裝包 下載地址例如以下:http://www.oracle.com/technetwork/java/javase/downloads/jd

linux中安裝配置 jdk

jdk1 png images 下載 配置環境變量 ftp 當前 bsp 官網 01.去官網下載指定的jdk 02.使用xftp把下載好的文件 傳遞到 linux指定文件夾中03.進入指定的文件夾輸入tar -zxvf 文件名稱04.發現文件 05.進入文件c

eclipse maven 插件的安裝配置

打印 屬性 配置文件 size text 官網下載 maven 應該 庫存 maven3 安裝: 安裝 Maven 之前要求先確定你的 JDK 已經安裝配置完畢。Maven是 Apache 下的一個項目。眼下最新版本號是 3.0.4。我用的也是這個。

Microsoft SQL Server for Linux安裝配置

fig fcm zone linux for onf targe 查看 nbsp 客戶端   雖說mssql for linux早已經出來了,但原本沒有打算這麽早就去嘗試的,無奈之下還是得先嘗試用了,這裏分幾篇介紹我在用mssql for linux時遇到的問題,不得不說作

nfs安裝配置

nfs nfs安裝 clas bind 修改配置文件 讀取 word 修改 source nfs安裝和配置 1,安裝軟件: yum -y install nfs-utils 註意,會自動安裝rpcbind 2,啟動nfs service rpcbind start serv

Gulp的安裝配置

分享 插件 gis 鏡像文件 path cal user sta 全局 註意:要安裝倆次gulp(全局和本地) 之前由大牛幫忙配置的gulp來用。今天時間充裕,就和小夥伴一起動手配置gulp及其插件。第一步:建了一個Gulp文件夾,保存插件用於使用第二步:進入nodejs環