【问题标题】:NOT_FOUND - no queue 'node-topic-queue-one' in vhost '/'NOT_FOUND - vhost '/' 中没有队列 'node-topic-queue-one'
【发布时间】:2016-10-14 18:27:38
【问题描述】:

我制作了一个简单的应用程序,通过 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 报错

错误: NOT_FOUND - vhost '/' 中没有交换 'node-topic-exchange' 错误 错误:读取 ECONNRESET

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

【问题讨论】:

  • 我猜你需要使用 node.js 事件驱动的方法:“当最终声明时,交换将发出 'open' 事件。” (github.com/postwait/…) - 当你尝试使用它时,它还没有声明。
  • 它有效,,!!但我在接收方也遇到了同样的错误......@Sigismondo
  • 同样的故事,还有队列!对于其他语言(例如 java),您需要明确地“声明”交换和队列(即,如果不存在则创建),并且为了很好地解耦其在生产者和消费者身上的良好实践。
  • 也添加了答案,以便其他人发现它有用
  • 谢谢..它有效... :) @Sigismondo

标签: node.js rabbitmq amqp


【解决方案1】:

您需要使用 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');
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 2017-01-20
    • 2017-11-19
    • 2013-01-02
    • 2016-10-17
    • 1970-01-01
    相关资源
    最近更新 更多