Redis队列的多个node.js消息接收器 [英] Multiple node.js message receivers for a redis queue
问题描述
尝试使用rsmq-worker( https://github.com/mpneuried/rsmq-worker ).必须运行多个工作程序来并行处理消息.
Trying to implement queue processor(s) in node.js application using rsmq-worker (https://github.com/mpneuried/rsmq-worker). Have to run multiple workers to process messages in parallel.
我们如何设置多名工作人员?
How do we setup multiple workers?
使用方法的探索- https://github.com/Automattic/kue #parallel-processing-with-cluster
推荐答案
只需创建指向同一队列的RSMQWorker的多个实例.
Simply create multiple instances of the RSMQWorker pointing to the same queue.
示例:
var workers = [];
for (let i = 0; i < NUMBER_OF_WORKERS; i++) {
workers[i] = createWorker(i);
}
function createWorker(i) {
const worker = new RSMQWorker(QUEUE_NAME, {...});
worker.on("message", processMessage)
return worker;
}
然后,您可以使用rsmq-worker的自动启动选项或手动启动它们:
Then you can use the auto-start option of the rsmq-worker or start them manually:
for (let i = 0; i < workers.length; i++) {
workers[i].start();
}
使用这种方法,您将使用一个Node.js实例并行处理多条消息.如果您的消息处理逻辑涉及某些I/O,则可以提高性能.
With this approach you will be processing multiple messages in parallel using one single Node.js instance. It improves the performance if your message processing logic involves some I/O.
要获得更高的并行度,您可以在Node.js的多个实例中运行上述代码,如其他答案所述.
For an additional level of parallelism, you can run the above code in multiple instances of Node.js, as mentioned in other answer.
这篇关于Redis队列的多个node.js消息接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!