【发布时间】: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