Node.js支持跨服务器的多个负载平衡?

Chr*_*ian 8 load-balancing node.js

我很好奇node.js中的水平scalling是否可以在机架空间云服务器等多个虚拟服务器上进行负载均衡?我读到了关于集群插件但我认为它只适用于具有多核cpu的单个服务器.

pse*_*ma4 10

尝试roundrobin.js节点HTTP代理:

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.
Run Code Online (Sandbox Code Playgroud)

  • 此实现是代理连接还是将客户端连接传递给恰好是下一个的服务器?我想在使用websockets和持久连接的上下文中.如果所有连接都通过负载均衡器汇集,那么您仍然存在瓶颈,因为负载均衡器会使连接饱和. (5认同)