node.js - Rabbit MQ amqplib 错误“没有可分配的通道”
问题描述
在 pub sub 模式中工作了一段时间的 rabbit mq workers 之后,我在创建频道时遇到了错误。
Error: No channels left to allocate
解决方案
如果您使用https://www.npmjs.com/package/amqplib,您可以使用 Promise 共享频道,同时发布多条消息
在message-queue.js
const q = 'tasks';
const open = require('amqplib').connect('amqp://localhost');
const channelPromise = open.then((conn) => conn.createChannel());
// Publisher
function publishMessage(message) {
channelPromise.then((ch) => ch.assertQueue(q)
.then((ok) => ch.sendToQueue(q, Buffer.from(message))))
.catch(console.warn);
}
// Consumer
open.then((conn) => conn.createChannel())
.then((ch) => ch.assertQueue(q).then((ok) => ch.consume(q, (msg) => {
if (msg !== null) {
console.log(msg.content.toString());
ch.ack(msg);
}
}))).catch(console.warn);
module.exports = {
publishMessage,
};
some-where.js
messageQueue.publishMessage('hello world')
推荐阅读
- jquery - 当点击正确数量的元素时,如何创建一个 if 语句来显示和隐藏我的页脚部分
- react-native - React Native Youtube androidx.annotation.Nullable 错误
- c - 如何判断一个函数是否在 vfork()'ed 进程中被调用?
- nginx - GKE 中的 Kubernetes - Nginx Ingress 部署 - 分配给 Ingress 资源的公共 IP
- c# - 如何将 BigQuery 时间戳转换为 DateTime?
- php - 尝试更新时的laravel验证错误
- json - 使用 Newtonsoft JSON 和 VB.NET 检查嵌套 JSON 元素中是否存在键
- ios - 具有动态高度的 UITableViewCell 中的 UITableView
- mongodb - Kubernetes MongoDB 自动缩放
- excel - 如何判断是一个月的第一天还是第二天?