: This article describes how to manually compile the php-amqp extension tutorial in ubuntu. if you are interested in the PHP Tutorial, refer to it. Linux Tutorial: manually compile php-amqp extension tutorial in ubuntu
First, what is amqp? Here, the introduction is simply advanced.QueueProtocol. This extension enables php to support the amqp protocol and relatedQueueService communication.
Advantage: it can solve the concurrency problem of server processing.
Advanced MessageQueueThe AMQP protocol is an application layer protocol specification used for asynchronous message transmission. As a line-layer protocol, rather than an API (such as JMS), the AMQP client can send and receive messages without considering the source of the message. Currently, a considerable number of servers and clients on different platforms are ready for use.
(1) Basic concepts
RabbitMQ is a popular open source message.QueueSystem, developed in erlang language. RabbitMQ is AMQP (Advanced MessageQueueAgreement. If you are not familiar with AMQP, it is difficult to directly read the document of RabbitMQ. However, it only has several key concepts. here is a brief introduction.
Several concepts:
- Broker: messageQueueServer entity.
- Exchange: The Message Switch, which specifies the rules and routes of messages.Queue.
- Queue: messageQueueCarrier, each message will be invested in one or moreQueue.
- Binding: Binding. it binds exchange and queue according to routing rules.
- Routing Key: The route keyword. exchange ships messages based on this keyword.
- Vhost: virtual host. multiple vhosts can be opened in a broker to separate permissions of different users.
- Producer: message producer, which is the program for delivering messages.
- Consumer: The Message consumer, which is the program that receives the message.
- Channel: Message channel. multiple channels can be created in each connection of the client. each channel represents a session task.
(2) procedure
Client-AMQP server-Client
The Client on the left sends a message to the Client on the right. The process is as follows:
- Get Conection
- Get Channel
- Define Exchange, Queue
- Use a RoutingKey to bind the Queue to an Exchange
- By specifying an Exchange and a RoutingKey, messages are sent to the corresponding Queue,
- The receiver also obtains the connection when receiving the message, then obtains the channel, and then specifies a Queue to directly fetch the message from the Queue it cares about. it does not care about Exchange, RoutingKey, and how to bind the message, get the message from the corresponding Queue.
Because ubuntu's default source does not have the php5-amqp package, so use the amqp to test the manual compilation.
Preparations:
Install the php compilation tool
sudo apt-get install php5-dev
Library for installing rabbitmq
sudo apt-get install librabbitmq-dev
If your Linux release does not have a ready-made librabbitmq-dev package, you can download the source code for compilation and installation.
If you have not installed git, install git because we need to obtain the source code from the official version Library.
Clone and compile the source code
git clone git://github.com/alanxz/rabbitmq-c.gitcd rabbitmq-cgit submodule initgit submodule update
Compile Library
autoreconf -i && ./configure && make && sudo make install
Then we need to download the php extension source code:
Http://pecl.php.net/package/amqp
The latest version is 1.4.0.
wget http://pecl.php.net/get/amqp-1.4.0.tgztar zxf amqp-1.4.0.tgzcd amqp-1.4.0/phpize && ./configure --with-amqp && make && sudo make install
Create a configuration file
sudoecho"extension = amqp.so" > /etc/php5/conf.d/amqp.ini
Restart your web server or php-fpm and print phpinfo. if you see the following content, the extension is installed.
Example
Producer
/*** PHP amqp (RabbitMQ) Demo-publisher * producer: Send messages * logic: create a connection --> create a channel --> Create a switchObject--> Send message ** // configuration information $ conn_args = array ('host' => 'localhost', 'port' => '123 ', 'login' => 'guest ', 'password' => 'guest', 'vhost' => '/'); $ e_name = 'e _ lamp '; // switch name $ k_route = 'key _ 1'; // route key // create a connection and channel $ conn = new AMQPConnection ($ conn_args); if (! $ Conn-> connect () {die ("Cannot connect to the broker! \ N ") ;}$ channel = new AMQPChannel ($ conn); // message content $ MESSAGE =" TEST message! Test message! "; // Create a vSwitchObject$ Ex = new AMQPExchange ($ channel); $ ex-> setName ($ e_name); // send a message // $ channel-> startTransaction (); // start the transaction for ($ I = 0; $ I <5; ++ $ I) {echo "Send Message :". $ ex-> publish ($ message, $ k_route ). "\ n" ;}// $ channel-> commitTransaction (); // submit the transaction $ conn-> disconnect ();
Consumer
/*** PHP amqp (RabbitMQ) Demo-consumer * consumer: receive messages * logic: create a connection --> create a channel --> Create a switch --> create a connectionQueue--> Bind a vSwitch/Queue/Route key --> receive message ** // configuration information $ conn_args = array ('host' => 'localhost', 'port' => '123 ', 'login' => 'guest ', 'password' => 'guest', 'vhost' => '/'); $ e_name = 'e _ lamp '; // switch name $ q_name = 'Q _ lamp ';//QueueNAME $ k_route = 'key _ 1'; // route key // create a connection and channel $ conn = new AMQPConnection ($ conn_args); if (! $ Conn-> connect () {die ("Cannot connect to the broker! \ N ") ;}$ channel = new AMQPChannel ($ conn); // Create a vSwitch $ ex = new AMQPExchange ($ channel); $ ex-> setName ($ e_name ); $ ex-> setType (AMQP_EX_TYPE_DIRECT); // direct Type $ ex-> setFlags (AMQP_DURABLE); // persistent echo "Exchange Status :". $ ex-> declare (). "\ n"; // CreateQueue$ Q = new AMQPQueue ($ channel); $ q-> setName ($ q_name); $ q-> setFlags (AMQP_DURABLE); // persistent echo "Message Total :". $ q-> declare (). "\ n"; // bind the vSwitchQueueAnd specify the route key echo 'queue Bind :'. $ q-> bind ($ e_name, $ k_route ). "\ n"; // The blocking mode receives the Message echo "Message: \ n"; while (True) {$ q-> consume ('processmessage '); // $ q-> consume ('processmessage', AMQP_AUTOACK); // automatic ACK response} $ conn-> disconnect (); /*** consumption callback function * processes messages */functionprocessMessage ($ envelope, $ queue) {$ msg = $ envelope-> getBody (); echo $ msg. "\ n"; // process the message $ queue-> ack ($ envelope-> getDeliveryTag (); // manually send ACK response}
The above describes the Linux tutorial: manually compile the php-amqp extension tutorial under ubuntu, including some content, and hope to help those who are interested in the PHP Tutorial.