问题描述
我需要设置一个使用pm2进行管理的nodejs集群。 为了在工人之间交流和传递消息,我正在使用Rabbitmq。
我看了很多文章,但基本上对流程感到困惑。
这些是要求:
创建订单时,还会为订购的服务创建预订。在这里,我正在考虑将预订的创建传递给工作流程。
这就是我现在脑海中的照片。
我将如下所示使用pm2启动一个节点js集群。
// ecosystem.js
{
"apps" : [{
"name" : "API","script" : "server.js",// name of the startup file
"instances" : 4,// number of workers you want to run
"exec_mode" : "cluster",// to turn on cluster mode; defaults to 'fork' mode
"env": {
"PORT" : "9090" // the port on which the app should listen
}
}]
}
这将启动4个工人。
现在我该如何通过Rabbitmq将任何任务传递给这些工人?
或者我应该为每个任务给工人另一个人。
NotificationWorker和BookingCreationWorker。
这两个工作人员将等待队列中的任何任务并处理吗?
解决方法
我建议你为 BookingCreationWorker 任务添加一个工作线程,为 NotificationWorker.js 添加一个消费者
{
apps: [
{
name: 'API',script: 'server.js',instances: 2,watch: true,exec_mode: "cluster",max_memory_restart: '1G',"env": {
"PORT" : "9090"
}
},{
name: 'CreateBookWorker',script: 'worker/BookingCreationWorker.js',instances: 2
}
]
};