rabbitmq 使用PhpAmqpLib


rabbitmq類

rabbitmq.php

<?php


require_once __DIR__ . '/vendor/autoload.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;

use PhpAmqpLib\Message\AMQPMessage;

abstract class rabbitmq
{
    //MQ的默認連接配置
    public $config = array(
        'host' => '192.168.33.50', //ip
        'port' => '5672',      //端口號
        'user' => 'admin',     //用戶
        'password' => 'admin', //密碼
        'vhost' => '/'         //虛擬host
    );
    public $connection;     //鏈接
    public $channel;        //信道

    public $exchangeName = '';     //交換機名
    public $queueName = '';        //隊列名
    public $routeKey = '';         //路由鍵
    public $exchangeType = 'direct';    //交換機類型

    public $autoAck = true; //是否自動ack應答

    public function __construct($exchangeName, $queueName, $routeKey, $exchangeType = 'direct', $config=array())
    {
        $this->exchangeName = empty($exchangeName) ? '' : $exchangeName;
        $this->queueName = empty($queueName) ? '' : $queueName;
        $this->routeKey = empty($routeKey) ? '' : $routeKey;
        $this->exchangeType = empty($exchangeType) ? '' : 'direct';
        if(!empty($config))
        {
            $this->setConfig($config);
        }
        $this->createConnect();
    }

    //創建連接與信道
    private function createConnect()
    {
        $host = $this->config['host'];
        $port = $this->config['port'];
        $user = $this->config['user'];
        $password = $this->config['password'];
        $vhost = $this->config['vhost'];
        if(empty($host) || empty($port) || empty($user) || empty($password))
        {
            throw new Exception('RabbitMQ的連接配置不正確');
        }
        //創建鏈接
        $this->connection = new AMQPStreamConnection($host, $port, $user, $password, $vhost);
        //創建信道
        $this->channel = $this->connection->channel();
        $this->createExchange();
    }

    //創建交換機
    private function createExchange()
    {
        //創建交換機$channel->exchange_declare($exhcange_name,$type,$passive,$durable,$auto_delete);
        //passive: 消極處理, 判斷是否存在隊列,存在則返回,不存在直接拋出 PhpAmqpLib\Exception\AMQPProtocolChannelException 異常
        //durable:true、false true:服務器重啟會保留下來Exchange。警告:僅設置此選項,不代表消息持久化。即不保證重啟后消息還在
        //autoDelete:true、false.true:當已經沒有消費者時,服務器是否可以刪除該Exchange
        $this->channel->exchange_declare($this->exchangeName, $this->exchangeType, false, true, false);
        //passive: 消極處理, 判斷是否存在隊列,存在則返回,不存在直接拋出 PhpAmqpLib\Exception\AMQPProtocolChannelException 異常
        //durable:true、false true:在服務器重啟時,能夠存活
        //exclusive :是否為當前連接的專用隊列,在連接斷開后,會自動刪除該隊列
        //autodelete:當沒有任何消費者使用時,自動刪除該隊列
        //arguments: 自定義規則
        $this->channel->queue_declare($this->queueName, false, true, false, false);
    }

    //發送消息
    public function sendMessage($data)
    {
        //創建消息$msg = new AMQPMessage($data,$properties)
        //#$data  string類型 要發送的消息
        //#roperties array類型 設置的屬性,比如設置該消息持久化[‘delivery_mode’=>2]
        $msg = new AMQPMessage($data, array('delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT));
        $this->channel->basic_publish($msg,$this->exchangeName, $this->routeKey);
    }

    //處理消息
    public function dealMq($flag)
    {
        $this->autoAck = $flag;
        $this->channel->queue_bind($this->queueName,$this->exchangeName, $this->routeKey);
        //prefetchSize:0
        //prefetchCount:會告訴RabbitMQ不要同時給一個消費者推送多於N個消息,即一旦有N個消息還沒有ack,則該consumer將block掉,直到有消息ack
        //global:true\false 是否將上面設置應用於channel,簡單點說,就是上面限制是channel級別的還是consumer級別
        //$this->channel->basic_qos(0, 1, false);
        //1:queue 要取得消息的隊列名
        //2:consumer_tag 消費者標簽
        //3:no_local false這個功能屬於AMQP的標准,但是rabbitMQ並沒有做實現.參考
        //4:no_ack  false收到消息后,是否不需要回復確認即被認為被消費
        //5:exclusive false排他消費者,即這個隊列只能由一個消費者消費.適用於任務不允許進行並發處理的情況下.比如系統對接
        //6:nowait  false不返回執行結果,但是如果排他開啟的話,則必須需要等待結果的,如果兩個一起開就會報錯
        //7:callback  null回調函數
        //8:ticket  null
        //9:arguments null
        $this->channel->basic_consume($this->queueName, '', false, $this->autoAck, false, false, function($msg){$this->get($msg);});
        //監聽消息
        while(count($this->channel->callbacks)){
            $this->channel->wait();
        }
    }

    public function get($msg)
    {
        $param = $msg->body;
        $this->doProcess($param);
        if(!$this->autoAck)
        {
            //手動ack應答
            $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
        }
    }

    abstract public function doProcess($param);

    public function closeConnetct()
    {
        $this->channel->close();
        $this->connection->close();
    }

    //重新設置MQ的鏈接配置
    public function setConfig($config)
    {
        if (!is_array($config)) {
            throw new Exception('config不是一個數組');
        }
        foreach ($config as $key => $value) {
            $this->config[$key] = $value;
        }

    }


}

  

send.php

<?php
include_once('rabbitmq.php');
class Publisher extends rabbitmq
{
    public function __construct()
    {
        parent::__construct('crm', 'crm_test', 'crm_test');
    }
    public function doProcess($msg)
    {

    }

}



$publisher = new Publisher();
for($i=0;$i<100000;$i++){
    $publisher->sendMessage('Hello,World!');
}

$publisher->closeConnetct();

  consumer.php

<?php
include_once('rabbitmq.php');
class Consumer extends rabbitmq
{
    public function __construct()
    {
        parent::__construct('crm', 'crm_test', 'crm_test');
    }
    public function doProcess($msg)
    {
        echo $msg."\n";
    }
}
$consumer = new Consumer();
//$consumer->dealMq(false);
$consumer->dealMq(false);

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM