node.js PM2 中的 Cluster 和 Fork 模式差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34682035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cluster and Fork mode difference in PM2
提问by Jinyoung Kim
I've searched a lot to figure out this question, but I didn't get clear explanation. Is there only one difference thing that clustered app can be scaled out and forked app cannot be?
我已经搜索了很多来弄清楚这个问题,但我没有得到明确的解释。集群应用程序可以横向扩展而分叉应用程序不能扩展,这是否只有一件事?
PM2's public site explains Cluster mode can do these featurebut no one says about pros of Fork mode (maybe, it can get NODE_APP_INSTANCEvariable).
PM2 的公共站点解释了 Cluster 模式可以完成这些功能,但没有人说 Fork 模式的优点(也许,它可以NODE_APP_INSTANCE可变)。
I feel like Cluster might be part of Fork because Fork seems like to be used in general. So, I guess Fork means just 'forked process' from the point of PM2 and Cluster means 'forked process that is able to be scaled out'. Then, why should I use Fork mode?
我觉得 Cluster 可能是 Fork 的一部分,因为 Fork 似乎被普遍使用。所以,我猜从 PM2 的角度来看,Fork 的意思只是“分叉的进程”,而 Cluster 的意思是“能够扩展的分叉的进程”。那么,为什么要使用 Fork 模式呢?
回答by soyuka
The main difference between fork_modeand cluster_modeis that it orders pm2 to use either the child_process.forkapi or the clusterapi.
fork_mode和之间的主要区别在于cluster_mode它命令pm2使用child_process.forkapi 或集群api。
What does this means internally?
这在内部意味着什么?
Fork mode
叉模式
Take the forkmode as a basic process spawning. This allows to change the exec_interpreter, so that you can run a phpor a pythonserver with pm2. Yes, the exec_interpreteris the "command" used to start the child process. By default, pm2 will use nodeso that pm2 start server.jswill do something like:
把fork模式作为一个基本的进程产生。这允许更改exec_interpreter,以便您可以使用 pm2运行一个php或一个python服务器。是的,这exec_interpreter是用于启动子进程的“命令”。默认情况下, pm2 将使用,node以便pm2 start server.js执行以下操作:
require('child_process').spawn('node', ['server.js'])
This mode is very useful because it enables a lot of possibilities. For example, you could launch multiple servers on pre-established ports which will then be load-balanced by HAProxy or Nginx.
这种模式非常有用,因为它提供了很多可能性。例如,您可以在预先建立的端口上启动多个服务器,然后由 HAProxy 或 Nginx 进行负载平衡。
Cluster mode
集群模式
The clusterwill only work with nodeas it's exec_interpreterbecause it will access to the nodejs cluster module (eg: isMaster, forkmethods etc.). This is great for zero-configuration process management because the process will automatically be forked in multiple instances.
For example pm2 start -i 4 server.jswill launch 4 instances of server.jsand let the cluster module handle load balancing.
该cluster只一起工作node,因为它是exec_interpreter(:例如,因为它会进入到集群的NodeJS模块isMaster,fork方法等)。这对于零配置流程管理非常有用,因为该流程将在多个实例中自动分叉。例如pm2 start -i 4 server.js将启动 4 个实例server.js并让集群模块处理负载平衡。
回答by haotang
Node.js is single-thread.
Node.js 是单线程的。
That means only 1 core of your Intel quad-core CPU can execute the node application.
这意味着只有英特尔四核 CPU 的 1 个内核可以执行节点应用程序。
It called: fork_mode.
它叫:fork_mode。
We use it for local dev.
我们将它用于本地开发。
pm2 start server.js -i 0helps you running 1 node thread on each core of your CPU.
pm2 start server.js -i 0帮助您在 CPU 的每个核心上运行 1 个节点线程。
And auto-load-balancethe stateless coming requests.
并自动负载平衡无状态即将到来的请求。
On the same port.
在同一个端口上。
We call it: cluster_mode.
我们称之为:cluster_mode。
Which is used for the sake of performance on production.
这是为了生产性能而使用的。
You may also choose to do this on local dev if you want to stress test your PC :)
如果您想对您的 PC 进行压力测试,您也可以选择在本地开发人员上执行此操作:)
回答by eljefedelrodeodeljefe
Documentation and sources are really misleading here.
文档和来源在这里确实具有误导性。
Reading up on this in the sources, the only differences seems to be, that they use either node clusteror child_processAPI. Since clusteruses the latter, you are actually doing the same. There is just a lot more custom stdiopassing around happening inn fork_mode. Also clustercan only be communicated with via strings, not objects.
在源代码中阅读这一点,唯一的区别似乎是,它们使用节点cluster或child_processAPI。由于cluster使用后者,您实际上也在做同样的事情。只是有更多的自定义stdio传递发生在客栈fork_mode。也cluster只能通过字符串而不是对象进行通信。
By default you are using fork_mode. If you pass the the -i [number]-option, you're going into cluster_mode, which you generally aim for w/ pm2.
默认情况下,您正在使用fork_mode. 如果您通过 - 选项-i [number],您将进入cluster_mode,您通常针对 w/ pm2。
Also fork_modeinstance probably can't listen on the same port due to EADDRINUSE. cluster_modecan. This way you also can structure you app to run on the same port being automatically load balanced. You have to build apps without state then though e.g. sessions, dbs.
此外,fork_mode由于EADDRINUSE. cluster_mode能够。通过这种方式,您还可以构建您的应用程序,使其在自动负载平衡的同一端口上运行。您必须构建没有状态的应用程序,例如会话、dbs。

