是否所有 AWS Node.js 应用程序都需要集群模块?
Do all AWS Node.js apps require the cluster module?
我正在学习教程 here, and noticed the index file (app.js) of the example code 与通常的 Express.js 应用程序完全不同。代码被包裹在集群模块中,如下所示:
// 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();
}
// Listen for terminating workers
cluster.on('exit', function (worker) {
// Replace the terminated workers
console.log('Worker ' + worker.id + ' died :(');
cluster.fork();
});
// Code to run if we're in a worker process
} else {
var AWS = require('aws-sdk');
var express = require('express');
var bodyParser = require('body-parser');
// the usual code ..
}
这是必要的吗,或者我可以只部署没有集群模块的常规代码,就像这样:
var AWS = require('aws-sdk');
var express = require('express');
var bodyParser = require('body-parser');
// the usual code ..
谢谢,
没必要。集群模块是一种更好地利用机器(虚拟的或物理的)处理能力的方法,但并不一定只为了 运行 在 AWS 上。
我正在学习教程 here, and noticed the index file (app.js) of the example code 与通常的 Express.js 应用程序完全不同。代码被包裹在集群模块中,如下所示:
// 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();
}
// Listen for terminating workers
cluster.on('exit', function (worker) {
// Replace the terminated workers
console.log('Worker ' + worker.id + ' died :(');
cluster.fork();
});
// Code to run if we're in a worker process
} else {
var AWS = require('aws-sdk');
var express = require('express');
var bodyParser = require('body-parser');
// the usual code ..
}
这是必要的吗,或者我可以只部署没有集群模块的常规代码,就像这样:
var AWS = require('aws-sdk');
var express = require('express');
var bodyParser = require('body-parser');
// the usual code ..
谢谢,
没必要。集群模块是一种更好地利用机器(虚拟的或物理的)处理能力的方法,但并不一定只为了 运行 在 AWS 上。