php使用RabbitMQ


RabbitMQ簡要概括

1、AMQP:Advanced Message Queuing Protocol,是一個提供統一消息服務的應用層標准協議。

2、IPC(單一系統進程間通信) -> socket(不同機器間進程通信) -> AMQP(解決大型系統模塊與組件間通信)

3、RabbitMQ 基於 Erlang 開發,是 AMQP 的一個開源實現。

4、RabbitMQ 系統架構圖:

 

 

5、名詞術語:

  • RabbitMQ Server(broker server):維護一條從 Producer 到 Consumer 的路線,保證數據能夠按照指定的方式進行傳輸;
  • Client A & B:數據發送方,Producers create messages and publish (send) them to a broker server (RabbitMQ),一個有效的 Message 包含 payload 和 label 兩部分
  • Client 1、2、3:數據消費方,Consumers attach to a broker server (RabbitMQ) and subscribe to a queue
  • Exchange:Exchanges are where producers publish their messages
  • Queue: Queues are where the messages end up and are received by consumers
  • Binding:Bindings are how the messages get routed from the exchange to particular queues

還有幾個隱式的概念:

  • Connection:Producer 和 Consumer 通過 TCP 連接到 RabbitMQ
  • Channel:它建立在上述的 TCP 連接中,數據流動都是在 Channel 中進行的

此外,Exchanges 分三種類型:

  • direct:如果 routing key 匹配,那么 Message 就會被傳遞到相應的 queue
  • fanout:會向響應的 queue 廣播
  • topic:對 key 進行模式匹配,比如 ab* 可以傳遞到所有 ab* 的 queue

 

RabbitMQ安裝使用

第一步:下載並安裝erlang

原因:RabbitMQ服務端代碼是使用並發式語言Erlang編寫的,安裝Rabbit MQ的前提是安裝Erlang

下載地址:http://www.erlang.org/downloads

 

 

根據本機位數選擇erlang下載版本。

下載完是這么個東西:

 

 

 

雙擊,點next就可以。

 

 

 

選擇一個自己想保存的地方,然后nextfinish就可以。

 

 

 

安裝完事兒后要記得配置一下系統的環境變量。

此電腦-->鼠標右鍵屬性”-->高級系統設置-->環境變量-->“新建系統環境變量

 

 

 

變量名:ERLANG_HOME

變量值就是剛才erlang的安裝地址,點擊確定。

然后雙擊系統變量path

 

 

點擊新建,將%ERLANG_HOME%\bin加入到path中。

 

 

 

最后windows+R鍵,輸入cmd,再輸入erl,看到版本號就說明erlang安裝成功了。

 

 

 

第二步:下載並安裝RabbitMQ

下載地址:http://www.rabbitmq.com/download.html

 

 

 

雙擊下載后的.exe文件,安裝過程與erlang的安裝過程相同。

RabbitMQ安裝好后接下來安裝RabbitMQ-Plugins。打開命令行cd,輸入RabbitMQsbin目錄。

我的目錄是:D:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.3\sbin

然后在后面輸入rabbitmq-plugins enable rabbitmq_management命令進行安裝

 

 

 

 

打開sbin目錄,雙擊rabbitmq-server.bat

 

 

 等幾秒鍾看到這個界面后,訪問http://localhost:15672

然后可以看到如下界面

 

 

默認用戶名和密碼都是guest

登陸即可。

安裝php擴展amqp

先查看自己的php版本

 

 

記住版本  至於這個線程安全問題 這里引用了別人的自己看看吧  http://blog.csdn.net/aoyoo111/article/details/19021295

接下來下載dll文件 地址http://pecl.php.net/package/amqp

 

下載穩定版的,點擊DLL

 

 

php版本 ,X86 和X64 根據自己情況  , NTS  和 TS 就是那個thread safty 的狀態 這個大家都會看吧 就不多說了

下載解壓

 

 

php_amqp.dll文件放到php目錄的ext文件夾下  見下圖:

 

 

 rabbitmq.4.dll文件放到php根目錄 見下圖:

 

 

 php.ini里面添加    

 

 

apache 修改http.conf 文件 添加

 

 

之后重啟apache  

 

 

php 中使用Rabbitmq實現實現消息發送和接收

1,建立一個send.php文件用來發送消息

2,建立一個 receive.php 文件用來接收消息

代碼如下

send.php

<?php
/**
* 發送消息
 */
$exchangeName = 'demo';
$routeKey = 'hello';
$message = 'Hello World!';
// 建立TCP連接
$connection = new AMQPConnection([
    'host' => 'localhost',
    'port' => '5672',
    'vhost' => '/',
    'login' => 'guest',
'password' => 'guest'
]);
$connection->connect() or die("Cannot connect to the broker!\n");
try {
    $channel = new AMQPChannel($connection);

    $exchange = new AMQPExchange($channel);
    $exchange->setName($exchangeName);
    $exchange->setType(AMQP_EX_TYPE_DIRECT);
    $exchange->declareExchange();

    echo 'Send Message: ' . $exchange->publish($message, $routeKey) . "\n";
    echo "Message Is Sent: " . $message . "\n";
} catch (AMQPConnectionException $e) {
    var_dump($e);
}
$connection->disconnect();// 斷開連接

receive.php

<?php
/**
 * 接收消息
 */
$exchangeName = 'demo';
$queueName = 'hello';
$routeKey = 'hello';
// 建立TCP連接
$connection = new AMQPConnection([
    'host' => 'localhost',
    'port' => '5672',
    'vhost' => '/',
    'login' => 'guest',
    'password' => 'guest'
]);
$connection->connect() or die("Cannot connect to the broker!\n");
$channel = new AMQPChannel($connection);
$exchange = new AMQPExchange($channel);
$exchange->setName($exchangeName);
$exchange->setType(AMQP_EX_TYPE_DIRECT);
echo 'Exchange Status: ' . $exchange->declareExchange() . "\n";
$queue = new AMQPQueue($channel);
$queue->setName($queueName);
echo 'Message Total: ' . $queue->declareQueue() . "\n";
echo 'Queue Bind: ' . $queue->bind($exchangeName, $routeKey) . "\n";
var_dump("Waiting for message...");
// 消費隊列消息
while(TRUE) {
    $queue->consume('processMessage');
}

// 斷開連接
$connection->disconnect();
function processMessage($envelope, $queue) {
    $msg = $envelope->getBody();
    var_dump("Received: " . $msg);
    $queue->ack($envelope->getDeliveryTag()); // 手動發送ACK應答
}

測試:

打開兩個終端,先運行接收者腳本監聽消息發送:

php receive.php

在另一個終端中運行消息發送腳本:

php send.php

 

 

 


免責聲明!

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



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