Clustering Node.js just got easier with PM2

introduce

  As we all know, Node.js runs on Chrome's JavaScript runtime platform , which we elegantly call the V8 engine. Both the V8 engine and the later Node.js run in a single-threaded manner, so it cannot exert its maximum performance in a multi-core processor system.

Node.js cluster module

  Fortunately, Node.js provides us with the cluster module, which can spawn multiple worker threads to share the same TCP connection.

  How does it work?

  First, the Cluster will create a master, and then replicate as many server apps (also called worker threads) as you specify. It communicates with worker threads through IPC channels, and uses built-in load balancing to better handle the stress between threads, which uses the Round-robin algorithm (also known as the round-robin algorithm).

  When using the Round-robin scheduling strategy, the master  accepts() all incoming connection requests and then sends the corresponding TCP request processing to the selected worker thread (which still communicates via IPC).

  How to use it?

  Here is a basic example:

copy code
var cluster = require('cluster');  
var http    = require('http');  
var os      = require('os');

var numCPUs = os.cpus().length;

if (cluster.isMaster) {  
  // Master:
  // Let's fork as many workers as you have CPU cores

  for (var i = 0; i < numCPUs; ++i) {
    cluster.fork();
  }
} 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.writeHead(200);
    res.end("hello world");
  }).listen(8080);
}
copy code

  Of course, you can specify any number of worker threads, and the number of threads is not limited to the number of CPU cores, since it just acts as a child thread running on the CPU.

  As you can see, to make it work, you need to wrap your code into the cluster's processing logic and add some extra code to specify what to do when a thread dies.

Ways to use PM2

Built-in cluster

  PM2 contains all of the above processing logic internally, so you don't have to make any changes to the code. We restore the above code to its most primitive form:

copy code
var http = require('http');

http.createServer(function(req, res) {  
  res.writeHead(200);
  res.end("hello world");
}).listen(8080);
copy code

  Then execute on the console:

$ pm2 start app.js -i 4

  The -i <number of workers> parameter is used to tell PM2 to run your app in cluster_mode (correspondingly called fork_mode), and the number after it indicates the number of worker threads to start. If the given number is 0, PM2 will spawn corresponding worker threads based on the number of your CPU cores.

Keep your apps running no matter what

  If any of the worker threads dies, don't worry, PM2 will restart them immediately. Of course, you can also restart these threads manually at any time:

Scale clusters in real time

  任何时候,如果你需要增加工作线程的数量,可以通过pm2 scale <app name> <n>来对集群进行扩展。参数<n>指定工作线程的数量,被用来增加或减少集群数。你也可以通过pm2 scale app +3的方式来指定要增加多少工作线程。

在产品环境实现零停机更新

  PM2的reload <app name>功能将依次重启所有的工作线程。每一个线程会等待在新的线程创建之后才会被终止掉,因此,当你在产品环境部署新的代码时,server会不间断地一直保持运行。

  使用gracefulReload功能可以达到相同的目的,不同的是它不会立即终止工作线程,而是通过IPC发送一个shutdown信号来关闭所有当前的连接并处理一些自定义的任务,然后再优雅地退出。如下面的代码:

copy code
process.on('message', function(msg) {  
  if (msg === 'shutdown') {
    close_all_connections();
    delete_cache();
    server.close();
    process.exit(0);
  }
});
copy code

将PM2配置成自动启动

  If you want PM2 to automatically run the previous application after the server restarts, you can start your application with pm2 start, and then execute the following command:

pm2 save

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

pm2 startup [platform]

  Note that it is necessary to add the optional parameter platform to explicitly inform pm2 of the current system environment. This way, the next time the server restarts, PM2 will automatically run the previously saved application.

in conclusion

  The Cluster module is very powerful and using PM2 makes it even easier. In the Node 0.10.x era, cluster.js was just an experimental product, but it has gradually matured since Node 0.11.x and started to prepare for official release, including Node 0.12.x of course. The latest versions of Node.js and PM2 are highly recommended, and the contributors to these products are constantly working to make them even better.

  Enjoy the convenience that PM2 brings to Node.js cluster operations!

Original address: https://keymetrics.io/2015/03/26/pm2-clustering-made-easy/

More information on PM2 installation and usage can be found here: http://pm2.keymetrics.io/docs/usage/quick-start/

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326570157&siteId=291194637