【问题标题】:pm2 list clusters and used/available CPUspm2 列出集群和使用/可用的 CPU
【发布时间】:2021-02-06 15:00:39
【问题描述】:

我刚开始使用 pm2 并希望已经使用集群模式,因为我将在我的 VPS 上管理多个 nodejs 应用程序。

文档含糊不清,monit/monitor 函数未显示使用的 CPU。 我的 VPS 运行的是 Intel Xeon E5-2620 v3 @ 2.4ghz x 4 cores。

出于测试目的,我使用 1 个 CPU 启动了 Node 服务。 它只显示 1 个进程。

当我停止它并重新运行 pm2 start index.js -i 0 时,它仍然显示 1 个进程。

当我删除所有进程并运行 pm2 start index.js -i 0 时,它现在显示 4 个进程。

如果要重构每个应用程序的可用核心数,是否只能通过删除进程来实现? 是通过进程名称区分应用程序的唯一方法吗?因为它只是将它们全部复制并使用index 作为名称。

对不起,如果这令人困惑。公开接受编辑。

【问题讨论】:

    标签: node.js pm2


    【解决方案1】:

    这似乎是正确的,因为 pm2 start -i 0 index.js 与 pm2 start -i max index.js 相同,后者会将集群分散到所有 CPU。

    进程 ID 是区分它们的好方法。您还可以使用名称 (-n) 向应用程序添加身份。使用以下命令,您应该能够根据需要启动、删除和重新加载集群进程。你甚至可以在开发中使用 --watch,这样你就不需要手动重启集群了。

    pm2 start -n webserver -i 4 index.js --watch
    

    添加一个进程或3(需要-f):

    pm2 start -n webserver -f -i 1 index.js
    
    pm2 start -n webserver -f -i 3 index.js
    

    对正在运行的进程进行分类:

    pm2 ls
    

    要删除一个使用 ID 号:

    pm2 del 2
    

    要重新加载一个或全部:

    pm2 reload 1
    
    pm2 reload all
    

    要以编程方式访问集群中的实例编号,请使用NODE_APP_INSTANCE:

    const app = require('express')()
    const PORT = 6767
    
    app.all('/*', (req, res) => {
      const { NODE_APP_INSTANCE } = process.env
      res.send(`Sent from instance ${NODE_APP_INSTANCE}`)
    })
    
    app.listen(PORT, console.log('LISTENING TO PORT', PORT))
    
    

    如果您正在运行一些复杂的东西,您应该考虑使用生态系统文件。检查以下来源。

    来源:

    https://pm2.keymetrics.io/docs/usage/application-declaration/

    https://pm2.io/docs/runtime/guide/load-balancing/#cluster-environment-variable

    https://pm2.keymetrics.io/docs/usage/cluster-mode/

    https://pm2.keymetrics.io/docs/usage/watch-and-restart/

    【讨论】:

      猜你喜欢
      • 2021-03-28
      • 1970-01-01
      • 2016-10-15
      • 1970-01-01
      • 2021-07-29
      • 1970-01-01
      • 2017-08-25
      • 1970-01-01
      • 2017-10-08
      相关资源
      最近更新 更多