Node.js on multi-core machines

前端 未结 15 1274
猫巷女王i
猫巷女王i 2020-11-22 07:46

Node.js looks interesting, BUT I must miss something - isn\'t Node.js tuned only to run on a single process and thread?

Then how does it scale for m

相关标签:
15条回答
  • 2020-11-22 08:09

    Spark2 is based on Spark which is now no longer maintained. Cluster is its successor, and it has some cool features, like spawning one worker process per CPU core and respawning dead workers.

    0 讨论(0)
  • 2020-11-22 08:10

    The cluster module allows you to utilise all cores of your machine. In fact you can take advantage of this in just 2 commands and without touching your code using a very popular process manager pm2.

    npm i -g pm2
    pm2 start app.js -i max
    
    0 讨论(0)
  • 2020-11-22 08:14

    Node Js is supporting clustering to take full advantages of your cpu. If you are not not running it with cluster, then probably you are wasting your hardware capabilities.

    Clustering in Node.js allows you to create separate processes which can share same server port. For example, if we run one HTTP server on Port 3000, it is one Server running on Single thread on single core of processor.

    Code shown below allow you to cluster your application. This code is official code represented by Node.js.

    var cluster = require('cluster');
    var numCPUs = require('os').cpus().length;
    
    if (cluster.isMaster) {
        // Fork workers.
        for (var i = 0; i < numCPUs; i++) {
            cluster.fork();
        }
    
        Object.keys(cluster.workers).forEach(function(id) {
            console.log("I am running with ID : " + cluster.workers[id].process.pid);
        });
    
        cluster.on('exit', function(worker, code, signal) {
            console.log('worker ' + worker.process.pid + ' died');
        });
    } else {
    
        //Do further processing.
    }
    

    check this article for the full tutorial

    0 讨论(0)
  • 2020-11-22 08:15

    I'm using Node worker to run processes in a simple way from my main process. Seems to be working great while we wait for the official way to come around.

    0 讨论(0)
  • 2020-11-22 08:15

    It's possible to scale NodeJS out to multiple boxes using a pure TCP load balancer (HAProxy) in front of multiple boxes running one NodeJS process each.

    If you then have some common knowledge to share between all instances you could use a central Redis store or similar which can then be accessed from all process instances (e.g. from all boxes)

    0 讨论(0)
  • 2020-11-22 08:16

    Ryan Dahl answers this question in the tech talk he gave at Google last summer. To paraphrase, "just run multiple node processes and use something sensible to allow them to communicate. e.g. sendmsg()-style IPC or traditional RPC".

    If you want to get your hands dirty right away, check out the spark2 Forever module. It makes spawning multiple node processes trivially easy. It handles setting up port sharing, so they can each accept connections to the same port, and also auto-respawning if you want to make sure a process is restarted if/when it dies.

    UPDATE - 10/11/11: Consensus in the node community seems to be that Cluster is now the preferred module for managing multiple node instances per machine. Forever is also worth a look.

    0 讨论(0)
提交回复
热议问题