如果不是必须的话,就不需要创建FANOUT交换机。
您可以使用与其他交换机已经使用的相同路由键创建DIRECT交换机。而且不需要为新交换机创建新队列。您可以使用现有队列来处理新交换机。您只需要将新交换机与队列绑定即可。
这是我的receive.js文件:
var amqp = require("amqplib/callback_api");
var crontab = require('node-crontab');
amqp.connect("amqp://localhost", function (err, conn) {
conn.createChannel(function (err, ch) {
var ex = 'direct_logs';
var ex2 = 'dead-letter-test';
var severity = 'enterprise-1-key';
ch.assertExchange(ex, 'direct', { durable: true });
ch.assertExchange(ex2, 'direct', { durable: true });
ch.assertQueue('enterprise-11', { exclusive: false, deadLetterExchange: ex2 }, function (err, q) {
var n = 0;
console.log(' [*] Waiting for logs. To exit press CTRL+C');
console.log(q);
ch.bindQueue(q.queue, ex, severity);
ch.bindQueue(q.queue, ex2, severity);
ch.consume(q.queue, function (msg) {
if (msg.fields.exchange === ex2) {
crontab.scheduleJob("* * * * * *", function () {
console.log("Received by latest exchange %s", msg.fields.routingKey, msg.content.toString());
});
} else {
console.log("Received %s", msg.fields.routingKey, msg.content.toString());
}
if (n < 1) {
ch.nack(msg, false, false);
n += 1;
} else {
ch.ack(msg)
n = 0
}
}, { noAck: false });
});
});
});