Linux教程之ubuntu下手動編譯php-amqp擴充教學
首先,神馬是amqp?介紹在這裡,簡單的講就是高階隊列協定。而這個擴充就是為了讓php可以支援amqp協定與相關的佇列服務通訊。
優點:可以解決伺服器處理的並發問題。
進階訊息佇列協定(AMQP)是一個非同步訊息傳遞所使用的應用層協定規範。作為線路層協議,而不是API(例如JMS),AMQP 用戶端能夠無視訊息的來源任意發送和接受訊息。現在,已經有相當一部分不同平台的伺服器和客戶端可以投入使用。
(一)基本概念
RabbitMQ 是流行的開源訊息佇列系統,用erlang語言開發。 RabbitMQ是AMQP(高階訊息佇列協 議)的標準實作。如果不熟悉AMQP,直接看RabbitMQ的文件會比較困難。不過它也只有幾個關鍵概念,這裡簡單介紹一下。
幾個概念說明:
(二)使用流程
即 Client - AMQP server - Client
左邊的Client向右邊的Client發送訊息,流程:
準備工作
:安裝php編譯工具<code>sudo apt<span>-get</span> install php5<span>-dev</span></code>
<code>sudo apt<span>-get</span> install librabbitmq<span>-dev</span></code>
如果你的Linux發行版沒有現成的librabbitmq-dev包,那麼安裝源包,那麼安裝源碼安裝git話請安裝git,因為我們要從官方的版本庫中獲取源代碼
克隆源碼並編譯
<code>git <span>clone</span> git:<span>//<strong>github</strong>.com/alanxz/rabbitmq-c.git</span> cd rabbitmq-c git submodule init git submodule update</code>
編譯庫
<code>autoreconf -i && ./configure && <span>make</span> && sudo <span>make</span> install</code>
然後我們需要去下載php擴展的源代碼,地址在此:
http://pecl.php.net/package/amqp
目前最新版本為1.4.0
<code>wget http://pecl<span>.php</span><span>.net</span>/get/amqp-<span>1.4</span><span>.0</span><span>.tgz</span>tar zxf amqp-<span>1.4</span><span>.0</span><span>.tgz</span> cd amqp-<span>1.4</span><span>.0</span>/ phpize && ./configure --with-amqp && make && sudo make install</code>
建立設定檔
<code><span>sudo</span><span>echo</span><span>"extension = amqp.so"</span> > /etc/php5/conf.d/amqp.ini</code>
然後重啟你的web伺服器或php-fpm並列印phpinfo,如果見到以下的內容就說明擴充安裝好了
範例
生產者
<code><span>/** * PHP amqp(RabbitMQ) Demo - publisher * 生产者:发送消息 * 逻辑:创建连接-->创建channel-->创建交换机<strong>对象</strong>-->发送消息* */</span><span>//配置信息</span><span>$conn_args</span> = <span>array</span>( <span>'host'</span> => <span>'localhost'</span>, <span>'port'</span> => <span>'5672'</span>, <span>'login'</span> => <span>'guest'</span>, <span>'password'</span> => <span>'guest'</span>, <span>'vhost'</span>=><span>'/'</span> ); <span>$e_name</span> = <span>'e_lamp'</span>; <span>//交换机名</span><span>$k_route</span> = <span>'key_1'</span>; <span>//路由key</span><span>//创建连接和channel</span><span>$conn</span> = <span>new</span> AMQPConnection(<span>$conn_args</span>); <span>if</span> (!<span>$conn</span>->connect()) { <span>die</span>(<span>"Cannot connect to the broker!\n"</span>); } <span>$channel</span> = <span>new</span> AMQPChannel(<span>$conn</span>); <span>//消息内容</span><span>$message</span> = <span>"TEST MESSAGE! 测试消息!"</span>; <span>//创建交换机<strong>对象</strong></span><span>$ex</span> = <span>new</span> AMQPExchange(<span>$channel</span>); <span>$ex</span>->setName(<span>$e_name</span>); <span>//发送消息</span><span>//$channel->startTransaction(); //开始事务</span><span>for</span>(<span>$i</span>=<span>0</span>; <span>$i</span><<span>5</span>; ++<span>$i</span>){ <span>echo</span><span>"Send Message:"</span>.<span>$ex</span>->publish(<span>$message</span>, <span>$k_route</span>).<span>"\n"</span>; } <span>//$channel->commitTransaction(); //提交事务</span><span>$conn</span>->disconnect();</code>
消費者
<code><span>/** * PHP amqp(RabbitMQ) Demo - consumer * 消费者:接收消息 * 逻辑:创建连接-->创建channel-->创建交换机-->创建<strong>队列</strong>-->绑定交换机/<strong>队列</strong>/路由键-->接收消息* */</span><span>//配置信息</span><span>$conn_args</span> = <span>array</span>( <span>'host'</span> => <span>'localhost'</span>, <span>'port'</span> => <span>'5672'</span>, <span>'login'</span> => <span>'guest'</span>, <span>'password'</span> => <span>'guest'</span>, <span>'vhost'</span>=><span>'/'</span> ); <span>$e_name</span> = <span>'e_lamp'</span>; <span>//交换机名</span><span>$q_name</span> = <span>'q_lamp'</span>; <span>//<strong>队列</strong>名</span><span>$k_route</span> = <span>'key_1'</span>; <span>//路由key</span><span>//创建连接和channel</span><span>$conn</span> = <span>new</span> AMQPConnection(<span>$conn_args</span>); <span>if</span> (!<span>$conn</span>->connect()) { <span>die</span>(<span>"Cannot connect to the broker!\n"</span>); } <span>$channel</span> = <span>new</span> AMQPChannel(<span>$conn</span>); <span>//创建交换机</span><span>$ex</span> = <span>new</span> AMQPExchange(<span>$channel</span>); <span>$ex</span>->setName(<span>$e_name</span>); <span>$ex</span>->setType(AMQP_EX_TYPE_DIRECT); <span>//direct类型</span><span>$ex</span>->setFlags(AMQP_DURABLE); <span>//持久化</span><span>echo</span><span>"Exchange Status:"</span>.<span>$ex</span>-><span>declare</span>().<span>"\n"</span>; <span>//创建<strong>队列</strong></span><span>$q</span> = <span>new</span> AMQPQueue(<span>$channel</span>); <span>$q</span>->setName(<span>$q_name</span>); <span>$q</span>->setFlags(AMQP_DURABLE); <span>//持久化</span><span>echo</span><span>"Message Total:"</span>.<span>$q</span>-><span>declare</span>().<span>"\n"</span>; <span>//绑定交换机与<strong>队列</strong>,并指定路由键</span><span>echo</span><span>'Queue Bind: '</span>.<span>$q</span>->bind(<span>$e_name</span>, <span>$k_route</span>).<span>"\n"</span>; <span>//阻塞模式接收消息</span><span>echo</span><span>"Message:\n"</span>; <span>while</span>(<span>True</span>){ <span>$q</span>->consume(<span>'processMessage'</span>); <span>//$q->consume('processMessage', AMQP_AUTOACK); //自动ACK应答</span> } <span>$conn</span>->disconnect(); <span>/** * 消费回调函数 * 处理消息 */</span><span><span>function</span><span>processMessage</span><span>(<span>$envelope</span>, <span>$queue</span>)</span> {</span><span>$msg</span> = <span>$envelope</span>->getBody(); <span>echo</span><span>$msg</span>.<span>"\n"</span>; <span>//处理消息</span><span>$queue</span>->ack(<span>$envelope</span>->getDeliveryTag()); <span>//手动发送ACK应答</span> }</code>
以上就介紹了Linux教程之ubuntu下手動編譯php-amqp擴充教程,包含了方面的內容,希望對PHP教程有興趣的朋友有所幫助。