[Reprinted] Practical application of PHP-AMQP

Original address: Practical application of PHP-AMQP Author: dream
Advantages: Can solve the concurrency problem handled by the server.

The Advanced Message 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 at will, regardless of their origin. Now, there are quite a few servers and clients on different platforms that can be put into use.

(1) Basic Concepts

RabbitMQ is a popular open source message queue system developed in erlang language. I used to be very interested in this language, I learned it for a while, but I didn't stick to it. 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 also has only a few key concepts, which are briefly introduced here.

The structure diagram of RabbitMQ is as follows:

php amqp example - zhukeqing - big fish
 

A few concept notes:

Broker: Simply put, it is the message queue server entity.

Exchange: The message exchange, which specifies what rules the message is routed to which queue.

Queue: message queue carrier, each message will be put into one or more queues.

Binding: Binding, its function is to bind exchange and queue according to routing rules.

Routing Key: Routing keyword, the exchange delivers messages according to this keyword.

vhost: virtual host, multiple vhosts can be opened in one broker to separate permissions for different users.

producer: A message producer is a program that delivers messages.

consumer: A message consumer is a program that accepts messages.

channel: message channel. In each connection of the client, multiple channels can be established, and each channel represents a session task.

(2) Use process

That is, Client - AMQP server - Client

on the left side of the Client sends a message to the Client on the right side, the process is as follows:

1, get Conection

2, get Channel

3, define Exchange, Queue

4, use a RoutingKey to bind Queue to an Exchange

5, by specifying a Exchange and a RoutingKey to send the message to the corresponding Queue.

6. The receiver also obtains the connection when receiving, then obtains the channel, and then specifies a Queue to directly fetch the message from the Queue it cares about. I don't care about binding, just go to the corresponding Queue to get the message and it's OK 

(3) Examples


Producer:

<?php
$conn_args = array('host' =>'localhost', 'port' => 5672, 'login' => 'guest', 'password' => 'guest', 'vhost' => '/');
$conn = new AMQPConnection($conn_args);
if ($conn->connect()) {
    echo "Established a connection to the broker n";
}else {
    echo "Cannot connect to the broker n ";
}
$e_name='ex_test5';//交换机名横
$q_name='q_test5';//队列名称
$r_key='key_test1';

//你的消息
$message = json_encode(array('Hello World!'));
//创建channel
$channel = new AMQPChannel($conn);
//创建exchange
$ex = new AMQPExchange($channel);
$ex->setName($e_name);//创建名字
$ex->setType(AMQP_EX_TYPE_DIRECT);
$ex->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
echo "exchange status:".$ex->declare();
echo "n";
//创建队列
$q = new AMQPQueue($channel);
//设置队列名字 如果不存在则添加
$q->setName($q_name);
$q->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
echo "queue status: ".$q->declare();
echo "n";
echo 'queue bind: '.$q->bind($e_name,$r_key);//将你的队列绑定到routingKey
echo "n"; 

$channel->startTransaction();
echo "send: ".$ex->publish($message, $r_key); //将你的消息通过制定routingKey发送
$channel->commitTransaction();
$conn->disconnect();
?>

消费方

<?php
$conn_args = array('host' =>'localhost', 'port' => 5672, 'login' => 'guest', 'password' => 'guest', 'vhost' => '/');

//$q_name='q_test6';//队列名称
$q_name=$_GET['q'];//队列名称

$conn = new AMQPConnection($conn_args); 
$conn->connect(); 
$channel = new AMQPChannel($conn); 
$q = new AMQPQueue($channel); 
$q->setName($q_name); 
$q->setFlags(AMQP_DURABLE | AMQP_AUTODELETE); 

while($a=$q->declare())
{
    echo "queue status: ".$a;
    echo "==========n";

    $messages = $q->get(AMQP_AUTOACK);
    print_r($messages->getBody()); 
    echo "n"; 
}
$conn->disconnect();
?>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326346138&siteId=291194637