PHP高級(jí)編程之消息隊(duì)列原理與實(shí)現(xiàn)方法詳解
本文實(shí)例講述了PHP高級(jí)編程之消息隊(duì)列原理與實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
1. 什么是消息隊(duì)列
消息隊(duì)列(英語:Message queue)是一種進(jìn)程間通信或同一進(jìn)程的不同線程間的通信方式
2. 為什么使用消息隊(duì)列
消息隊(duì)列技術(shù)是分布式應(yīng)用間交換信息的一種技術(shù)。消息隊(duì)列可駐留在內(nèi)存或磁盤上,隊(duì)列存儲(chǔ)消息直到它們被應(yīng)用程序讀出。通過消息隊(duì)列,應(yīng)用程序可獨(dú)立地執(zhí)行,它們不需要知道彼此的位置、或在繼續(xù)執(zhí)行前不需要等待接收程序接收此消息。
3. 什么場合使用消息隊(duì)列
你首先需要弄清楚,消息隊(duì)列與遠(yuǎn)程過程調(diào)用的區(qū)別,在很多讀者咨詢我的時(shí)候,我發(fā)現(xiàn)他們需要的是RPC(遠(yuǎn)程過程調(diào)用),而不是消息隊(duì)列。
消息隊(duì)列有同步或異步實(shí)現(xiàn)方式,通常我們采用異步方式使用消息隊(duì)列,遠(yuǎn)程過程調(diào)用多采用同步方式。
MQ與RPC有什么不同? MQ通常傳遞無規(guī)則協(xié)議,這個(gè)協(xié)議由用戶定義并且實(shí)現(xiàn)存儲(chǔ)轉(zhuǎn)發(fā);而RPC通常是專用協(xié)議,調(diào)用過程返回結(jié)果。
4. 什么時(shí)候使用消息隊(duì)列
同步需求,遠(yuǎn)程過程調(diào)用(PRC)更適合你。
異步需求,消息隊(duì)列更適合你。
目前很多消息隊(duì)列軟件同時(shí)支持RPC功能,很多RPC系統(tǒng)也能異步調(diào)用。
消息隊(duì)列用來實(shí)現(xiàn)下列需求
① 存儲(chǔ)轉(zhuǎn)發(fā)
② 分布式事務(wù)
③ 發(fā)布訂閱
④ 基于內(nèi)容的路由
⑤ 點(diǎn)對點(diǎn)連接
5. 誰負(fù)責(zé)處理消息隊(duì)列
通常的做法,如果小的項(xiàng)目團(tuán)隊(duì)可以有一個(gè)人實(shí)現(xiàn),包括消息的推送,接收處理。如果大型團(tuán)隊(duì),通常是定義好消息協(xié)議,然后各自開發(fā)各自的部分,例如一個(gè)團(tuán)隊(duì)負(fù)責(zé)寫推送協(xié)議部分,另一個(gè)團(tuán)隊(duì)負(fù)責(zé)寫接收與處理部分。
那么為什么我們不講消息隊(duì)列框架化呢?
框架化有幾個(gè)好處:
① 開發(fā)者不用學(xué)習(xí)消息隊(duì)列接口
② 開發(fā)者不需要關(guān)心消息推送與接收
③ 開發(fā)者通過統(tǒng)一的API推送消息
④ 開發(fā)者的重點(diǎn)是實(shí)現(xiàn)業(yè)務(wù)邏輯功能
6. 怎么實(shí)現(xiàn)消息隊(duì)列框架
下面是作者開發(fā)的一個(gè)SOA框架,該框架提供了三種接口,分別是SOAP,RESTful,AMQP(RabbitMQ),理解了該框架思想,你很容易進(jìn)一步擴(kuò)展,例如增加XML-RPC, ZeroMQ等等支持。
https://github.com/netkiller/SOA
本文只講消息隊(duì)列框架部分。
6.1. 守護(hù)進(jìn)程
消息隊(duì)列框架是本地應(yīng)用程序(命令行程序),我們?yōu)榱俗屗诤笈_(tái)運(yùn)行,需要實(shí)現(xiàn)守護(hù)進(jìn)程。
https://github.com/netkiller/SOA/blob/master/bin/rabbitmq.php
每個(gè)實(shí)例處理一組隊(duì)列,實(shí)例化需要提供三個(gè)參數(shù),$queueName = '隊(duì)列名', $exchangeName = '交換名', $routeKey = '路由'
$daemon = new \framework\RabbitDaemon($queueName = 'email', $exchangeName = 'email', $routeKey = 'email');
守護(hù)進(jìn)程需要使用root用戶運(yùn)行,運(yùn)行后會(huì)切換到普通用戶,同時(shí)創(chuàng)建進(jìn)程ID文件,以便進(jìn)程停止的時(shí)候使用。
守護(hù)進(jìn)程核心代碼https://github.com/netkiller/SOA/blob/master/system/rabbitdaemon.class.php
6.2. 消息隊(duì)列協(xié)議
消息協(xié)議是一個(gè)數(shù)組,將數(shù)組序列化或者轉(zhuǎn)為JSON推送到消息隊(duì)列服務(wù)器,這里使用json格式的協(xié)議。
$msg = array( 'Namespace'=>'namespace', "Class"=>"Email", "Method"=>"smtp", "Param" => array( $mail, $subject, $message, null ) );
序列化后的協(xié)議
{"Namespace":"single","Class":"Email","Method":"smtp","Param":["netkiller@msn.com","Hello"," TestHelloWorld",null]}
使用json格式是考慮到通用性,這樣推送端可以使用任何語言。如果不考慮兼容,建議使用二進(jìn)制序列化,例如msgpack效率更好。
6.3. 消息隊(duì)列處理
消息隊(duì)列處理核心代碼
https://github.com/netkiller/SOA/blob/master/system/rabbitmq.class.php
所以消息的處理在下面一段代碼中進(jìn)行
$this->queue->consume(function($envelope, $queue) { $speed = microtime(true); $msg = $envelope->getBody(); $result = $this->loader($msg); $queue->ack($envelope->getDeliveryTag()); //手動(dòng)發(fā)送ACK應(yīng)答 //$this->logging->info(''.$msg.' '.$result) $this->logging->debug('Protocol: '.$msg.' '); $this->logging->debug('Result: '. $result.' '); $this->logging->debug('Time: '. (microtime(true) - $speed) .''); });
public function loader($msg = null)
負(fù)責(zé)拆解協(xié)議,然后載入對應(yīng)的類文件,傳遞參數(shù),運(yùn)行方法,反饋結(jié)果。
Time 可以輸出程序運(yùn)行所花費(fèi)的時(shí)間,對于后期優(yōu)化十分有用。
提示
loader() 可以進(jìn)一步優(yōu)化,使用多線程每次調(diào)用loader將任務(wù)提交到線程池中,這樣便可以多線程處理消息隊(duì)列。
6.4. 測試
測試代碼 https://github.com/netkiller/SOA/blob/master/test/queue/email.php
<?php $queueName = 'example'; $exchangeName = 'email'; $routeKey = 'email'; $mail = $argv[1]; $subject = $argv[2]; $message = empty($argv[3]) ? 'Hello World!' : ' '.$argv[3]; $connection = new AMQPConnection(array( 'host' => '192.168.4.1', '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); $queue = new AMQPQueue($channel); $queue->setName($queueName); $queue->setFlags(AMQP_DURABLE); $queue->declareQueue(); $msg = array( 'Namespace'=>'namespace', "Class"=>"Email", "Method"=>"smtp", "Param" => array( $mail, $subject, $message, null ) ); $exchange->publish(json_encode($msg), $routeKey); printf("[x] Sent %s \r\n", json_encode($msg)); $connection->disconnect();
這里只給出了少量測試與演示程序,如有疑問請到瀆者群,或者公眾號(hào)詢問。
7. 多線程
上面消息隊(duì)列 核心代碼如下
$this->queue->consume(function($envelope, $queue) { $msg = $envelope->getBody(); $result = $this->loader($msg); $queue->ack($envelope->getDeliveryTag()); });
這段代碼生產(chǎn)環(huán)境使用了半年,發(fā)現(xiàn)效率比較低。有些業(yè)務(wù)場入隊(duì)非常快,但處理起來所花的時(shí)間就比較長,容易出現(xiàn)隊(duì)列堆積現(xiàn)象。
增加多線程可能更有效利用硬件資源,提高業(yè)務(wù)處理能力。代碼如下
<?php namespace framework; require_once( __DIR__.'/autoload.class.php' ); class RabbitThread extends \Threaded { private $queue; public $classspath; protected $msg; public function __construct($queue, $logging, $msg) { $this->classspath = __DIR__.'/../queue'; $this->msg = $msg; $this->logging = $logging; $this->queue = $queue; } public function run() { $speed = microtime(true); $result = $this->loader($this->msg); $this->logging->debug('Result: '. $result.' '); $this->logging->debug('Time: '. (microtime(true) - $speed) .''); } // private public function loader($msg = null){ $protocol = json_decode($msg,true); $namespace = $protocol['Namespace']; $class = $protocol['Class']; $method = $protocol['Method']; $param = $protocol['Param']; $result = null; $classspath = $this->classspath.'/'.$this->queue.'/'.$namespace.'/'.strtolower($class) . '.class.php'; if( is_file($classspath) ){ require_once($classspath); //$class = ucfirst(substr($request_uri, strrpos($request_uri, '/')+1)); if (class_exists($class)) { if(method_exists($class, $method)){ $obj = new $class; if (!$param){ $tmp = $obj->$method(); $result = json_encode($tmp); $this->logging->info($class.'->'.$method.'()'); }else{ $tmp = call_user_func_array(array($obj, $method), $param); $result = (json_encode($tmp)); $this->logging->info($class.'->'.$method.'("'.implode('","', $param).'")'); } }else{ $this->logging->error('Object '. $class. '->' . $method. ' is not exist.'); } }else{ $msg = sprintf("Object is not exist. (%s)", $class); $this->logging->error($msg); } }else{ $msg = sprintf("Cannot loading interface! (%s)", $classspath); $this->logging->error($msg); } return $result; } } class RabbitMQ { const loop = 10; protected $queue; protected $pool; public function __construct($queueName = '', $exchangeName = '', $routeKey = '') { $this->config = new \framework\Config('rabbitmq.ini'); $this->logfile = __DIR__.'/../log/rabbitmq.%s.log'; $this->logqueue = __DIR__.'/../log/queue.%s.log'; $this->logging = new \framework\log\Logging($this->logfile, $debug=true); //.H:i:s $this->queueName = $queueName; $this->exchangeName = $exchangeName; $this->routeKey = $routeKey; $this->pool = new \Pool($this->config->get('pool')['thread']); } public function main(){ $connection = new \AMQPConnection($this->config->get('rabbitmq')); try { $connection->connect(); if (!$connection->isConnected()) { $this->logging->exception("Cannot connect to the broker!" .PHP_EOL); } $this->channel = new \AMQPChannel($connection); $this->exchange = new \AMQPExchange($this->channel); $this->exchange->setName($this->exchangeName); $this->exchange->setType(AMQP_EX_TYPE_DIRECT); //direct類型 $this->exchange->setFlags(AMQP_DURABLE); //持久�? $this->exchange->declareExchange(); $this->queue = new \AMQPQueue($this->channel); $this->queue->setName($this->queueName); $this->queue->setFlags(AMQP_DURABLE); //持久�? $this->queue->declareQueue(); $this->queue->bind($this->exchangeName, $this->routeKey); $this->queue->consume(function($envelope, $queue) { $msg = $envelope->getBody(); $this->logging->debug('Protocol: '.$msg.' '); //$result = $this->loader($msg); $this->pool->submit(new RabbitThread($this->queueName, new \framework\log\Logging($this->logqueue, $debug=true), $msg)); $queue->ack($envelope->getDeliveryTag()); }); $this->channel->qos(0,1); } catch(\AMQPConnectionException $e){ $this->logging->exception($e->__toString()); } catch(\Exception $e){ $this->logging->exception($e->__toString()); $connection->disconnect(); $this->pool->shutdown(); } } private function fault($tag, $msg){ $this->logging->exception($msg); throw new \Exception($tag.': '.$msg); } public function __destruct() { } }
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結(jié)》及《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
- PHP實(shí)現(xiàn)異步延遲消息隊(duì)列的方法詳解
- PHP加MySQL消息隊(duì)列深入理解
- PHP Beanstalkd消息隊(duì)列的安裝與使用方法實(shí)例詳解
- PHP PDO和消息隊(duì)列的個(gè)人理解與應(yīng)用實(shí)例分析
- php+redis實(shí)現(xiàn)消息隊(duì)列功能示例
- PHP使用ActiveMQ實(shí)現(xiàn)消息隊(duì)列的方法詳解
- PHP消息隊(duì)列實(shí)現(xiàn)及應(yīng)用詳解【隊(duì)列處理訂單系統(tǒng)和配送系統(tǒng)】
- PHP多進(jìn)程通信-消息隊(duì)列使用
- php消息隊(duì)列實(shí)現(xiàn)詳解
相關(guān)文章
PHP實(shí)現(xiàn)的AES雙向加密解密功能示例【128位】
這篇文章主要介紹了PHP實(shí)現(xiàn)的AES雙向加密解密功能,結(jié)合實(shí)例形式分析了基于AES算法的加密解密類定義與使用技巧,需要的朋友可以參考下2018-09-09php定義參數(shù)數(shù)量可變的函數(shù)用法實(shí)例
這篇文章主要介紹了php定義參數(shù)數(shù)量可變的函數(shù)用法,實(shí)例分析了func_get_args的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03php獲取當(dāng)前時(shí)間的毫秒數(shù)的方法
php本身沒有提供返回毫秒數(shù)的函數(shù),但提供了一個(gè)microtime()函數(shù),借助此函數(shù),可以很容易定義一個(gè)返回毫秒數(shù)的函數(shù)2014-01-01PHP Undefined index報(bào)錯(cuò)的修復(fù)方法
用$_GET["xx"]這種形式取得數(shù)據(jù)時(shí),如果之前不加判斷,$_GET["xx"]不存在時(shí)會(huì)出現(xiàn)這樣的警告:PHP Notice: undefined index xxx。2011-07-07利用PHP計(jì)算有多少小于當(dāng)前數(shù)字的數(shù)字方法示例
這篇文章主要給大家介紹了關(guān)于利用PHP計(jì)算有多少小于當(dāng)前數(shù)字的數(shù)字的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08PHP如何獲取Cookie并實(shí)現(xiàn)模擬登錄
這篇文章主要介紹了PHP如何獲取Cookie并實(shí)現(xiàn)模擬登錄,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07