Node.js supports multiple load balance across servers?

前端 未结 1 500
长情又很酷
长情又很酷 2020-12-17 22:52

Im curious about horizontal scalling in node.js is possible to load balance acrross multiple virtual servers like rackspace cloud servers? I read about cluster plugin but

相关标签:
1条回答
  • 2020-12-17 23:33

    Try roundrobin.js for node-http-proxy:

    var httpProxy = require('http-proxy');
    //
    // A simple round-robin load balancing strategy.
    //
    // First, list the servers you want to use in your rotation.
    //
    var addresses = [
      {
        host: 'ws1.0.0.0',
        port: 80
      },
      {
        host: 'ws2.0.0.0',
        port: 80
      }
    ];
    
    httpProxy.createServer(function (req, res, proxy) {
      //
      // On each request, get the first location from the list...
      //
      var target = addresses.shift();
    
      //
      // ...then proxy to the server whose 'turn' it is...
      //
      proxy.proxyRequest(req, res, target);
    
      //
      // ...and then the server you just used becomes the last item in the list.
      //
      addresses.push(target);
    });
    
    // Rinse; repeat; enjoy.
    
    0 讨论(0)
提交回复
热议问题