RabbitMQ+PHP demonstration example

Keywords: Programming PHP RabbitMQ

Create rabbit_consumer.php as a consumer

<?php 
//configuration information 
$conn_args = array( 
    'host' => '127.0.0.1',  
    'port' => '5672',  
    'login' => 'admin',  
    'password' => 'admin', 
    'vhost'=>'/' 
);   
$e_name = 'e_linvo'; //Switch name 
$q_name = 'q_linvo'; //Team name 
$k_route = 'key_1'; //Routing key 
 
//Create connections and channel s 
$conn = new AMQPConnection($conn_args);   
if (!$conn->connect()) {   
    die("Cannot connect to the broker!\n");   
}   
$channel = new AMQPChannel($conn);   
 
//Create switch    
$ex = new AMQPExchange($channel);   
$ex->setName($e_name); 
$ex->setType(AMQP_EX_TYPE_DIRECT); //direct type  
$ex->setFlags(AMQP_DURABLE); //Persistence 
echo "Exchange Status:".$ex->declare()."\n";   
   
//Create queue    
$q = new AMQPQueue($channel); 
$q->setName($q_name);   
$q->setFlags(AMQP_DURABLE); //Persistence  
echo "Message Total:".$q->declare()."\n";   
 
//Bind switch and queue, and specify routing key 
echo 'Queue Bind: '.$q->bind($e_name, $k_route)."\n"; 
 
//Blocking mode receive message 
echo "Message:\n";   
while(True){ 
    $q->consume('processMessage');   
    //$Q - > consume ('processmessage ', AMQP ﹐ autoack); / / Auto ACK reply  
} 
$conn->disconnect();   
 
/**
 * Consumption callback function
 * Processing message
 */ 
function processMessage($envelope, $queue) { 
    $msg = $envelope->getBody(); 
    echo $msg."\n"; //Processing message 
    $queue->ack($envelope->getDeliveryTag()); //Send ACK response manually 
}
?>

 

New rabbit publisher.php as producer

<?php
//configuration information 
$conn_args = array( 
    'host' => '127.0.0.1',  
    'port' => '5672',  
    'login' => 'admin',  
    'password' => 'admin', 
    'vhost'=>'/' 
);   
$e_name = 'e_linvo'; //Switch name 
//$q'name ='q'linvo '; / / no queue name required 
$k_route = 'key_1'; //Routing key 
 
//Create connections and channel s 
$conn = new AMQPConnection($conn_args);   
if (!$conn->connect()) {   
    die("Cannot connect to the broker!\n");   
}   
$channel = new AMQPChannel($conn);   
 

 
//Create switch object    
$ex = new AMQPExchange($channel);   
$ex->setName($e_name);   
date_default_timezone_set("Asia/Shanghai");
//send message 
//$Channel - > starttransaction(); / / start transaction  
for($i=0; $i<5; ++$i){ 
    sleep(1);//Dormancy for one second
    //Message content 
    $message = "TEST MESSAGE!".date("h:i:sa");   
    echo "Send Message:".$ex->publish($message, $k_route)."\n";  
} 
//$Channel - > committransaction(); / / commit transaction 
 
$conn->disconnect();
?>

 

Test it:

Start with a window and switch to the PHP directory. Enter: php rabbit_consumer.php

Run consumer - > consumer receives message

Then start a dos window, switch to the PHP root directory, and enter the following command: PHP rabbit? Publisher.php

Run producer

In this way, we simulate the queue processing of messages. I hope we can improve our understanding of RabbitMQ through this article.

 

In addition, the guide to use PHP on the official website: http://www.rabbitmq.com/tutorials/tutorial-one-php.html

Posted by rallan on Mon, 09 Dec 2019 19:52:35 -0800