NOT_FOUND - 虚拟主机“/”中没有队列 'node-topic-queue-one'

NOT_FOUND - no queue 'node-topic-queue-one' in vhost '/'

我制作了一个简单的应用程序,可以通过 RabbitMQ 服务器向客户端发送消息。 为此,我创建了如下发送方代码。

var amqp=require('amqp');

var connection = amqp.createConnection({host:'localhost',login:'guest',password:'guest'});

connection.on('ready', function () {
 // There is no need to declare type, 'topic' is the default:

 var exchange = connection.exchange('node-topic-exchange'); 

 console.log("publishing messages");
 exchange.publish("topic_a.subtopic_a", {msg:'First Message'});
 exchange.publish("topic_a.subtopic_b", {msg:'Second Message'});
 exchange.publish("topic_b.subtopic_b", {msg:'Third Message'});
 });

 connection.on('error',function (err) {
  console.log('an error '+err);
 });

我的接收方代码如下所示:

var amqp=require('amqp');
var connection = amqp.createConnection({host:'localhost',login:'guest',password:'guest'}); 
 connection.on('ready', function () {
  // There is no need to declare type, 'topic' is the default:
  var exchange = connection.exchange('node-topic-exchange');
  // Consumer:
  var queue = connection.queue('node-topic-queue-one');
  queue.bind(exchange, "topic_a.*");
  queue.subscribe(function (message) {
  // Get original message string:
  console.log('Message : ' + message.msg);
   });
  });
  connection.on('error',function (err) {
 console.log('an error '+err);
});

问题是..它给出了这样的错误

D:\node example\RabbitMQ Example>node worker1.js an error

Error: NOT_FOUND - no exchange 'node-topic-exchange' in vhost '/' an error Error: read ECONNRESET

请帮忙解决这个问题.....

您需要使用 node.js 事件驱动方法:"An exchange will emit the 'open' event when it is finally declared." - 当您尝试使用它时,交换尚未声明。

在接收方,您需要对交换和队列执行相同的操作:"A queue will call the callback given to the connection.queue() method once it is usable"

为了完整起见,我在这里报告示例:

var q = connection.queue('my-queue', function (queue) {
  console.log('Queue ' + queue.name + ' is open');
});