我正在尝试使用Node.js连接模块进行RabbitMQ第一次测试,我想先尝试直接交换所以我得到了一个服务器我也是试图收听消息和发布者脚本。
服务器
y
出版商:
5
所以,根据我的理解,我是:
var amqp = require('amqp');
var connection = amqp.createConnection();
connection.on('ready', function () {
var queue = connection.queue('mongo-ops');
connection.exchange('badanie-exchange', {type: 'direct', durable: 'true'}, function () {
queue.bind('badanie-exchange', '*');
queue.subscribe(function (message) {
console.log(message);
})
});
});
类型的var amqp = require('amqp');
var connection = amqp.createConnection();
connection.on('ready', function () {
var exchange = connection.exchange('badanie-exchange');
exchange.publish('*', "La decima", function (err,result) {
console.log(err,result);
});
});
交换。然而,当我运行两个脚本时,控制台上没有任何反应。我做错了什么?
从badanie-exchange
进行测试我尝试过:
direct
和
rabbitmqctl
答案 0 :(得分:1)
There are multiple minor issues with your code, for which I suggest you to read more closely the library documentation. However, there is one particular protocol issue that is preventing your code from working at all: if the exchange being declarated already exists, it must be declared with the same options as the existing exchange, otherwise a channel-error is raised and the channel is closed. From the AMQP specification:
If the exchange exists, the server MUST check that the existing exchange has the same values for type, durable, and arguments fields. The server MUST respond with Declare-Ok if the requested exchange matches these fields, and MUST raise a channel exception if not.
Getting more specific, when you publish the message, you must declare the exchange with the same options that the server use:
var amqp = require('amqp');
var connection = amqp.createConnection();
connection.on('ready', function () {
var exchange = connection.exchange('badanie-exchange',
{type: 'direct', durable: 'true'});
exchange.publish('*', "La decima", function (err,result) {
console.log(err,result);
});
});
答案 1 :(得分:0)
可能这是一个令你搞砸的订单。 尝试先运行server.js文件然后再运行publisher.js。 如果您先运行publisher.js,那么它将不会创建一个工作队列,并且控制台上不会显示任何内容。