首页 > 解决方案 > 请求未分布在其工作进程中

问题描述

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

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

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

标签: node.jschild-processnode-worker-threads

解决方案


如果你想使用 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

在此处输入图像描述


推荐阅读