简体   繁体   English

PM2中的群集和前叉模式差异

[英]Cluster and Fork mode difference in PM2

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 feature but no one says about pros of Fork mode (maybe, it can get NODE_APP_INSTANCE variable). PM2的公共站点解释说集群模式可以实现这些功能,但没有人说出Fork模式的优点(也许可以获取NODE_APP_INSTANCE变量)。

I feel like Cluster might be part of Fork because Fork seems like to be used in general. 我觉得Cluster可能是Fork的一部分,因为Fork似乎被广泛使用。 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'. 因此,我猜想Fork从PM2的角度讲只是“分叉的过程”,而Cluster则是“能够扩展的分叉的过程”。 Then, why should I use Fork mode? 然后,为什么要使用分叉模式?

The main difference between fork_mode and cluster_mode is that it orders pm2 to use either the child_process.fork api or the cluster api. fork_modecluster_mode之间的主要区别在于,它命令pm2使用child_process.fork api或集群 api。

What does this means internally? 这在内部意味着什么?

Fork mode 货叉模式

Take the fork mode as a basic process spawning. fork模式作为基本过程生成。 This allows to change the exec_interpreter , so that you can run a php or a python server with pm2. 这允许更改exec_interpreter ,以便您可以使用exec_interpreter运行phppython服务器。 Yes, the exec_interpreter is the "command" used to start the child process. 是的, exec_interpreter是用于启动子进程的“命令”。 By default, pm2 will use node so that pm2 start server.js will do something like: 默认情况下,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 cluster will only work with node as it's exec_interpreter because it will access to the nodejs cluster module (eg: isMaster , fork methods etc.). cluster只能与node因为它是exec_interpreter因为它将访问nodejs集群模块(例如: isMasterfork方法等)。 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.js will launch 4 instances of server.js and let the cluster module handle load balancing. 例如pm2 start -i 4 server.js将推出的4个实例server.js ,让簇模块处理负载平衡。

Node.js is single-thread. Node.js是单线程的。

That means only 1 core of your Intel quad-core CPU can execute the node application. 这意味着您的Intel四核CPU中只有1个核可以执行该节点应用程序。

It called: fork_mode . 它称为: fork_mode

We use it for local dev . 我们将其用于本地开发人员

pm2 start server.js -i 0 helps you running 1 node thread on each core of your CPU. pm2 start server.js -i 0帮助您在CPU的每个内核上运行1个节点线程。

And auto-load-balance the 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进行压力测试,也可以选择在本地开发人员上执行此操作:)

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 cluster or child_process API. 阅读源代码中的内容,唯一的区别似乎是它们使用节点clusterchild_process API。 Since cluster uses the latter, you are actually doing the same. 由于cluster使用后者,因此您实际上在做同样的事情。 There is just a lot more custom stdio passing around happening inn fork_mode . 客栈fork_mode发生了很多自定义stdio传递。 Also cluster can only be communicated with via strings, not objects. 此外, cluster只能通过字符串进行通信,而不能通过对象进行通信。

By default you are using fork_mode . 默认情况下,您使用fork_mode If you pass the the -i [number] -option, you're going into cluster_mode , which you generally aim for w/ pm2 . 如果传递-i [number] ,那么您将进入cluster_mode ,通常目标是w / pm2

Also fork_mode instance probably can't listen on the same port due to EADDRINUSE . 另外, fork_mode实例可能无法在同一端口上EADDRINUSE cluster_mode can. cluster_mode可以。 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 eg sessions, dbs. 您必须通过会话,数据库等无状态构建应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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