如何在节点js中使用集群? [英] How to use clusters in node js?

查看:117
本文介绍了如何在节点js中使用集群?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Node.js非常陌生并表示。我目前正在通过构建自己的服务来学习它。
我最近阅读了有关群集的信息。我了解集群的作用。我无法理解的是如何在生产应用程序中使用集群。

I am very new to Node.js and express. I am currently learning it by building my own services. I recently read about clusters. I understood what clusters do. What I am not able to understand is how to make use of clusters in a production application.

我能想到的一种方法是使用Master进程坐在前面,并以循环方式将传入的请求路由到下一个可用的子进程。我不确定这是否是设计用途。我想知道如何在典型的Web应用程序中使用群集。

One way I can think of is to use the Master process to just sit in front and route the incoming request to the next available child process in a round robin fashion. I am not sure if this is how it is designed to be used. I would like to know how should clusters be used in a typical web application.

谢谢。

推荐答案

只要您想将请求处理分散到多个node.js进程中,就可以将node.js集群模块与node.js一起使用。当您希望提高每秒处理更多请求的能力并且您的服务器中有多个CPU内核时,通常使用此方法。默认情况下,node.js的单个实例将不会完全利用多个核心,因为您在服务器中运行的核心Javascript是单线程的(使用一个核心)。 Node.js本身确实在内部将线程用于某些事情,但是仍然不太可能充分利用多核系统。为每个CPU内核设置集群的node.js进程将使您能够更好地最大化可用的计算资源。

The node.js cluster modules is used with node.js any time you want to spread out the request processing across multiple node.js processes. This is most often used when you wish to increase your ability to handle more requests/second and you have multiple CPU cores in your server. By default, a single instance of node.js will not fully utilize multiple cores because the core Javascript you run in your server is single threaded (uses one core). Node.js itself does use threads for some things internally, but that's still unlikely to fully utilize a mult-core system. Setting up a clustered node.js process for each CPU core will allow you to better maximize the available compute resources.

集群化还为您提供了一些额外的容错能力。如果一个群集进程出现故障,则在禁用的群集重新启动时,您仍然可以让其他活动群集为请求服务。

Clustering also provides you with some additional fault tolerance. If one cluster process goes down, you can still have other live clusters serving requests while the disabled cluster restarts.

node.js的群集模块具有几种不同的调度算法-您提到的循环赛是其中之一。您可以在此处了解更多信息:集群循环负载均衡

The cluster module for node.js has a couple different scheduling algorithms - the round robin you mention is one. You can read more about that here: Cluster Round-Robin Load Balancing.

由于每个集群都是一个单独的进程,因此不同集群进程之间没有自动共享的数据。这样,在没有共享数据或共享数据已经存在于可以被多个进程访问的位置(例如在数据库中)的地方,群集化是最​​简单的实现。

Because each cluster is a separate process, there is no automatic shared data among the different cluster processes. As such, clustering is simplest to implement either where there is no shared data or where the shared data is already in a place that it can be accessed by multiple processes (such as in a database).

请记住,单个node.js进程(如果被编写为正确使用异步I / O而不是大量的计算绑定)可以一次处理许多请求。群集是您想要将可伸缩性扩展到一个实例所不能提供的范围。

Keep in mind that a single node.js process (if written to properly use async I/O and not heavily compute bound) can server many requests itself at once. Clustering is when you want to expand scalability beyond what one instance can deliver.

这篇关于如何在节点js中使用集群?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆