rabbitmq你该知道的秘密 - 消息如何做到持久化
消息如何做到持久化?
实践
查看send_durable.php
文件
git地址:https://github.com/xiaochengfu/rabbitmq-tutorials/blob/develop/php/send_durable.php
代码
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
$connection = new AMQPStreamConnection('59.110.213.203', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('durable', false, true, false, false,false);
//$properties = [
// 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT
//];
//$msg = new AMQPMessage('消息不要丢!', $properties);
$msg = new AMQPMessage('消息不要丢!');
for ($i = 1; $i <= 50; $i++) {
$channel->basic_publish($msg, '', 'durable');
echo "$i [x] Sent '消息不要丢!'\n";
sleep(1);
}
$channel->close();
$connection->close();
执行步骤:
- 将queue的durable参数设为false,循环发送50个消息,通过管理后台查看消息数,重启服务,再次查看消息数
- 将queue的durable参数设为true,message的delivery_mode为1, 循环发送50个消息,通过管理后台查看消息数,重启服务,再次查看消息数
- 将queue的durable参数设为true,message的delivery_mode为2, 循环发送50个消息,通过管理后台查看消息数,重启服务,再次查看消息数
重启服务,通过docker来重启rabbitmq服务
结论
- 消息的持久化(写到磁盘),需要将消息和存放消息的队列同时设为持久化才有效
- 只有队列才有存储消息的能力
官方文档:https://www.rabbitmq.com/tutorials/tutorial-two-python.html
When RabbitMQ quits or crashes it will forget the queues and messages unless you tell it not to. Two things are required to make sure that messages aren't lost: we need to mark both the queue and messages as durable.
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
评论已关闭