Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

通过集群提高 Node.js 应用程序性能 #8101

Merged
merged 3 commits into from Mar 6, 2021
Merged

通过集群提高 Node.js 应用程序性能 #8101

merged 3 commits into from Mar 6, 2021

Conversation

ghost
Copy link

@ghost ghost commented Feb 27, 2021

@lsvih 译文完成 1 次校对,resolve #8001

@Ashira97
Copy link
Contributor

Ashira97 commented Mar 5, 2021

@lsvih 校对认领~

@lsvih
Copy link
Member

lsvih commented Mar 5, 2021

@Ashira97 欢迎校对~

@lsvih lsvih removed the 校对认领 label Mar 5, 2021
Copy link
Contributor

@Ashira97 Ashira97 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lsvih 校对完成
@zenblo 提了一点建议供您参考~ 译者大大辛苦啦~


An instance of Node.js runs in a single thread which means that on a multi-core system (which most computers are these days), not all cores will be utilized by the app. To take advantage of the other available cores, you can launch a cluster of Node.js processes and distribute the load between them.
Node.js 实例是在单个进程中运行的,意味着在如今大多数计算机都在使用的多核系统(multi-core system)上,该应用程序不会使用所有核硬件。要想使用其他可用核硬件,你可以启动 Node.js 进程的集群并在它们之间分配负载。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

核硬件 => CPU
这样似乎更容易理解一点~


An instance of Node.js runs in a single thread which means that on a multi-core system (which most computers are these days), not all cores will be utilized by the app. To take advantage of the other available cores, you can launch a cluster of Node.js processes and distribute the load between them.
Node.js 实例是在单个进程中运行的,意味着在如今大多数计算机都在使用的多核系统(multi-core system)上,该应用程序不会使用所有核硬件。要想使用其他可用核硬件,你可以启动 Node.js 进程的集群并在它们之间分配负载。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你可以启动 Node.js 进程的集群
=>以集群模式启动 Node.js 进程


Having multiple processes to handle incoming requests means that several requests can be processed simultaneously and if there is a long-running/blocking operation on one worker, the other workers can continue handling other incoming requests — your app won’t have come to a standstill until the blocking operation completes.
有多个进程来处理传入请求意味着可以同时处理多个请求,如果一个工作进程上有长时间运行或阻塞操作,其他工作进程可以继续处理传入请求,直到阻塞操作完成,应用程序才会停止运行。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直到阻塞操作完成,应用程序才会停止运行。
=>这里的意思我理解应该是:因为利用多核来处理传入请求,所以当遇到阻塞请求的时候,不会发生阻塞请求结束才去处理其他请求的情况。
那么翻译出来应该是:如果一个工作进程上有长时间运行或阻塞操作,其他工作进程可以继续处理传入请求,避免了直到阻塞操作完成,应用程序才继续处理请求。
但是看原文意思确实是译者翻译的这个意思......我迷惑了.......
译者大大可以帮忙解答一下吗...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多个进程处理任务,即使有阻塞操作也不影响整体任务,完成正常传入任务以及阻塞任务,就可以结束整个应用程序。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里确实读起来有些让人疑惑,建议按您自己的理解改改


Running multiple workers also makes it possible to update your app in production with little to no downtime. You can make changes to your app and restart workers one at a time, waiting for one child process to fully spawn before restarting another. This way, there will always be workers running while you update the app.
通过运行多个工作进程,我们可以在不停机停止生产的情况下更新应用程序。我们可以在修改应用程序,每次只重启一个工作进程,等待一个子进程完全生成后再重启另一个子进程。这样,在我们更新应用程序时,就一直会有工作进程在维持运作。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不停机停止生产 => 不停机

* The master process listens for connections on a port and distributes them across the workers in a round-robin fashion. This is the default approach on all platforms, except Windows.
* The master process creates a listen socket and sends it to interested workers that will then be able to accept incoming connections directly.
* 除了 Windows 平台,所有平台默认采用主进程监听端口上的连接,并以循环方式将它们分布在工作进程中的方法。
* 主进程创建一个监听套接字,并将其发送给感兴趣的工作进程,然后这些工作进程将能够直接收到传入的连接。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感兴趣的 => 相关的

@@ -141,31 +141,31 @@ App listening on port 3000
App listening on port 3000
```

To see the improvement clustering offers, run the same experiment as before: first make a request to the server with a large value for `n` and quickly run another request in another browser tab. The second request will complete while the first is still running — it doesn’t have to wait for the other request to complete. With multiple workers available to take requests, both server availability and throughput are improved.
要查看改进集群提供的功能,请运行与之前相同的示例:首先向服务器发送一个很大的 `n` 值请求,然后在另一个浏览器选项卡中快速运行另一个请求。第二个请求将在第一个请求仍在运行时完成,而无需等待其它请求完成。由于可以使用多个工作进程来处理请求,因此服务器可用性和吞吐量都得到了提高。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要查看改进集群提供的功能 => 要体验使用集群获得的改进


However, if your app isn’t running a lot of CPU-intensive tasks, then it might not be worth the overhead to spawn up so many workers. Remember, each process you create has its own memory and V8 instance. Because of the additional resource allocations, spawning a large number of child Node.js processes is not always recommended.
但是,如果你的应用程序没有执行大量的 CPU 密集型任务,那么可能不值得产生大量工作进程的开销。请记住,你创建的每个进程都有其自身的内存和 V8 实例。由于存在额外的资源分配,因此通常不建议生成大量 Node.js 子进程。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那么可能不值得产生大量工作进程的开销 =>那么通过集群模式获得的性能提升可能并不足以弥补产生大量工作进程带来的开销


![Stopped app](https://d33wubrfki0l68.cloudfront.net/3f2289bd93daf31b044a86b88c1966da0ac07fd4/91949/images/blog/2021-02/stopped_app.png)

Rather than always passing in configurations when you run the app with `pm2 start app.js -i 0`, you can save them to a separate configuration file — called [Ecosystem File](https://pm2.keymetrics.io/docs/usage/application-declaration/#ecosystem-file). The file also allows you to set up specific configurations for different applications, which can be particularly useful for micro-service apps, for example.
可以使用 `pm2 start app.js -i 0` 运行应用程序,而不必总是传递配置,而是可以将它们保存到单独的配置文件中,称为[生态系统文件(Ecosystem File](https://pm2.keymetrics.io/docs/usage/application-declaration/#ecosystem-file)。该文件还允许你为不同的应用程序设置特定的配置,例如,这对于微服务应用程序特别有用。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以使用 pm2 start app.js -i 0 运行应用程序,而不必总是传递配置,而是可以将它们保存到单独的配置文件中
=>使用 pm2 start app.js -i 0 运行应用程序时不必总是传递配置,您也可以将它们保存到单独的配置文件中,


```sh
$ pm2 ecosystem
```

It will generate a file named `ecosystem.config.js`. As for our app, we need to modify it as shown below:
它将生成一个名为 `ecosystem.config.js` 的文件。同时,我们需要对应用程序进行如下修改:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我们需要对应用程序进行如下修改 => 我们需要对该文件进行如下修改

@@ -436,23 +436,23 @@ module.exports = {
}
```

By setting `exec_mode` with the `cluster` value, you instruct PM2 to load balance between each instance. The `instances` are set to `0` just as before, which will spawn as many workers as there are CPU cores.
通过使用集群值(`cluster value`)设置 `exec_mode`,可以指示 PM2 在每个示例之间进行负载平衡。将示例设置为 `0`,这将产生与 CPU 内核数量一样多的工作进程。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

示例 => 实例

@ghost
Copy link
Author

ghost commented Mar 6, 2021

@Ashira97 感谢校对!
@lsvih 完成 2 次校对

@lsvih lsvih merged commit f6d18d1 into xitu:master Mar 6, 2021
@lsvih
Copy link
Member

lsvih commented Mar 6, 2021

@zenblo 已经 merge 啦~ 快快麻溜发布到掘金然后给我发下链接,方便及时添加积分哟。

掘金翻译计划有自己的知乎专栏,你也可以投稿哈,推荐使用一个好用的插件
专栏地址:https://zhuanlan.zhihu.com/juejinfanyi

@lsvih lsvih added 翻译完成 and removed Tag 一次校对修改完成 正在校对 labels Mar 6, 2021
@ghost ghost deleted the improving-node-application-performance-with-clustering branch March 6, 2021 07:45
@ghost
Copy link
Author

ghost commented Mar 7, 2021

@PassionPenguin @Ashira97 @lsvih 掘金文章发布地址 https://juejin.cn/post/6936755284685619213

@lsvih
Copy link
Member

lsvih commented Mar 7, 2021

收到

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

通过集群提高 Node.js 应用程序性能
2 participants