NodeJS:如何将三台服务器作为一个应用程序运行?

发布于 2024-12-02 00:57:00 字数 441 浏览 0 评论 0原文

我的应用程序是由三台不同的服务器构建的:每台服务器都有不同的用途,并且它们必须保持分离(至少对于使用多个核心而言)。作为示例(这不是真实的),您可以将此设置视为管理用户身份验证的一台服务器,另一台充当游戏引擎,另一台充当 pubsub 服务器。从逻辑上讲,“应用程序”只是一个,客户端根据其特定需要连接到一台或另一台服务器。

现在我正在尝试找出在生产环境中运行此类设置的最佳方法。

最简单的方法可能是使用一个 bash 脚本,该脚本将在后台逐个运行每个服务器。这种方法的一个问题是,如果我需要重新启动“应用程序”,我应该保存每台服务器的 pid 并杀死每台服务器。

另一种方法是使用节点进程,该节点进程将每个服务器作为其自己的子进程运行(使用 child_process.spawn)。节点生成节点。出于某种原因这很愚蠢吗?这样,当我需要停止/重新启动整个应用程序时,我就可以终止一个进程。

你怎么认为?

My application is built with three distincts servers: each one of them serves a different purpose and they must stay separated (at least, for using more than one core). As an example (this is not the real thing) you could think about this set up as one server managing user authentication, another one serving as the game engine, another one as a pubsub server. Logically the "application" is only one and clients connect to one or another server depending on their specific need.

Now I'm trying to figure out the best way to run a setup like this in a production environment.

The simplest way could be to have a bash script that would run each server in background one after the other. One problem with this approach would be that in the case I need to restart the "application", I should have saved each server's pid and kill each one.

Another way would be to use a node process that would run each servers as its own child (using child_process.spawn). Node spawning nodes. Is that stupid for some reason? This way I'd have a single process to kill when I need to stop/restart the whole application.

What do you think?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜司空 2024-12-09 00:57:00

如果您使用的是 Linux 或其他 *nix 操作系统,您可以尝试编写启动/停止/重新启动应用程序的 init 脚本。 这是一个示例。

If you're on Linux or another *nix OS, you might try writing an init script that start/stop/restart your application. here's an example.

漫漫岁月 2024-12-09 00:57:00

使用特定工具进行过程监控。例如,Monit 可以通过 pid 监视您的进程,并在它们死掉时重新启动它们,并且您可以使用 monit-cmd 或它们的 web-gui 手动重新启动每个进程。

因此,在您的示例中,您将创建 3 个独立的进程并告诉 monit 监视每个进程。

Use specific tools for process monitoring. Monit for example can monitor your processes by their pid and restart them whenever they die, and you can manually restart each process with the monit-cmd or with their web-gui.

So in your example you would create 3 independent processes and tell monit to monitor each of them.

染年凉城似染瑾 2024-12-09 00:57:00

我最终在 Node 中创建了一个包装器/管理程序脚本,它使用 child_process.spawn 来执行所有三个进程。

  • 它将每个进程的 stdout/stderr 通过管道传输到它的 stdout/stderr
  • 它拦截每个进程的错误,记录它们,然后退出(因为这是它的错误)
  • 然后它分叉并守护自身

我可以使用启动/停止范例来停止整个事情。

现在我有了一个强大的守护进程,我可以创建一个 unix 脚本来像往常一样在启动/关闭时启动/停止它(如 @Levi 所说)

另请参阅我的其他(相关)问题: NodeJS:此代码是否可以多核运行?

I ended up creating a wrapper/supervisor script in Node that uses child_process.spawn to execute all three processes.

  • it pipes each process stdout/stderr to the its stdout/stderr
  • it intercepts errors of each process, logs them, then exit (as it were its fault)
  • It then forks and daemonize itself

I can stop the whole thing using the start/stop paradigm.

Now that I have a robust daemon, I can create a unix script to start/stop it on boot/shutdown as usual (as @Levi says)

See also my other (related) Q: NodeJS: will this code run multi-core or not?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文