Php operation rabbitmq tutorial 1: connect rabbitmq to create exchange and queue
Amqp_manager.php
$ Conn_args = array ('host' => 'localhost', 'port' => '123', 'login' => 'guest ', 'password' => 'guest ');
$ Conn = new AMQPConnection ($ conn_args );
If ($ conn-> connect ()){
Echo "Established a connection to the broker \ n ";
}
Else {
Echo "Cannot connect to the broker \ n ";
Exit (0 );
}
$ Channel = new AMQPChannel ($ conn );
$ Exchange = new AMQPExchange ($ channel );
$ Exchange-> setName ('lizhifeng ');
$ Exchange-> setType (AMQP_EX_TYPE_DIRECT );
$ Exchange-> setFlags (AMQP_DURABLE | AMQP_AUTODELETE );
$ Exchange-> declare (); // declare a router named lizhifeng
// Add a queue named queue1 and bind it to key1
$ Queue = new AMQPQueue ($ channel );
$ Queue-> setName ('queue1 ');
$ Queue-> setFlags (AMQP_DURABLE | AMQP_AUTODELETE );
$ Queue-> declare ();
$ Queue-> bind ('lizhifeng', 'key1 ');
// Add a queue named queue2 and bind it to key2
$ Queue = new AMQPQueue ($ channel );
$ Queue-> setName ('queue2 ');
$ Queue-> setFlags (AMQP_DURABLE | AMQP_AUTODELETE );
$ Queue-> declare ();
$ Queue-> bind ('lizhifeng', 'key2 ');
// Bind queue1 to key3. Note that key3 does not overwrite key1.
// The key1 and key3 will take effect at the same time.
$ Queue = new AMQPQueue ($ channel );
$ Queue-> setName ('queue1 ');
# $ Queue-> setFlags (AMQP_DURABLE | AMQP_AUTODELETE); you do not need to repeatedly set the attributes of queue queue1
# $ Queue-> declare (); you do not need to repeat the statement here.
$ Queue-> bind ('lizhifeng', 'key3 ');
/*
// Delete exchange
$ Exchange = new AMQPExchange ($ channel );
$ Exchange-> setName ('lizhifeng ');
$ Exchange-> delete ();
// Delete a queue
$ Queue = new AMQPQueue ($ channel );
$ Queue-> setName ('queue1 ');
$ Queue-> delete ();
$ Queue = new AMQPQueue ($ channel );
$ Queue-> setName ('queue2 ');
$ Queue-> delete ();
*/
?>
2: connect rabbitmq to write messages to exchange
Amqp_server.php
$ Routingkey = 'key1 ';
$ Conn_args = array ('host' => 'localhost', 'port' => '123', 'login' => 'guest ', 'password' => 'guest ');
$ Conn = new AMQPConnection ($ conn_args );
If ($ conn-> connect ()){
Echo "Established a connection to the broker \ n ";
}
Else {
Echo "Cannot connect to the broker \ n ";
}
$ Channel = new AMQPChannel ($ conn );
$ Exchange = new AMQPExchange ($ channel );
$ Exchange-> setName ('lizhifeng ');
For ($ I = 0; I I <100000; $ I ++)
{
If ($ routingkey = 'key1 ')
{
$ Routingkey = 'key2'; // route to queue queue2
}
Else if ($ routingkey = 'key2 ')
{
$ Routingkey = 'key3'; // route to queue queue1
}
Else
{
$ Routingkey = 'key1'; // route to queue queue1
}
$ Tmp = array ();
$ Tmp [] = "th". $ I. "The key of the message is". $ routingkey;
$ Message = json_encode ($ tmp );
If ($ exchange-> publish ($ message, $ routingkey ))
{
Print $ routingkey. "\ tok \ n ";
}
Else
{
Print "error \ n ";
}
}
3: Connect to rabbitmq to consume messages
Amqp_client.php
// Connect to RabbitMQ
$ Conn_args = array ('host' => '127. 0.0.1 ', 'port' => '123456', '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 ";
Exit ();
}
$ Channel = new AMQPChannel ($ conn );
$ Q = new AMQPQueue ($ channel );
$ Q-> setName ('queue1 ');
// This is not a new queue, but a queue named quene1 is connected.
// I understand that the queue is actually on the server, and the message has been routed to different queues. we only need to retrieve the message
While ($ messages = $ q-> get (AMQP_AUTOACK ))
{
Var_dump (json_decode ($ messages-> getBody (), true ));
}
$ Q = new AMQPQueue ($ channel );
$ Q-> setName ('queue2 ');
While ($ messages = $ q-> get (AMQP_AUTOACK ))
{
Var_dump (json_decode ($ messages-> getBody (), true ));
}
$ Conn-> disconnect ();
?>