Home > Article > Backend Development > Linux tutorial: manual compilation of php-amqp extension tutorial under ubuntu
Linux Tutorial: Manually compile php-amqp extension tutorial under ubuntu
First of all, what is amqp? The introduction is here. In simple terms, it is the advanced Queue protocol. This extension is to enable PHP to support amqp protocol and related queueservice communication.
Advantages: It can solve the concurrency problem of server processing.
Advanced Messaging Queuing Protocol (AMQP) is an application layer protocol specification used for asynchronous messaging. As a wire-layer protocol, rather than an API (such as JMS), AMQP clients can send and receive messages arbitrarily regardless of their source. Now, there are already quite a few servers and clients for different platforms that can be put into use.
(1) Basic concepts
RabbitMQ is a popular open source messaging queue system, developed in erlang language. RabbitMQ is a standard implementation of AMQP (Advanced Message Queuing Protocol). If you are not familiar with AMQP, it will be difficult to read the RabbitMQ documentation directly. However, it only has a few key concepts, which are briefly introduced here. A few concept explanations:
Broker: Simply put, it is a messageThat is, Client - AMQP server - Client
The client on the left sends a message to the client on the right. The process is::
Install the php compilation tool<code>sudo apt<span>-get</span> install php5<span>-dev</span></code>
Install the rabbitmq library
<code>sudo apt<span>-get</span> install librabbitmq<span>-dev</span></code>
If your Linux distribution does not have a ready-made librabbitmq-dev package, you can compile and install it by downloading the source code
And if you don’t have To install git, please install git, because we need to get the source code from the official repository
Clone the source code and compile
<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>
Compile library
<code>autoreconf -i && ./configure && <span>make</span> && sudo <span>make</span> install</code>
Then we need to download the source code of the php extension, the address is here:
http://pecl.php.net/package/amqp
The latest version is 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>
Create the configuration file
<code><span>sudo</span><span>echo</span><span>"extension = amqp.so"</span> > /etc/php5/conf.d/amqp.ini</code>
Then restart your web server or php-fpm and print phpinfo, if you see the following The content indicates that the extension is installed
Example
Producer
<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>
Consumer
<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>
The above has introduced the Linux tutorial on manually compiling the php-amqp extension tutorial under ubuntu, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.