首页 > 解决方案 > 如何更新 RabbitMQ 连接参数而不删除 NodeJS 中的队列?

问题描述

我有 2 个 RabbitMQ 实例,它们都有不同的设置。意思是,它们可以按如下方式连接:

在这里,问题是,我可以成功发布到第一个,但是当我尝试连接到第二个实例时,我收到了这个错误。仅供参考,这是两个实例上的同一个队列。

Error: Channel closed by server: 406 (PRECONDITION-FAILED) with message "PRECONDITION_FAILED - inequivalent arg 'alternate-exchange' for exchange 'MY-EXCAHNGE' in vhost '/': received none but current is the value 'MY-EXCAHNGE' of type 'longstr'"

我正在使用"amqplib": "^0.8.0",NodeJS。基于谷歌(更改 RabbitMQ 队列中的参数https ://jira.spring.io/browse/AMQP-746 ),看起来这是一个 RabbitMQ 库问题,您无法在此处更新参数。

我的方法是:a)连接到第一个实例并发布消息,然后关闭通道和连接,然后 b)连接到第二个。以下代码有效,但是当我更新 url (不带vhost)时,我看到了错误。

我不确定为什么await confirmChannel.close()&await getRmqConnection.close()会以undefined. 我可以成功地将消息发布到队列中。

代码:

const url = ${protocol}://${username}:${password}@${hostname}:${port}/${vhost};
async function PublishToRmq(url: string): Promise<Connection> {
    try {
        // 1. Create RMQ connection
        const getRmqConnection = await connect(url);
        // console.log("getRmqConnection -> ", getRmqConnection);

        // 2. Confirm channel connection
        const confirmChannel = await getRmqConnection.createConfirmChannel();
        // console.log("confirmChannel -> ", confirmChannel);

        // 3. verify exchange
        const verifiedExchange: Replies.AssertExchange = await confirmChannel.assertExchange(exchangeOptions.exchange, exchangeOptions.type, exchangeOptions);
        // console.log("verifiedExchange -> ", verifiedExchange);

        // 4. check channel queue
        const checkQueue = await confirmChannel.checkQueue(queueOptions.queue);
        // const checkQueue = await confirmChannel.assertQueue(queueOptions.queue, queueOptions);
        console.log("checkQueue -> ", checkQueue);

        // 5. publish message
        const publishMessage = await confirmChannel.publish(
            exchangeOptions.exchange,
            queueOptions.queue,
            Buffer.from(message),
            );
        console.log("publishMessage -> ", publishMessage);

        // Close channel
        const closeChannel = await confirmChannel.close();
        console.log("closeChannel -> ", closeChannel);

        // Close connection
        const closeConnection = await getRmqConnection.close();
        console.log("closeConnection -> ", closeConnection);

    } catch (error) {
        console.log("error ->", error);
    }
}

问题:我可以通过dead-letter-exchange在 RMQ 中使用来实现这一点吗?或者删除队列是唯一的选择?(这是我第一次使用 RMQ,所以提前感谢您的任何意见)。

标签: node.jsrabbitmqnode-amqplib

解决方案


推荐阅读