rabbitmq消息確認php實現詳細例子(從php客戶端安裝支持到實現)

安裝rabbitmq客戶端

  1. 安裝rabbitmq-c庫
    yum install librabbitmq -y
    
  2. 安裝rabbitmq-c
    wget https://github.com/alanxz/rabbitmq-c/archive/v0.10.0.tar.gz
    
  3. 解壓編譯安裝
    tar -zxvf v0.10.0.tar.gz && cd rabbitmq-c-0.10.0 && mkdir build && cd build && cmake -DCMAKE_INSTALL_PREFIX=/usr/local/rabbitmq-c .. && cmake --build .  --target install
    

安裝php的amqp擴展

  1. 安裝擴展支持
    yum install librabbitmq-devel && wget http://pecl.php.net/get/amqp-1.10.2.tgz && tar -zxvf amqp-1.10.2.tgz && cd amqp-1.10.2 && phpize && ./configure --with-php-config=/usr/local/php/bin/php-config --with-amqp --with-librabbitmq-dir=/usr/local/rabbitmq-c && make && make install
    
  2. 添加擴展支持
    編輯php.ini添加:
    extension=amqp.so
    
  3. 重啓php

發送消息(send.php)

<?php
//創建連接和channel
$amqpConnection = new AMQPConnection([
	'host' => '127.0.0.1',
	'port' => '5672',
	'login' => 'guest',
	'password' => 'guest',
	'vhost' => '/'
]);
//交換機名
$exchange_name = 'test_exchange_name';
//隊列名
$queue_name = 'test_queue_name';
//路由key
$route_key = 'test_route_key';
//創建連接和channel
if (!$amqpConnection->connect()) {
    die("Cannot connect to the broker!\n");
}
$amqpChannel = new AMQPChannel($amqpConnection);
//創建交換機
$amqpExchange = new AMQPExchange($amqpChannel);
$amqpExchange->setName($exchange_name);
$amqpExchange->setType(AMQP_EX_TYPE_DIRECT); //direct類型
$amqpExchange->setFlags(AMQP_DURABLE); //持久化
echo "Exchange Status:" . $amqpExchange->declareExchange() . PHP_EOL;
echo "Send Message:" . $amqpExchange->publish(json_encode(['user_id' => 1,'time' => time()]),$route_key). PHP_EOL;

接收消息(receive.php)

<?php
//創建 RabbitMQ 連接
$amqpConnection = new AMQPConnection([
	'host' => '127.0.0.1',
    'port' => '5672',
    'login' => 'guest',
    'password' => 'guest',
    'vhost' => '/'
]);
//交換機名
$exchange_name = 'test_exchange_name'; 
//隊列名
$queue_name = 'test_queue_name'; 
//路由key
$route_key = 'test_route_key'; 
//創建連接和channel
if (!$amqpConnection->connect()) {
    die("Cannot connect to the broker!\n");
}
$amqpChannel= new AMQPChannel($amqpConnection);
//創建交換機
$amqpExchange= new AMQPExchange($amqpChannel);
$amqpExchange->setName($exchange_name);
$amqpExchange->setType(AMQP_EX_TYPE_DIRECT); //direct類型
$amqpExchange->setFlags(AMQP_DURABLE); //持久化
echo "Exchange Status:" . $amqpExchange->declareExchange() . "\n";
//創建隊列
$amqpQueue= new AMQPQueue($amqpChannel);
$amqpQueue->setName($queue_name);
$amqpQueue->setFlags(AMQP_DURABLE); //持久化
echo "Message Total:" . $amqpQueue->declareQueue() . "\n";
//綁定交換機與隊列,並指定路由鍵
echo 'Queue Bind: ' . $amqpQueue->bind($exchange_name, $route_key) . "\n";
//阻塞模式接收消息
echo "Message:\n";
while (true) {
    $amqpQueue->consume(function ($envelope, $queue) {
        /**
         * @var AMQPEnvelope $envelope
         * @var AMQPQueue $queue
         */
        $msg = $envelope->getBody();
        echo $msg . "\n"; //處理消息
        $queue->ack($envelope->getDeliveryTag()); //手動ACK應答確認
    });
//        自動ACK應答確認
//        $amqpQueue->consume(function ($envelope, $queue) {
//            /**
//             * @var AMQPEnvelope $envelope
//             * @var AMQPQueue $queue
//             */
//            $msg = $envelope->getBody();
//            echo $msg . "\n"; //處理消息
//        },AMQP_AUTOACK);
}
$amqpConnection->disconnect();

測試

  1. 通過命令行執行:
    php receive.php
    
  2. 通過瀏覽器訪問:

    http://localhost/send.php

以上信息是本人翻閱了好多例子後實踐整理的結果,本地已經可以跑通,各位看官和有需要的朋友如果覺得好請給個贊啊!

相關資料整理

從rabbitmq服務端安裝到使用,包含rabbitmq management安裝
redis發佈訂閱模式下實現消息隊列和rabbitmq的對比

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