高併發rabbitmq隊列的安裝使用

準備工作
一臺服務器:Ubuntu Server 16.04.1 LTS 64位

安裝RabbitMq
可以參照RabbitMq官網的安裝教程(Ihttps://www.rabbitmq.com/install-debian.html),來進行安裝。
這裏我們使用apt-get來安裝,就簡單的幾條命令:

1.由於rabbitMq需要erlang語言的支持,在安裝rabbitMq之前需要安裝erlang,執行命令:

apt-get install erlang-nox     # 安裝erlang


erl    # 查看relang語言版本,成功執行則說明relang安裝成功

2.添加公鑰

wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | sudo apt-key add -

3.更新軟件包

apt-get update

4.安裝 RabbitMQ

apt-get install rabbitmq-server  #安裝成功自動啓動

5.查看 RabbitMq狀態

systemctl status rabbitmq-server   #Active: active (running) 說明處於運行狀態

# service rabbitmq-server status 用service指令也可以查看,同systemctl指令

6.啓動、停止、重啓

service rabbitmq-server start    # 啓動
service rabbitmq-server stop     # 停止
service rabbitmq-server restart  # 重啓 

執行了上面的步驟,rabbitMq已經安裝成功。

7.啓用 web端可視化操作界面,我們還需要配置Management Plugin插件

rabbitmq-plugins enable rabbitmq_management   # 啓用插件
service rabbitmq-server restart    # 重啓

此時,應該可以通過 http://localhost:15672 查看,使用默認賬戶guest/guest 登錄。
注意:RabbitMQ 3.3 及後續版本,guest 只能在服務本機登錄。
瞄了一眼官方文檔,說的是默認會創建guest用戶,但是隻能服務器本機登錄,建議創建其他新用戶,授權,用來做其他操作。


8.查看用戶

rabbitmqctl list_users

9.添加管理用戶

rabbitmqctl add_user admin yourpassword   # 增加普通用戶
rabbitmqctl set_user_tags admin administrator    # 給普通用戶分配管理員角色 
ok,你可以在你的瀏覽器上輸入:http://服務器Ip:15672/ 來訪問你的rabbitmq監控頁面。使用剛剛添加的新用戶登錄。

 

接下來你就可以配置 Virtual host ,exchange,queue了

PHP 操作rabbitmq類RabbitMQCommand.php

<?php
/*
 * amqp協議操作類,可以訪問rabbitMQ
 * 需先安裝php_amqp擴展
 */
class RabbitMQCommand{

    public $configs = array();
    //交換機名稱
    public $exchange_name = '';
    //隊列名稱
    public $queue_name = '';
    //路由名稱
    public $route_key = '';
    /*
     * 持久化,默認True
     */
    public $durable = True;
    /*
     * 自動刪除
     * exchange is deleted when all queues have finished using it
     * queue is deleted when last consumer unsubscribes
     * 
     */
    public $autodelete = False;
    /*
     * 鏡像
     * 鏡像隊列,打開後消息會在節點之間複製,有master和slave的概念
     */
    public $mirror = False;
    
    private $_conn = Null;
    private $_exchange = Null;
    private $_channel = Null;
    private $_queue = Null;

    /*
     * @configs array('host'=>$host,'port'=>5672,'username'=>$username,'password'=>$password,'vhost'=>'/')
     */

    public function __construct($configs = array(), $exchange_name = '', $queue_name = '', $route_key = '') {
        $this->setConfigs($configs);
        $this->exchange_name = $exchange_name;
        $this->queue_name = $queue_name;
        $this->route_key = $route_key;
    }
    
    private function setConfigs($configs) {
        if (!is_array($configs)) {
            throw new Exception('configs is not array');
        }
        if (!($configs['host'] && $configs['port'] && $configs['username'] && $configs['password'])) {
            throw new Exception('configs is empty');
        }
        if (empty($configs['vhost'])) {
            $configs['vhost'] = '/';
        }
        $configs['login'] = $configs['username'];
        unset($configs['username']);
        $this->configs = $configs;
    }

    /*
     * 設置是否持久化,默認爲True
     */

    public function setDurable($durable) {
        $this->durable = $durable;
    }

    /*
     * 設置是否自動刪除
     */

    public function setAutoDelete($autodelete) {
        $this->autodelete = $autodelete;
    }
    /*
     * 設置是否鏡像
     */
    public function setMirror($mirror) {
        $this->mirror = $mirror;
    }

    /*
     * 打開amqp連接
     */

    private function open() {
        if (!$this->_conn) {
            try {
                $this->_conn = new AMQPConnection($this->configs);
                
                $this->_conn->connect();
                
                $this->initConnection();
            } catch (AMQPConnectionException $ex) {
                
                throw new Exception('cannot connection rabbitmq',500);
            }
        }
    }

    /*
     * rabbitmq連接不變
     * 重置交換機,隊列,路由等配置
     */

    public function reset($exchange_name, $queue_name, $route_key) {
        $this->exchange_name = $exchange_name;
        $this->queue_name = $queue_name;
        $this->route_key = $route_key;
        $this->initConnection();
    }

    /*
     * 初始化rabbit連接的相關配置
     */

    private function initConnection() {
        if (empty($this->exchange_name) || empty($this->queue_name) || empty($this->route_key)) {
            throw new Exception('rabbitmq exchange_name or queue_name or route_key is empty',500);
        }
        $this->_channel = new AMQPChannel($this->_conn);
        $this->_exchange = new AMQPExchange($this->_channel);
        $this->_exchange->setName($this->exchange_name);

        $this->_exchange->setType(AMQP_EX_TYPE_DIRECT);
        if ($this->durable)
            $this->_exchange->setFlags(AMQP_DURABLE);
        if ($this->autodelete)
            $this->_exchange->setFlags(AMQP_AUTODELETE);
        $this->_exchange->declare();

        $this->_queue = new AMQPQueue($this->_channel);
        $this->_queue->setName($this->queue_name);
        if ($this->durable)
            $this->_queue->setFlags(AMQP_DURABLE);
        if ($this->autodelete)
            $this->_queue->setFlags(AMQP_AUTODELETE);
        if ($this->mirror) 
            $this->_queue->setArgument('x-ha-policy', 'all');
        $this->_queue->declare();
        
        $this->_queue->bind($this->exchange_name, $this->route_key);
    }

    public function close() {
        if ($this->_conn) {
            $this->_conn->disconnect();
        }
    }
    
    public function __sleep() {
        $this->close();
        return array_keys(get_object_vars($this));
    }

    public function __destruct() {
        $this->close();
    }
    
    /*
     * 生產者發送消息
     */
    public function send($msg) {
        $this->open();
        if(is_array($msg)){
            $msg = json_encode($msg);
        }else{
            $msg = trim(strval($msg));
        }
        return $this->_exchange->publish($msg, $this->route_key);
    }
    /*
     * 消費者
     * $fun_name = array($classobj,$function) or function name string
     * $autoack 是否自動應答
     * 
     * function processMessage($envelope, $queue) {
            $msg = $envelope->getBody(); 
            echo $msg."\n"; //處理消息
            $queue->ack($envelope->getDeliveryTag());//手動應答
        }
     */
    public function run($fun_name, $autoack = True){
        $this->open();
        if (!$fun_name || !$this->_queue) return False;  
        while(True){
            if ($autoack) $this->_queue->consume($fun_name, AMQP_AUTOACK);   
            else $this->_queue->consume($fun_name);    
        }
    }

}

php 生產端 send.php

set_time_limit(0);
include_once('RabbitMQCommand.php');
 
$configs = array('host'=>'192.168.206.128','port'=>5672,'username'=>'mytest','password'=>'mytest','vhost'=>'/');
$exchange_name = 'class-e-1';
$queue_name = 'class-q-1';
$route_key = 'class-r-1';
$ra = new RabbitMQCommand($configs,$exchange_name,$queue_name,$route_key);
$ra->durable = true;
for($i=0;$i<=10;$i++){
    $ra->send(json_encode(['date'=>date('Y-m-d H:i:s',time()),'type'=>1,'name'=>rand(1,233).'nice']));
}
exit();

php消費類consumer.php

<?php
error_reporting(0);
include_once('RabbitMQCommand.php');

$configs = array('host'=>'192.168.206.128','port'=>5672,'username'=>'mytest','password'=>'mytest','vhost'=>'/');
$exchange_name = 'class-e-1';
$queue_name = 'class-q-1';
$route_key = 'class-r-1';
$ra = new RabbitMQCommand($configs,$exchange_name,$queue_name,$route_key);

class A{
    function processMessage($envelope, $queue) {
        $msg = $envelope->getBody();
        //處理網站業務
        $envelopeID = $envelope->getDeliveryTag();
        
        //$queue->ack($envelopeID);
        
        $pid = posix_getpid();
       
        file_put_contents("/app/bossadmin/log{$pid}.log", $msg.'|'.$envelopeID.''."\r\n",FILE_APPEND);
        echo 56;
        $queue->ack($envelopeID);
    }
}
$a = new A();

$s = $ra->run(array($a,'processMessage'),false);

接下來就按照網站的業務來處理消費隊列了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章