【问题标题】:Do all AWS Node.js apps require the cluster module?所有 AWS Node.js 应用程序都需要集群模块吗?
【发布时间】:2017-11-04 19:07:39
【问题描述】:

我在学习 here 的教程时,注意到 example code 的索引文件 (app.js) 与通常的 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 ..

谢谢,

【问题讨论】:

    标签: javascript node.js amazon-web-services cluster-computing amazon-elastic-beanstalk


    【解决方案1】:

    没必要。集群模块是一种更好地利用机器(虚拟或物理)处理能力的方法,但并非只需要在 AWS 上运行。

    【讨论】:

    • 太好了,所以当我将第二个示例捆绑为 zip 文件时,它会起作用,对吧?
    • 假设你已经运行了节点并执行 npm install 和所有常见的东西,是的。
    • 我可以做这些事情并在本地运行我的应用程序,但我不知道如何在 AWS 中做这些事情。无论如何,希望我能弄清楚:)谢谢! P.S.: 只需使用 EB 控制台上传我的常规代码即可,我什至不需要 npm install。我猜 AWS 会自动执行此操作。
    • 您之前没有提到您使用的是 Elastic Beanstalk。 EB 是 AWS 对所谓的平台即服务的实现,这意味着它将为您提供应用程序代码的所有基础设施,并根据您的应用程序类型运行一些标准流程(在这种情况下,运行 npm install 作为 post-部署步骤)。
    • 哦,我明白了。所以我可以跳过 EB,使用 EC2 并自己运行这些命令吗?好的,现在我明白了 EB 的意义了 :)
    猜你喜欢
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 2018-08-25
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 2022-11-05
    • 1970-01-01
    相关资源
    最近更新 更多