我正在使用rabbitMq node-amqplib库编写生产者和消费者,我担心突然失去服务器连接,如何检查连接是否有效?
我尝试创建rabbit-mq 发布者和订阅者。它按预期工作,直到我尝试重新启动我的rabbit-mq 服务器。
我使用rabbitmq:3-management
docker image、ampqlib 5.3
和 Node.js11.10.0
来制作这个简单的程序:
const q = 'tasks';
const { execSync } = require("child_process");
const amqplib = require("amqplib");
function createChannel() {
return amqplib.connect("amqp://root:toor@0.0.0.0:5672/")
.then((conn) => conn.createChannel());
}
Promise.all([createChannel(), createChannel()])
.then(async (channels) => {
const [publisherChannel, consumerChannel] = channels;
// publisher
await publisherChannel.assertQueue(q).then(function(ok) {
return publisherChannel.sendToQueue(q, Buffer.from("something to do"));
});
// consumer
await consumerChannel.assertQueue(q).then(function(ok) {
return consumerChannel.consume(q, function(msg) {
if (msg !== null) {
console.log(msg.content.toString());
consumerChannel.ack(msg);
}
});
});
}) …
Run Code Online (Sandbox Code Playgroud) 我需要为我的连接设置一个友好的名称,如下所示,而不是“?” 在用于 NodeJS 的 amqplib 的 RabbitMQ 中:
我找到了 Java 和 Python 的示例,但还没有找到这个库的示例。谢谢。
我正在尝试集成到使用 NestJS 完成的项目,这是一个简单的 api,您可以在其中发布带有名称(或模式)的消息并将其发送到已实现与该名称匹配的处理程序的系统。
我正在构建的系统非常小,使用 NestJS 没有多大意义。
我遇到的问题如下:
我正在创建一个简单的 API,用于触发将消息发布到队列上。消费者位于使用 NestJS 的系统上。
我不知道如何为该消息提供该系统可识别的模式。例如:
假设我想发布一条名为“CreateRecord”的消息,其中的有效负载要从具有同名处理程序和实现的其他系统进行处理。
使用 amqplib 如何为消息指定名称或模式?
我从 RabbitMq 服务器收到此错误
通道被服务器关闭:406(PRECONDITION-FAILED),消息为“PRECONDITION_FAILED - 未知的交货标签 80”
发生这种情况是因为在消费者任务期间连接丢失,最后,当消息被确认/确认时,我收到此错误,因为我无法在与我收到消息的通道不同的通道上确认消息。
这是 RabbitMq 连接的代码
async connect({ prefetch = 1, queueName }) {
this.queueName = queueName;
console.log(`[AMQP][${this.queueName}] | connecting`);
return queue
.connect(this.config.rabbitmq.connstring)
.then(conn => {
conn.once('error', err => {
this.channel = null;
if (err.message !== 'Connection closing') {
console.error(
`[AMQP][${this.queueName}] (evt:error) | ${err.message}`,
);
}
});
conn.once('close', () => {
this.channel = null;
console.error(
`[AMQP][${this.queueName}] (evt:close) | reconnecting`,
);
this.connect({ prefetch, queueName: this.queueName });
});
return conn.createChannel();
})
.then(ch => {
console.log(`[AMQP-channel][${this.queueName}] created`);
ch.on('error', …
Run Code Online (Sandbox Code Playgroud) $rabbitmqctl list_queues
Timeout: 60.0 seconds ...
Listing queues for vhost / ...
privateTransactionQ 2
amq.gen-o9dl3Zj7HxS50gkTC2xbBQ 0
task_queue 0
Run Code Online (Sandbox Code Playgroud)
rabbitmqctl 的输出如下所示。我无法弄清楚每列的含义。我怎样才能看到每一列的含义?