RabbitMQ 数据在崩溃时丢失

RabbitMQ data lost on crash

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

我创建了这些场景:

I updated stock data 3 times with consumers off state (inactive). Then I activated the consumer.It consumed all the three messages from the queue. [Works good.]

Now I again produced three messages (consumer inactive again) then I turned off the rabbitmq server. When I restarted the server and activated the consumer. It doesn't seem to be consuming the data (are the messages that were on the queue has been lost?)

消费者:

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);});

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

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

提前致谢。

为什么你的消息丢失了?

很遗憾,你发送message.Checkhttps://www.rabbitmq.com/tutorials/tutorial-two-javascript.html的时候没有声明{persistent: true},所以你应该使用channel.sendToQueue(queue, Buffer.from(msg), {persistent: true});

他们不执着吗?

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

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

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