php-amqplib
client. First, you need to import the php-amqplib
library into your project.composer.json
file to your project.{"require": {"php-amqplib/php-amqplib": ">=3.0"}}
composer.phar install
composer install
require_once('../vendor/autoload.php');
require_once('../vendor/autoload.php');use PhpAmqpLib\\Connection\\AMQPStreamConnection;use PhpAmqpLib\\Message\\AMQPMessage;$exchange_name = 'exchange_name';$exchange_type = 'direct';// Create a connection$connection = new AMQPStreamConnection($host,$port,$username,$password,$vhost,false,'PLAIN');// Establish a channel$channel = $connection->channel();// Declare the exchange$channel->exchange_declare($exchange_name, $exchange_type, false, true, false);// Set the message routing key$routing_keys = array('info', 'waring', 'error');for ($x = 0; $x < count($routing_keys); $x++) {// Message content$msg = new AMQPMessage('This is a direct[' . $routing_keys[$x] . '] message!');// Send a message to the specified exchange and set the routing key$channel->basic_publish($msg, $exchange_name, $routing_keys[$x]);echo " [Producer(Routing)] Sent '" . $msg->body . "'\\n";}// Release resources$channel->close();$connection->close();
Parameter | Description |
$exchange_name | Exchange name, which can be obtained from the exchange list in the console. |
$exchange_type | It must be the same as the type of the above exchange. |
$host | Cluster access address, which can be obtained from Access Address in the Operation column on the Cluster page. |
$port | Cluster access port. |
$username | |
$password | |
$vhost | Vhost name in the format of "cluster ID + | + vhost name", which can be copied on the Vhost page in the console. |
$routing_keys[$x] | Routing key bound to the consumer message queue, which is also the message routing rule and can be obtained in the Binding Key column in the binding list in the console. |
<?phprequire_once('../vendor/autoload.php');require_once('../Constant.php');use PhpAmqpLib\\Connection\\AMQPStreamConnection;$exchange_name = 'exchange_name';$exchange_type = 'direct';$queue_name = 'route_queue1';// Create a connection$connection = new AMQPStreamConnection($host,$port,$username,$password,$vhost,false,'PLAIN');// Establish a channel$channel = $connection->channel();// Declare the exchange$channel->exchange_declare($exchange_name, $exchange_type, false, true, false);// Declare the message queue$channel->queue_declare($queue_name, false, true, false, false);// Set the queue routing key$routing_keys = array('info', 'waring', 'error');for ($x = 0; $x < count($routing_keys); $x++) {// Bind the message queue to the specified exchange and set `routingKey`$channel->queue_bind($queue_name, $exchange_name, $routing_keys[$x]);}echo " [Consumer1(Routing: info/waring/error)] Waiting for messages. To exit press CTRL+C\\n";// Message callback (message consumption logic)$callback = function ($msg) {echo ' [Consumer1(Routing: info/waring/error)] Received ', $msg->body, "\\n";};// Create a consumer to listen on the specified message queue$channel->basic_consume($queue_name, '', false, true, false, false, $callback);while ($channel->is_open()) {$channel->wait();}// Disable the resource$channel->close();$connection->close();
Parameter | Description |
$exchange_name | Exchange name, which can be obtained from the exchange list in the console. |
$exchange_type | It must be the same as the type of the above exchange. |
$queue_name | Queue name, which can be obtained from the queue list in the console. |
$host | Cluster access address, which can be obtained from Access Address in the Operation column on the Cluster page. |
$port | Cluster access port. |
$username | |
$password | |
$vhost | Vhost name in the format of "cluster ID + | + vhost name", which can be copied on the Vhost page in the console. |
$routing_keys[$x] | Routing key bound to the consumer message queue, which is also the message routing rule and can be obtained in the Binding Key column in the binding list in the console. |
Was this page helpful?