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