简体   繁体   English

如何更新正在运行的Node.js服务器?

[英]How to update running Node.js servers?

As far as I understand, a Node.js server is basically a JavaScript file, run by the node executable. 据我所知,Node.js服务器基本上是一个JavaScript文件,由node可执行文件运行。 Is it possible to update this file without stopping request handling? 是否可以在不停止请求处理的情况下更新此文件?

I guess the node executable must be stopped for this, so I think I should use a reverse proxy. 我想必须为此停止节点可执行文件,所以我想我应该使用反向代理。 For example: 例如:

  1. Original server version listens on localhost:50001 原始服务器版本侦听localhost:50001
  2. Proxy server listens to requests on webinterface:80 and forwards them to localhost:50001 代理服务器侦听webinterface上的请求:80并将它们转发到localhost:50001
  3. New server version should be started on localhost:50002 应在localhost:50002上启动新的服务器版本
  4. Proxy server forwarding target should be changed to localhost:50002 代理服务器转发目标应更改为localhost:50002
  5. Original server version should be stopped 应停止原始服务器版本

Is this a valid approach? 这是一种有效的方法吗? How can such a version update be done automatically on multiple server machines (accessible from the same LAN)? 如何在多台服务器计算机上自动完成此类版本更新(可从同一LAN访问)?

A different "pure-node" solution is to use the built-in cluster module: 一个不同的“纯节点”解决方案是使用内置的集群模块:

Your code runs as one cluster-client (of many). 您的代码作为一个集群客户端运行(许多)。 A cluster-server process binds to the port and does automatic load-balancing between clients. 集群服务器进程绑定到端口,并在客户端之间进行自动负载平衡。 When you've changed the code, you can send a signal to the cluster-server and it will gracefully restart your clients, without dropping any existing connections. 更改代码后,您可以向群集服务器发送信号,它将正常地重新启动客户端,而不会丢弃任何现有连接。

Here are some projects that can do the cluster-server management for you: 以下是一些可以为您执行集群服务器管理的项目:

Advantages: 好处:

  • Fewer components in your stack. 堆栈中的组件更少。 (Easier to deploy / manage.) (更易于部署/管理。)
  • Can automatically start a node process for each core on the CPU, so your program can fully leverage the underlying hardware. 可以为CPU上的每个核心自动启动节点进程,因此您的程序可以充分利用底层硬件。
  • No need to learn nginx's config syntax 无需学习nginx的配置语法

Basically, what you can (should) do is the following: 基本上,你可以(应该)做的是以下内容:

  • Run a webserver such as Nginx as a reverse proxy 运行Nginx等Web服务器作为反向代理
  • Use at least two (!) Node.js instances behind it 在其后面至少使用两个(!)Node.js实例
  • Put the sessions in a database such as Redis 将会话放在Redis等数据库中

When you need to update your system, shut down one of the two Node.js instances, update it, and restart it. 当您需要更新系统时,请关闭其中一个Node.js实例,更新它并重新启动它。 Then do the same with the other. 然后对另一个做同样的事情。

Basically, this should do it. 基本上,这应该做到这一点。

The downside is that you are potentially running different versions of your application for a small amount of time concurrently, and your database (eg) needs to be able to catch up with this. 缺点是您可能会在很短的时间内同时运行不同版本的应用程序,并且您的数据库(例如)需要能够赶上这一点。

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

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