composer.json file to the 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 an 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 should be consistent with the type of the above exchange. |
$host | Cluster access address, which can be obtained from the Client Access module on the basic cluster information page. |
$port | Port number in the cluster access address. |
$username | Username. Enter the username created in the console. |
$password | User password. Enter the password specified during user creation in the console. |
$vhost | Vhost name, which can be obtained from the vhost list in the console. |
$routing_keys[$x] | Routing Key bound to the consumer message queue, which is the routing rule for messages and can be obtained from the Binding Key column of the binding relationship 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 an exchange.$channel->exchange_declare($exchange_name, $exchange_type, false, true, false);// Declare a 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 the Routing Key.$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 to the specified message queue.$channel->basic_consume($queue_name, '', false, true, false, false, $callback);while ($channel->is_open()) {$channel->wait();}// Disable 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 should be consistent with 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 the Client Access module on the basic cluster information page. |
$port | Port number in the cluster access address. |
$username | Username. Enter the username created in the console. |
$password | User password. Enter the password specified during user creation in the console. |
$vhost | Vhost name, which can be obtained from the vhost list in the console. |
$routing_keys[$x] | Routing Key supported by TDMQ for RabbitMQ. The Routing Key bound to the consumer message queue is the routing rule for messages and can be obtained from the Binding Key column of the binding relationship list in the console. |

Feedback