Node.js 项目到生产,流量很大

Node.js project to production with a lot of traffic

我开始了一个项目,开始有大量的网络流量,在某些时候我对如何将项目扩展到生产环境感到不安全,这有两个问题:

  1. 如何在不让我的用户失去服务的情况下执行更新?
  2. 如何正确配置Node.js使其占用更少的内存?

我有使用 Hydra-express 的微服务,但我无法实现 Hydra-router,我用 Express.js 来实现;我也有 NGINX 作为代理网关。

我正在用 ES6 编程,用 BABEL 转译并用 PM2 维护活跃的微服务,一些用 fork,最重要的是集群模式。

我正在考虑使用 docker,但我还没有找到任何关于如何将其与 CDN 一起使用、上传文件并将其提供给用户的教程。

不可能对 2 给出明确的答案,因为这完全取决于应用程序的功能,没有您可以应用的灵丹妙药配置。

这就剩下第一点了,它围绕着所谓的零停机时间

因此,在有多个服务器向用户返回内容的情况下,例如http 服务器 我想可以公平地说,大多数生产环境在前端都有 something,这与业务逻辑无关。这可以是负载平衡器(有多种形状和形式)或反向代理。这通常是您指向 DNS A 记录的地方。这个服务器基本上应该永远不会宕机。

现在,假设您更改了一些业务逻辑并想要部署新的后端。您通常做的是将负载均衡器(或反向代理)后面已经 运行ning 的进程一个一个地换掉。因此,如果您有五个节点进程,您将停止一个,使用更新后的代码启动一个新进程,并重复直到所有 运行ning 都被换出。

您也可以利用它只换出一个,运行 测试那个,然后继续换出其余的。

要真正确保您不会干扰任何用户,您应该停止在旧进程上接受新的 http 请求,以便将新的 http 请求路由到更新的进程。这将允许正在发生的 http 请求完成。然后你停止旧的进程。

希望这对您有所帮助。

添加到@ralphtheninja 的回答中,我建议您阅读更多关于蓝绿部署的内容,正如 Martin Fowler 所提议的那样; https://martinfowler.com/bliki/BlueGreenDeployment.html

One of the challenges with automating deployment is the cut-over itself, taking software from the final stage of testing to live production. You usually need to do this quickly in order to minimize downtime. The blue-green deployment approach does this by ensuring you have two production environments, as identical as possible. At any time one of them, let's say blue for the example, is live. As you prepare a new release of your software you do your final stage of testing in the green environment. Once the software is working in the green environment, you switch the router so that all incoming requests go to the green environment - the blue one is now idle.

Blue-green deployment also gives you a rapid way to rollback - if anything goes wrong you switch the router back to your blue environment.

我不知道你的后端在哪里 运行 但有一些服务可以为你做这件事,例如 AWS ElasticBeanstalk 会将你的实例放在负载均衡器后面并根据策略管理部署.看一看; https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.rolling-version-deploy.html.