6

我只是在试验工作进程,因此试试这个:

const http = require("http");
const cluster = require("cluster");
const CPUs = require("os").cpus();
const numCPUs = CPUs.length;

if (cluster.isMaster) {
  console.log("This is the master process: ", process.pid);
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
  cluster.on("exit", (worker) => {
    console.log(`worker process ${process.pid} has died`);
    console.log(`Only ${Object.keys(cluster.workers).length} remaining...`);
  });
} else {
  http
    .createServer((req, res) => {
      res.end(`process: ${process.pid}`);
      if (req.url === "/kill") {
        process.exit();
      }
      console.log(`serving from ${process.pid}`);
    })
    .listen(3000);
}

我使用loadtest来检查“请求是否分布在他们的工作进程中?” 但我也一样process.pid

This is the master process:  6984
serving from 13108
serving from 13108
serving from 13108
serving from 13108
serving from 13108
...

即使我杀了他们中的一个,我也会得到同样的结果process.pid

worker process 6984 has died
Only 3 remaining...
serving from 5636
worker process 6984 has died
Only 2 remaining...
worker process 6984 has died
Only 1 remaining...

process.pid当我杀死它时,我怎么变得一样了?为什么我的请求没有分布在他们的工作进程中?

即使我使用pm2来测试集群情绪,使用:

$ pm2 start app.js -i 3
[PM2] Starting app.js in cluster_mode (3 instances)
[PM2] Done.
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 0  │ app                │ cluster  │ 0    │ online    │ 0%       │ 31.9mb   │
│ 1  │ app                │ cluster  │ 0    │ online    │ 0%       │ 31.8mb   │
│ 2  │ app                │ cluster  │ 0    │ online    │ 0%       │ 31.8mb   │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘

因为loadtest -n 50000 http://localhost:3000我检查了 pm2 监视器:

$ pm2 monit

┌─ Process List ───────────────────────────────────────────────────┐┌──  app Logs  ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ 
│[ 0] app                         Mem:  43 MB    CPU: 34 %  online ││                                                                                                                                                              │ 
│[ 1] app                         Mem:  28 MB    CPU:  0 %  online ││                                                                                                                                                              │ 
│[ 2] app                         Mem:  27 MB    CPU:  0 %  online ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
└──────────────────────────────────────────────────────────────────┘└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ 
┌─ Custom Metrics ─────────────────────────────────────────────────┐┌─ Metadata ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ 
│ Heap Size                                             20.81 MiB  ││ App Name              app                                                                                                                                    │ 
│ Heap Usage                                              45.62 %  ││ Namespace             default                                                                                                                                │ 
│ Used Heap Size                                         9.49 MiB  ││ Version               N/A                                                                                                                                    │ 
│ Active requests                                               0  ││ Restarts              0                                                                                                                                      │ 
└──────────────────────────────────────────────────────────────────┘└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ 
 left/right: switch boards | up/down/mouse: scroll | Ctrl-C: exit                                                                                                                           To go further check out https://pm2.io/

但令人惊讶的是,app1 和 app2 从未收到任何请求,也没有显示任何应用程序日志。

更新 1

我仍然想不出任何解决方案。如果有任何进一步的查询需要,请询问。我第一次遇到这个问题。这就是为什么我可能无法表示确切的问题发生在哪里。

更新 2

得到一些答案后,我尝试用一​​个简单的节点服务器再次测试它:

在没有任何配置的情况下使用 pm2:

gif1

使用@Naor Tedgi 的回答中建议的配置:

gif1

现在服务器根本没有运行。

4

3 回答 3

0

如果你想使用 pm2 作为负载均衡器,你没有启用集群模式,你需要添加 exec_mode cluster

添加这个配置文件名config.js

module.exports = {
  apps : [{
    script    : "app.js",
    instances : "max",
    exec_mode : "cluster"
  }]
}

并运行pm2 start config.js

那么所有的 CPU 使用率将平分

在 osmacOS Catalina 10.15.7 节点上品尝v14.15.4

在此处输入图像描述

于 2021-01-28T10:07:49.177 回答
0

我懂了

在此处输入图像描述

可能与操作系统有关,我在 Ubuntu 20.04 上。

于 2021-01-28T10:01:38.960 回答
0

不知道为什么,但似乎无论出于何种原因,集群在您的机器上的行为都不像它应该的那样。

代替使用 node.js 进行平衡,您可以使用 nginx。在 nginx 部分,如果其中一种可用策略对您来说足够了,那就相当容易了:http: //nginx.org/en/docs/http/load_balancing.html

然后,您需要确保为您的节点进程分配了不同的端口。在 pm2 中,您可以使用https://pm2.keymetrics.io/docs/usage/environment/根据实例 ID 手动增加端口或将其完全委托给 pm2。

不用说,在这种情况下,您必须将请求发送到 nginx。

于 2021-01-30T06:13:56.193 回答