Cluster Mode集群模式

The cluster mode allows networked Node.js applications (http(s)/tcp/udp server) to be scaled across all CPUs available, without any code modifications. 群集模式允许网络Node.js应用程序(http(s)/tcp/udp服务器)跨所有可用CPU进行扩展,而无需任何代码修改。This greatly increases the performance and reliability of your applications, depending on the number of CPUs available. 这将大大提高应用程序的性能和可靠性,具体取决于可用CPU的数量。Under the hood, this uses the Node.js cluster module such that the scaled application’s child processes can automatically share server ports. 在引擎盖下,它使用Node.js集群模块,这样扩展应用程序的子进程可以自动共享服务器端口。To learn more, see How It Works in the official Node.js documentation on the cluster module.要了解更多信息,请参阅集群模块的官方Node.js文档中的工作原理

http://i.imgur.com/kTAowsL.png

Usage用法

To enable the cluster mode, just pass the -i option:要启用群集模式,只需传递-i选项:

pm2 start app.js -i max

max means that PM2 will auto detect the number of available CPUs and run as many processes as possiblemax意味着PM2将自动检测可用CPU的数量,并运行尽可能多的进程

Or via a js/yaml/json file:或者通过js/yaml/json文件

module.exports = {
  apps : [{
    script    : "api.js",
    instances : "max",
    exec_mode : "cluster"
  }]
}

NOTE: you need to set the exec_mode to cluster so PM2 know you want to load balance between each instances, by default it will not注意:您需要将exec_mode设置为cluster,以便PM2知道您希望在每个实例之间实现负载平衡,默认情况下不会

Then to start the Process File:然后启动流程文件:

pm2 start processes.json

The -i or instances option can be:-iinstances选项可以是:

Reload重新加载

As opposed to restart, which kills and restarts the process, reload achieves a 0-second-downtime reload.restart启动相反,reload会终止并重新启动进程,重新加载可实现0秒的停机重新加载。

To reload an app:要重新加载应用程序,请执行以下操作:

pm2 reload <app_name>

Or:或:

pm2 reload process.json
pm2 reload process.json --only api

If the reload system hasn’t managed to reload your application, a timeout will fallback to a classic restart.如果重新加载系统没有成功地重新加载应用程序,则超时将退回到经典的重新启动。

Graceful Shutdown优雅地关闭

In production environment, you may need to wait for remaining queries to be processed or close all connections before exiting the application. 在生产环境中,您可能需要等待剩余的查询得到处理,或者在退出应用程序之前关闭所有连接。On the PM2 reload context it can be translated into a very long reload or a reload that doesn’t work (fallback to restart) meaning that your application still has open connections on exit. PM2重新加载上下文中,它可以转换为非常长的重新加载或无法工作的重新加载(回退以重新启动),这意味着您的应用程序在退出时仍有打开的连接。You may alternatively need to close all databases connections, clear data queues or whatever.或者,您可能需要关闭所有数据库连接、清除数据队列或其他任何操作。

To Gracefully Shutdown an application you can catch the SIGINT signal (the first signal sent on exit by PM2) and execute actions to wait/clear all these states:要正常关闭应用程序,您可以捕获SIGINT信号(PM2在退出时发送的第一个信号),并执行操作以等待/清除所有这些状态:

process.on('SIGINT', function() {
   db.stop(function(err) {
     process.exit(err ? 1 : 0);
   });
});

Read more about Graceful Shutdown feature.了解更多有关优雅关机功能的信息。

Statelessify your application无状态化应用程序

Be sure your application is stateless meaning that no local data is stored in the process, for example sessions/websocket connections, session-memory and related. 确保您的应用程序是无状态的,这意味着进程中没有存储任何本地数据,例如会话/websocket连接、会话内存和相关数据。Use Redis, Mongo or other databases to share states between processes.使用Redis、Mongo或其他数据库在进程之间共享状态。

Another resource on how to write efficient, production ready stateless application is The Twelve Factor Application manifesto.关于如何编写高效、生产就绪的无状态应用程序的另一个资源是12因素应用程序宣言