node.js - 集群模块在 Node.js 中是如何工作的?

标签 node.js tcp

谁能详细解释一下核心cluster模块在 Node.js 中工作?

worker 是如何监听单个端口的?

据我所知,主进程进行监听,但是由于工作人员在主进程之后启动,它如何知道要监听哪些端口?他们是否通过使用 child_process.fork 通信 channel 以某种方式将其传达回主服务器?如果是这样,到端口的传入连接如何从 master 传递到 worker?

我还想知道使用什么逻辑来确定传入连接传递给哪个工作人员?

最佳答案

我知道这是一个老问题,但现在在 nodejs.org here: 上对此进行了解释。

The worker processes are spawned using the child_process.fork method, so that they can communicate with the parent via IPC and pass server handles back and forth.

When you call server.listen(...) in a worker, it serializes the arguments and passes the request to the master process. If the master process already has a listening server matching the worker's requirements, then it passes the handle to the worker. If it does not already have a listening server matching that requirement, then it will create one, and pass the handle to the worker.

This causes potentially surprising behavior in three edge cases:

server.listen({fd: 7}) - Because the message is passed to the master, file descriptor 7 in the parent will be listened on, and the handle passed to the worker, rather than listening to the worker's idea of what the number 7 file descriptor references.

server.listen(handle) - Listening on handles explicitly will cause the worker to use the supplied handle, rather than talk to the master process. If the worker already has the handle, then it's presumed that you know what you are doing.

server.listen(0) - Normally, this will cause servers to listen on a random port. However, in a cluster, each worker will receive the same "random" port each time they do listen(0). In essence, the port is random the first time, but predictable thereafter. If you want to listen on a unique port, generate a port number based on the cluster worker ID.

When multiple processes are all accept()ing on the same underlying resource, the operating system load-balances across them very efficiently. There is no routing logic in Node.js, or in your program, and no shared state between the workers. Therefore, it is important to design your program such that it does not rely too heavily on in-memory data objects for things like sessions and login.

Because workers are all separate processes, they can be killed or re-spawned depending on your program's needs, without affecting other workers. As long as there are some workers still alive, the server will continue to accept connections. Node does not automatically manage the number of workers for you, however. It is your responsibility to manage the worker pool for your application's needs.

关于node.js - 集群模块在 Node.js 中是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9830741/

相关文章:

javascript - nodejs 服务器,写出 png 到响应页面

向我的 Node 服务器发送 AJAX 请求 - 未通过

c# - 简单的网络帮助 (C# WinForms)

javascript - 为什么有人会关闭 Nagle 的算法?

javascript - 如何在 node.js 中设置 xlsx 文件的样式

node.js - 如何在sqlite中保存pdf等文件和图像

javascript - 如何使用 Express 要求多个 Controller ?

c++ - 设置 SO_RCVBUF 会减小窗口缩放因子

linux - 如何实时合并三个TCP流

c# - 处理在不同线程中读取的 TCP Socket 数据