简体   繁体   中英

How to achieve 100 % uptime for the server using cluster for handling the load and express for server configration

I want to create a server using cluster. I have quadcore(4 cpu).

I want to make one as master for switching the request / response among three worker server. When a worker throws an exception or dies it should restart meanwhile other worker will be still running and when the worker will be restarting itself master should not route any request to it.

So that 100 % uptime can be achieved for the server.

I had tried following but it not fulfilling my requirement

const cluster = require('cluster');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
    // Fork workers.
    for (var i = 0; i < numCPUs; i++) {
        cluster.fork();
    }

    cluster.on('exit', (worker, code, signal) => {
        console.log(`worker ${worker.process.pid} died`);
    });
} else {
    var express = require(NODE_PATH + 'express'),
        app = express();

    var addApiList = require("./api_routes/apiList");
    addApiList(app);

    app.listen(8085, function() {
        console.log('listening on port : ' + 8085);
    });

}

The cluster master needs to create a new worker to take the dead worker's place, this process isn't automatic.

cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`);
    cluster.fork();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM