【发布时间】: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.html 和 https://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 中。更新了更高版本 - 它工作正常。