Node.js是否支持跨服务器的多个负载平衡? [英] Node.js supports multiple load balance across servers?

查看:98
本文介绍了Node.js是否支持跨服务器的多个负载平衡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对node.js中的水平缩放感到好奇 是否可以在多个虚拟服务器(如机架空间云服务器)之间实现负载平衡? 我读过有关集群插件的信息,但我认为它仅适用于具有多核cpu的单个服务器.

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 I think it is only for a single server with a multi core cpu.

推荐答案

对于节点,请尝试roundrobin.js -http-代理:

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.

这篇关于Node.js是否支持跨服务器的多个负载平衡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆