【问题标题】:Setting Heartbeat when connecting to RabbitMQ via Amqplib通过 Amqplib 连接到 RabbitMQ 时设置 Heartbeat
【发布时间】:2020-05-22 11:43:36
【问题描述】:

通过amqplib连接RabbitMQ时,Heartbeat参数的值无法设置。

我这样做:

// NPM Modules
const amqp = require('amqplib/callback_api');
const withAutoRecovery = require('amqplib-auto-recovery');

// Application configuration
const config = settings.getConfig();

withAutoRecovery(amqp, {
  onError: (err) => { console.log(err.message); },
  isErrorUnrecoverable: (err) => { console.log(err) }
}).connect(config.rabbitmq, (err, connection) => {
    // If there are connection errors
    if (err) {
        console.error(this.conn_str_amqp);
        console.error('[AMQP] Connection error:', err.message); 
    } else if (connection) {
        // Connection Error Event
        connection.on('error', (err) => {
            if (err.message !== 'Connection closing') {
                console.error('[AMQP] Closing connection', err.message);
            }
        });
        // Error event when you close the connection
        connection.on('close', () => {
            console.error('[AMQP] Closing connection');
        });
        // Hint to user
        console.log('[AMQP] Connected');
    }
});

这里 config.rabbitmq 是:

{
  protocol: 'amqp',
  hostname: 'localhost',
  port: 5672,
  username: 'guest',
  password: 'guest',
  locale: 'en_US',
  frameMax: 0x1000,
  heartbeat: 1800,
  vhost: '/',
}

我希望得到Heartbeat = 1800s参数值,在UI管理窗口显示默认值Heartbeat = 60s

截图:

我尝试传递另一个对象,而不是 config.rabbitmq 行(https://www.rabbitmq.com/uri-spec.htmlhttps://www.rabbitmq.com/uri-query-parameters.html),格式为:

function getRabbitMQConnectionString() {
    const rabbitmq = config.rabbitmq;
    return `${rabbitmq.protocol}://${rabbitmq.username}:${rabbitmq.password}@${rabbitmq.hostname}:${rabbitmq.port}${encodeURIComponent(rabbitmq.vhost)}?heartbeat=${rabbitmq.heartbeat}`;
}

因此,结果也与上述效果相似。

请告诉我,如何通过 amqplib 上的 Node.js 客户端应用程序正确设置 Heartbeat 参数?

【问题讨论】:

  • 嗨,你是怎么解决这个问题的?提前致谢
  • 嘿,我也面临同样的问题
  • 嗨。在我看来,问题出在特定版本的 RabbitMQ 中。更新了更高版本 - 它工作正常。

标签: node.js rabbitmq


【解决方案1】:

出于某种原因,我将心跳设置为 30 秒,它起作用了。

【讨论】:

    【解决方案2】:

    从 RabbitMQ 文档中,他们建议不要将心跳设置为高,因为它可以禁用它们。 但是,我不知道显示为 60 年代的心跳。

    https://www.rabbitmq.com/heartbeats.html#disabling

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-15
      • 1970-01-01
      • 2017-04-17
      • 1970-01-01
      • 1970-01-01
      • 2011-09-30
      • 2017-10-15
      • 1970-01-01
      相关资源
      最近更新 更多