For details, how to use PM2 to make Node.js’s clusters easier

As we all know, Node.js runs on the Chrome’s JavaScript runtime platform, and we are elegantly called V8 engine. Whether it is a V8 engine, or after the Node.js is running in a single thread, there is no maximum performance in the system of multi-core processors.

Cluster module

Fortunately, node.js provides us with a Cluster module, which can generate multiple work threads to share the same TCP connect.

How is it working?

First, Cluster creates an master and then copies multiple Server App according to your specified quantity, and also known as a working thread). It communicates between the IPC channel with the working thread and uses a built-in load balancing to better handle the pressure between the threads, which uses the Round-Robin algorithm (also known as a cycle algorithm).

When using the Round-Robin scheduling policy, all incoming connection requests, and then transmits the corresponding TCP request processing to the selected working thread (this mode is still communicated by IPC).

How do you use it?

The following is the most basic example:

VAR Cluster = Require (‘cluster’); var http = request (‘http’); var OS = Require (‘OS’); var numcpus = os.cpus (). Length; if (cluster.ismaster) {// master: // let’s fork as a company workers as you have, cpu Cores forks(VAR i = 0; i
  < numCPUs; ++i) {
} else {
 // Worker:
 // Let's spawn a HTTP server
 // (Workers can share any TCP connection.
 // In this case its a HTTP server)

 http.createServer(function(req, res) {
  res.end("hello world");

 You can specify any number of working threads, the number of threads is not limited to the number of CPU cores, because it is just as a run The sub-thread on the CPU. 
As you can see, you need to make it normally, you need to package your code into the cluster’s processing logic and add some extra code to specify the thread. How to do it after hanging it.

built-in cluster

The internal contained processing logic is included, so you Don’t make any modifications to the code. We restore the above code into the most original form:

VAR http = required (‘http’); http.createserver (Function) REQ, RES) {RES.WRITEHEAD (200); res. (“Hello World”);}). Listen (8080);

then execute at the console :
   $ PM2 START APP.JS -I 4 
parameter is used to tell PM2 Run your app (corresponding called fork_mode) in the form of Cluster_Mode, the number of work threads to be started. If a given number is 0, PM2 will generate the corresponding working thread based on your CPU core

No matter what circumstances, keep your apps has been running
 If any of the working thread is hanging, don't worry, PM2 will immediately Restart. Of course, you can also manually restart these threads at any time:   
Real-time extension cluster

If you need to increase the number of working threads, the cluster can be extended by PM2 Scale

. Parameter

Specifies the number of working threads to increase or decrease the number of clusters. You can also specify how much work threads to add by PM2 Scale App +3. 详解如何使用PM2将Node.js的集群变得更加容易

The reload

feature of the PM2 in the product environment will be restarted in turn. Each thread will wait for the new thread to be terminated, so when you deploy a new code in the product environment, Server will continue to operate.

Use the gracefulreLoad function to reach the same purpose, the difference is that it will not terminate the working thread immediately, but send a shutdown signal through the IPC to close all current connections and handle some custom tasks, then Elegantly exit. As of the following code: 详解如何使用PM2将Node.js的集群变得更加容易

Process.on (‘Message’, Function (MSG) {if (msg === ‘shutdown’) {close_all_connections (); delete_cache ); server.close (); process.exit (0);}});

configured PM2 to automatically start I want PM2 After the server is restarted, the previous application is automatically run, and you can start your application by PM2 START, then execute the following command:


PM2 Save

This will generate a dump.pm2 file in ~ / .pm2 directory, which describes current PM2 All applications running on. Then execute the command:

PM2 STARTUP [Platform]
Note It is necessary to add an optional parameter Platform to clearly notify PM2 current system environment. In this way, the next time the server is restarted, PM2 will automatically run the previously saved application.

The function of the Cluster module is very powerful, which makes it easier to use PM2. In the Node 0.10.x era Cluster.js is just a test product, but starting from Node 0.11.x, it has gradually matured and started to be officially released, and of course, Node 0.12.x versions. It is highly recommended to use the latest version of Node.js and PM2, which contributors of these products have been working hard and make them better.

Enjoy the convenience of PM2 to Node.js cluster operation!
 Original address:   More related PM2 installation and use can be viewed here documents : Http:// 
The above is the full content of this article, I hope that everyone will help, I hope everyone supports Tumi Cloud.

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment