Node.js支持在多个服务器之间进行负载平衡吗?

8
我对Node.js中的水平扩展很感兴趣。是否可以跨多个虚拟服务器(如Rackspace云服务器)进行负载均衡?我了解到有群集插件,但我认为它仅适用于具备多核CPU的单个服务器。

2
nginx?=> http://www.shinstudio.com/blog/backend-tech/setting-up-node-js-in-nginx/ - Alfred
1
很容易,在Node中编写一个节点负载均衡代理。 - Raynos
1
但是对于实例连接呢,例如多个聊天室,如何将用户路由到特定的服务器,如果用户分布在不同的服务器上怎么办? - Christian
1个回答

10

尝试使用roundrobin.js来配合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.

5
这个实现是代理连接还是将客户端连接移交给下一个服务器?我在使用 WebSocket 并且保持持久连接的情况下想知道。如果所有连接都通过负载均衡器传输,那么仍然会存在瓶颈,因为负载均衡器会饱和处理连接。 - Jeff LaFay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接