Linux教程之ubuntu下手动编译php-amqp扩展教程
首先,神马是amqp?介绍在这里,简单的讲就是高级队列协议。而这个扩展就是为了让php可以支持amqp协议与相关的队列服务通讯。
优点:可以解决服务器处理的并发问题。
高级消息队列协议(AMQP)是一个异步消息传递所使用的应用层协议规范。作为线路层协议,而不是API(例如JMS),AMQP 客户端能够无视消息的来源任意发送和接受信息。现在,已经有相当一部分不同平台的服务器和客户端可以投入使用。
(一)基本概念
RabbitMQ 是流行的开源消息队列系统,用erlang语言开发。RabbitMQ是AMQP(高级消息队列协 议)的标准实现。如果不熟悉AMQP,直接看RabbitMQ的文档会比较困难。不过它也只有几个关键概念,这里简单介绍。
几个概念说明:
(二)使用流程
即 Client - AMQP server - Client
左边的Client向右边的Client发送消息,流程:
由于ubuntu的默认源里面没有php5-amqp这个包,所以要用上amqp得考手动编译。
准备工作:
安装php编译工具
<code>sudo apt<span>-get</span> install php5<span>-dev</span></code>
安装rabbitmq的库
<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>5; ++<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教程有兴趣的朋友有所帮助。