【消息隊列】RabbitMQ+PHP實現

本文連接:http://www.cnblogs.com/aiweixiao/p/7374249.htmlphp

文檔提綱:html

 

掃描關注微信公衆號git

 

 

1.【下載和安裝】

  1)gitHub下載地址: https://github.com/php-amqplib/php-amqplibgithub

  2)【環境準備】:微信

         安裝php的RabbitMQ PHP擴展安裝,依賴的Erlang環境composer

   參考文檔: http://www.cnblogs.com/chunguang/p/5634342.htmlide

  3)【類的依賴加載】:建議使用 composer實現,函數

             composer入門
      4)【教程】:
    官方教程連接地址: http://www.rabbitmq.com/tutorials/tutorial-one-php.html

  5)測試腳本:測試

            --使用官方的demo:ui

開一個訂閱者窗口
$ cd php-amqplib/demo
$ php amqp_consumer.php



開一個發佈者窗口
$ cd php-amqplib/demo
$ php amqp_publisher.php some text to publish

 

 

    --使用第三方腳本 http://blog.csdn.net/u014071426/article/details/53649226

 1 //引用所需文件  
 2 require_once __DIR__ . '/vendor/autoload.php';  
 3 use PhpAmqpLib\Connection\AMQPStreamConnection;  
 4 use PhpAmqpLib\Message\AMQPMessage;  
 5 //創建一個鏈接通道,聲明一個能夠發送消息的隊列hello  
 6 $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');  
 7 $channel = $connection->channel();  
 8 $channel->queue_declare('hello', false, false, false, false);  
 9   
10 //定義一個消息,消息內容爲Hello World!  
11 $msg = new AMQPMessage('Hello World!');  
12 $channel->basic_publish($msg, '', 'hello');  
13   
14 //發送完成後打印消息告訴發佈消息的人:發送成功  
15 echo " [x] Sent 'Hello World!'\n";  
16 //關閉鏈接  
17 $channel->close();  
18 $connection->close();
View Code
 1     require_once __DIR__ . '/vendor/autoload.php';  
 2     use PhpAmqpLib\Connection\AMQPStreamConnection;  
 3     $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');  
 4     $channel = $connection->channel();  
 5       
 6     $channel->queue_declare('hello', false, false, false, false);  
 7       
 8     echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";  
 9     $callback = function($msg) {  
10       echo " [x] Received ", $msg->body, "\n";  
11     };  
12       
13     //在接收消息的時候調用$callback函數  
14     $channel->basic_consume('hello', '', false, true, false, false, $callback);  
15       
16     while(count($channel->callbacks)) {  
17         $channel->wait();  
18     }  
相關文章
相關標籤/搜索