崩溃时RabbitMQ数据丢失 为什么您的消息丢失了?他们不是执着吗?哪种中间件可能更适合您?

问题描述

我正在使用RabbitMQ来存储和检索数据。我提到了这个article。我已将durable标志设置为true,并将noAck标志设置为false(即使在消费后,我也需要将消息存储在队列中)。

我创建了以下方案:

我在消费者关闭状态(无效)下更新了3次库存数据。然后我激活了使用者,它消耗了队列中的所有三个消息。 [效果很好。]

现在,我再次产生了三则消息(消费者再次处于非活动状态),然后关闭了Rabbitmq服务器。当我重新启动服务器并激活使用者时。似乎并没有消耗数据(队列中的消息是否丢失了?)

消费者:

connection.createChannel(function (error1,channel) {
if (error1) {
  throw error1;
}
var queue = "updateStock2";

channel.assertQueue(queue,{
  durable: true,});

console.log(
  " [*] Waiting for stockData messages in %s. To exit press CTRL+C",queue
);

channel.consume(
  queue,function (data) {
    stock = JSON.parse(data.content.toString());
    console.log(" [x] Received Stock:",stock.name + " : " + stock.value);
  },{
    noAck: false,}
);

制作人:

  connection.createChannel(function (error1,channel) {
  if (error1) {
    throw error1;
  }

  var queue = "updateStock2";

  channel.assertQueue(queue,{
    durable: true,});
  channel.sendToQueue(queue,Buffer.from(data));

  console.log(" [x] Sent %s",data);
});
setTimeout(function () {
  connection.close();
  //process.exit(0);
},500);});

他们不是执着吗?如果服务器崩溃,队列中的所有消息将永远消失吗?

当服务器崩溃时,如何检索队列中的数据?

谢谢。

解决方法

为什么您的消息丢失了?

遗憾地说,您在发送消息时未声明{persistent: true}。请选中https://www.rabbitmq.com/tutorials/tutorial-two-javascript.html,因此应使用channel.sendToQueue(queue,Buffer.from(msg),{persistent: true});

他们不是执着吗?

持久队列将在节点启动时恢复,包括其中持久发布的消息。发布为临时消息的消息将在恢复期间被丢弃,即使它们存储在持久队列中也是如此。

哪种中间件可能更适合您?

如果您想要一个即使消费者使用也能保留消息的中间件,则可能需要kafka