简体   繁体   中英

Is this a good server's load balancing system?

I'm new to this concept and I'm thinking on how to horizontally scale my Xepler Node.js framework.

So, the main app on the master server will proxy the request to the first cluster in a queue (maybe retrieved using a shared-memory with redis). Each X requests (depending on the server capabilities, X is decided by me, maybe using a map), the cluster will be moved to the last place in this queue. In this way, all the clusters will receive only a reduced number of requests.

Another app, on another server, each X seconds will make a request to all the clusters in order to check if someone is failed, removing it from the queue (this queue will be on redis?)

All the clusters normally will run an instance of my web framework.

Is for you this a good system of load balancing, or have I completely misunderstanding how works? Thank you guys

edit: that's what I mean (only an example):

var http = require('http'),
     https = require('https'),
    httpProxy = require('http-proxy'),
    proxy = httpProxy.createProxyServer({}),

     clusters = [
        {
            id: 1,
            host: "localhost",
            port: 8080,
            dead : false,
            deadTime : undefined
        },      
        {
            id: 2,
            host: "localhost",
            port: 8081,
            dead : false,
            deadTime : undefined
        }
     ];

http.createServer(function(req, res) {
    var target = getAvailableCluster();

    if (target != -1) {
        proxy.web(req, res, { target: 'http://' + target.host + ':' + target.port });

        res.setTimeout(1e3 * 20, function() {
            target.dead = true;
            target.deadTime = new Date().getTime();
            console.log("Cluster " + target.id + " is dead");
        });
    }   
}).listen(80, function() {
    console.log('Proxy listening on port 80..');
});

proxy.on('error', function (error, req, res) {
    var json;
    console.log('proxy error', error);

    if (!res.headersSent)
        res.writeHead(500, { 'content-type': 'application/json' });

    json = { error: 'proxy_error', reason: error.message };
    res.end(JSON.stringify(json));
});

setInterval(function() {
    var cluster,
        currentTime = new Date().getTime();

    for (var i=0; i<clusters.length; i++) {
        cluster = clusters[i];

        if (cluster.dead && (currentTime - cluster.deadTime) > 1000) {
            cluster.dead = false;
            console.log("Cluster " + cluster.id + " is now alive");
        }
    }   
}, 5000);

function getAvailableCluster() {
    var cluster;

    for (var i=0; i<clusters.length; i++) {
        cluster = clusters.shift();
        clusters.push(cluster);

        if (!cluster.dead)      
            return cluster;
    }

    return -1;
}

Why are you re-inventing the wheel? There is hipache a reverse proxy/ load balancer which has all the features you need as far as I can see.

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