- 启动个RabbitMQ用于测试
docker run -d --name some-rabbit-mgr -p 8080:15672 -p 5672:5672 rabbitmq:3-management
2. 创建生产者 send.php
<?php
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
require_once __DIR__ . '/vendor/autoload.php';
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('hello', false, false, false, false);
$msg = new AMQPMessage('Hello World!');
$channel->basic_publish($msg, '', 'hello');
echo " [x] Sent 'Hello World!'\n";
$channel->close();
$connection->close();
3. 创建消费者 receive.php
<?php
use PhpAmqpLib\Connection\AMQPStreamConnection;
require_once __DIR__ . '/vendor/autoload.php';
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('hello', false, false, false, false);
echo " [*] Waiting for messages. To exit press CTRL+C\n";
$callback = function (PhpAmqpLib\Message\AMQPMessage $msg) {
echo ' [x] Received ', $msg->body, "\n";
$msg->ack(); // 手动确认消息
};
// 第四个参数改成false不自动确认消息
$channel->basic_consume('hello', '', false, false, false, false, $callback);
while ($channel->is_open()) {
$channel->wait();
}
$channel->close();
$connection->close();
关键词: RabbitMQ消息确认
查看所有容器:
docker container ls -a
停止刚才启动的容器:
docker container stop x2-rabbit-mgr
启动刚才的容器:
docker container sart x2-rabbit-mgr
查看这个容器的状态:
docker container stats x2-rabbit-mgr