Linux tutorial Ubuntu Manual compilation Php-amqp Extension tutorial

Source: Internet
Author: User
Tags ack rabbitmq
Linux tutorial Ubuntu Manual compilation Php-amqp Extension tutorial

First, the god Horse is AMQP? The introduction here, simply speaking, is the advanced Queuing protocol. This extension is intended to allow PHP to support the AMQP protocol to communicate with the associated queue service.

Pros: Resolves concurrency issues that are handled by the server.
Advanced Message Queuing Protocol (AMQP) is an application-layer protocol specification used by asynchronous messaging. As a line-layer protocol, rather than an API (such as JMS), AMQP clients can send and receive information arbitrarily regardless of the source of the message. Now, quite a few servers and clients of different platforms can be put into use.

(i) Basic concepts

RabbitMQ is a popular open source message queuing system, developed in Erlang language. RABBITMQ is the standard implementation of the AMQP (Advanced message Queuing protocol). If you are unfamiliar with AMQP, it can be difficult to see RABBITMQ documents directly. But it also has only a few key concepts, which are briefly described here.

Several concept notes:

    • Broker: The message Queuing Server entity is simply the case.
    • Exchange: A message switch that specifies what rules the message is routed to and to which queue .
    • Queue: A message queue carrier in which each message is put into one or more queues .
    • Binding: Bind, which is the role of binding exchange and queue according to routing rules.
    • Routing key: The routing keyword, exchange messages are delivered based on this keyword.
    • Vhost: Virtual host, a broker can open multiple vhost, as a separate user permissions.
    • Producer: The message producer is the program that delivers the message.
    • Consumer: The message consumer is the program that receives the message.
    • Channel: The message channels, in each connection of the client, multiple channels can be established, each channel represents a session task.

(ii) Use of the process

namely CLIENT-AMQP Server-client
The client on the left sends a message to the client on the right, the process:

    • Get conection
    • Get Channel
    • Define Exchange,queue
    • Binding a queue to an exchange using a Routingkey
    • Send messages to the corresponding queue by specifying an exchange and a routingkey.
    • The receiver also acquires the connection at the time of receipt, then acquires the channel, then assigns a queue directly to the queue it cares about, and it does not care about exchange,routingkey and how it is binding. Go to the corresponding queue and pick up the message, OK?

Because the default source of Ubuntu does not have php5-amqp this package, so you have to use the AMQP Test manual compilation.

preparatory work :

Installing the PHP Compilation tool

sudo apt-get install php5-dev

Installing the RABBITMQ Library

sudo apt-get install librabbitmq-dev

If your Linux distribution does not have a ready-made Librabbitmq-dev package, you can compile and install it by downloading the source code

Then if you don't have git installed, install git, because we're going to get the source code from the official repository.

Cloning and compiling the source code

clone git://github.com/alanxz/rabbitmq-c.gitcd rabbitmq-cgit submodule initgit submodule update

Compiling libraries

makemake install

Then we need to download the source code of the PHP extension, the address here:

Http://pecl.php.net/package/amqp

Currently 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

Creating a configuration file

sudoecho"extension = amqp.so" > /etc/php5/conf.d/amqp.ini

Then restart your Web server or PHP-FPM and print the phpinfo, if you see the following to indicate that the extension is installed

Example

Production side

/** * PHP amqp (RabbitMQ) demo-publisher * Producer: Send Message * Logic: Create connection--Create channel--> Create switch object --Send Message * *///configuration information$conn _args=Array(' Host '=' localhost ',' Port '=' 5672 ',' Login '=' Guest ',' Password '=' Guest ',' Vhost '='/');$e _name=' E_lamp ';//Switch name$k _route=' Key_1 ';//Route key//Create connections and channel$conn=NewAmqpconnection ($conn _args);if(!$conn->connect ()) { die("Cannot connect to the broker!\n");}$channel=NewAmqpchannel ($conn);//message content$message="TEST message! Test Message! ";//Create switch object $ex=NewAmqpexchange ($channel);$ex->setname ($e _name);//Send Message//$channel->starttransaction ();//Start Transaction for($i=0;$i<5; ++$i){Echo"Send Message:".$ex->publish ($message,$k _route)."\ n";}//$channel->committransaction ();//Submit a transaction$conn->disconnect ();

Consuming party

/** * PHP amqp (RabbitMQ) demo-consumer * Consumer: Receive Message * Logic: Create a connection--create a channel--> Create a switch--Create a queue -- Bind switch/ queue /route keys to receive messages * *///configuration information$conn _args=Array(' Host '=' localhost ',' Port '=' 5672 ',' Login '=' Guest ',' Password '=' Guest ',' Vhost '='/');$e _name=' E_lamp ';//Switch name$q _name=' Q_lamp ';// queue name$k _route=' Key_1 ';//Route key//Create connections and channel$conn=NewAmqpconnection ($conn _args);if(!$conn->connect ()) { die("Cannot connect to the broker!\n");}$channel=NewAmqpchannel ($conn);//Create switch$ex=NewAmqpexchange ($channel);$ex->setname ($e _name);$ex->settype (Amqp_ex_type_direct);//direct Type$ex->setflags (amqp_durable);//PersistenceEcho"Exchange Status:".$ex-Declare()."\ n";//Create a queue $q=NewAmqpqueue ($channel);$q->setname ($q _name);$q->setflags (amqp_durable);//PersistenceEcho"Message total:".$q-Declare()."\ n";//Bind switches and queues , and specify routing keysEcho' Queue Bind: '.$q->bind ($e _name,$k _route)."\ n";//Blocking mode to receive messagesEcho"message:\n"; while(True){$q->consume (' ProcessMessage ');//$q->consume (' ProcessMessage ', amqp_autoack);//Auto ACK response}$conn->disconnect ();/*** Consumption callback function * Processing messages * / functionprocessmessage($envelope, $queue) {$msg=$envelope->getbody ();Echo$msg."\ n";//Processing messages$queue->ack ($envelope->getdeliverytag ());//Send ACK response manually}

The above describes the Linux tutorial, Ubuntu Manual compilation Php-amqp extension tutorial, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.