node.js - 请求未分布在其工作进程中
问题描述
我只是在试验工作进程,因此试试这个:
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:
使用@Naor Tedgi 的回答中建议的配置:
现在服务器根本没有运行。
解决方案
推荐阅读
- c++ - 在这个简单的例子中,为什么 std::optional 的赋值运算符不能在编译时上下文中使用?
- apache - 查找 +php-osx.conf 时遇到问题
- r - 如何在 tmaps 标签中添加与值组合的标签
- python - Flask 和 SQLAlchemy - 错误绑定参数
- jdbc - Kafka Connect:使用 JDBC Sink Connector 展平 ARRAY 记录
- javascript - 带有 XMLHttpRequest() 的 formData POST 请求不起作用,但在邮递员中工作
- python - 列表索引超出范围python OpenCV?
- macos - 如何将图标添加到 Launchpad 以用于位于我的捆绑包资源文件夹 (macOS Big Sur) 中的辅助应用程序?
- sql-server - EF Core 如何在 EF Core 中实现一次读取操作的脏读?
- google-cloud-platform - 如何以编程方式从 GCP 市场创建 WURFL 服务?