简体   繁体   中英

Using pm2 with rabbitmq nodejs

I need to setup a nodejs cluster that uses pm2 to manage. For communicating and passing message between the workers I am using rabbitmq.

I have gone through many articles but having some confusion basically regarding the flow.

These are the requirements:

When a order is created also create a booking for ordered services. Here I am thinking to pass the creation of booking to the worker process.

When a booking is created notify user and the devliery body also the admin.

This is what picture I have in my head for now.

I will start a node js cluster using pm2 as below.

 // 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
      }
    }]
    }

This is will start 4 workers.

Now How would I pass any task to these workers through rabbitmq?

Or should I another to workers for each task like.

NotificationWorker and BookingCreationWorker.

These two workers will wait for any task in their queue and process it?

i'll suggest you add a worker for BookingCreationWorker task and a consumer for NotificationWorker.js

{
  apps: [
    {
      name: 'API',
      script: 'server.js',
      instances: 2,
      watch: true,
      exec_mode: "cluster",
      max_memory_restart: '1G',
      "env": {
        "PORT" : "9090"
      }
    },
    {
      name: 'CreateBookWorker',
      watch: true,
      script: 'worker/BookingCreationWorker.js',
      instances: 2
    }
  ]
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM