【发布时间】:2018-06-11 12:51:13
【问题描述】:
我决定要研究使用 NodeJS 服务器处理大量流量的最佳方法是什么,我在 2 个具有 1GB RAM / 2 个 CPU 的数字海洋服务器上做了一个小测试 无集群服务器代码:
// Include Express
var express = require('express');
// Create a new Express application
var app = express();
// Add a basic route – index page
app.get('/', function (req, res) {
res.redirect('http://www.google.co.il');
});
// Bind to a port
app.listen(3000);
console.log('Application running');
集群服务器代码:
// Include the cluster module
var cluster = require('cluster');
// Code to run if we're in the master process
if (cluster.isMaster) {
// Count the machine's CPUs
var cpuCount = require('os').cpus().length;
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
// Code to run if we're in a worker process
} else {
// Include Express
var express = require('express');
// Create a new Express application
var app = express();
// Add a basic route – index page
app.get('/', function (req, res) {
res.redirect('http://www.walla.co.il');
});
// Bind to a port
app.listen(3001);
console.log('Application running #' + cluster.worker.id);
}
并且我向这些服务器发送了压力测试请求,我除了集群服务器将处理更多请求但它没有发生,两台服务器在相同的负载下崩溃,虽然集群上运行了 2 个节点服务和 1 个服务在非集群上。
现在我想知道为什么?我做错什么了吗?
也许是其他原因导致服务器到达断点?两台服务器都以约 800 rps 的速度崩溃
【问题讨论】:
-
是一共2个node进程还是一共3个node进程?,因为包括master,cluser中需要3个node进程
-
是的,这是 3 个过程 :) 我在事实方面比较懒散
标签: javascript node.js express server node-cluster