rabbitmq模式 hello word
send.php
<?php
require_once __DIR__ . '/../../vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
$connection = new AMQPStreamConnection('localhost', 5672, 'admin', 'admin','vhost1');
$channel = $connection->channel();
$channel->queue_declare('hello', false, false, false, false);
for($num=0;$num<10;$num++){
$msg = new AMQPMessage('Hello World!' . $num);
$channel->basic_publish($msg, '', "hello");
echo " [x] $num Sent 'Hello World!'\n";
sleep(1);
}
$channel->close();
$connection->close();
receive.php
<?php
require_once __DIR__ . '/../../vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
$connection = new AMQPStreamConnection('localhost', 5672, 'admin', 'admin','vhost1');
$channel = $connection->channel();
$channel->queue_declare('hello', false, false, false, false);
echo " [*] Waiting for messages. To exit press CTRL+C\n";
$callback = function ($msg) {
echo ' [x] Received ', $msg->body, "\n";
};
$channel->basic_consume('hello', '', false, true, false, false, $callback);
while ($channel->is_open()) {
$channel->wait();
}
$channel->close();
$connection->close();
运行调试
php receive.php
php send.php
查看监听队列
sudo rabbitmqctl list_queues