如何在多台机器上集群Node.js应用 [英] How to cluster Node.js app in multiple machines

查看:265
本文介绍了如何在多台机器上集群Node.js应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Express js Node-cluster 以利用群集优势,我也在使用 PM2 用于进程和内存管理。对于单台机器,它可以正常工作,但是我的机器具有2个核心,并且我想提供更多核心。因此,我决定再加入3台计算机,现在所有4台计算机都使用LAN连接。我也可以在网络浏览器中使用IP地址访问其他机器。

I am using Express js and Node-cluster for taking the advantage of clustering I am also using PM2 for process and memory management. For a single machine, it is working fine, but my machine having 2 cores and I want to make available more cores. So I decided to join 3 more machines and now all 4 machines are connected using LAN. I am able to access the other machines using IP address in web browser also.

现在,我想连接所有机器并希望共享它们的内核,这样我的应用程序最终将拥有2 + 6 = 8个内核。怎么可能呢?是否有可用的节点模块来实现这一目标?谢谢。

Now I want to connect all the machines and want to share their cores so that I will finally have 2 + 6 = 8 cores for my application. How can it possible? Is there any node module available to achieve this? Thanks.

推荐答案

Node-cluster很好地利用了多核处理器,但是在水平扩展方面(添加更多内容)机器),则需要使用负载平衡器或反向代理。对于反向代理,您可以使用任何Web服务器,例如Apache或nginx。如果您要依赖node和npm,则可以使用 nodejitsu 的模块:http-proxy。这是一个用于运行您的节点应用程序的3台计算机的http代理的示例。

Node-cluster is good for taking advantage of multi core processors, but when it comes to horizontal scaling(adding more machines), you'll need to use load balancers or reverse proxy. For reverse proxy you can use any web server like Apache or nginx. If you want to rely on node and npm, there is a module by nodejitsu: http-proxy. Here is an example for http proxy for 3 machines running your node app.


  1. 创建一个新的节点项目。

  2. 安装http-proxy模块。

新版本:


npm install-保存http-proxy

npm install --save http-proxy

如果您喜欢旧的版本:


npm install-保存http-proxy@0.8

npm install --save http-proxy@0.8




  1. 创建一个新的js文件(server.js或您喜欢的任何文件)。

对于1.xx版(新)

server.js

server.js

var http = require('http'),
httpProxy = require('http-proxy');

var addresses = [
  {
    host: "localhost",
    port: 8081
  },
  {
    host: "localhost",
    port: 8082
  },
  {
    host: "localhost",
    port: 8083
  }
];

//Create a set of proxy servers
var proxyServers = addresses.map(function (target) {
  return new httpProxy.createProxyServer({
    target: target
  });
});

var server = http.createServer(function (req, res) {
  var proxy = proxyServers.shift();

  proxy.web(req, res);

  proxyServers.push(proxy);
});

server.listen(8080);

对于版本0.xx(旧)

for version 0.x.x (Old)

server.js

server.js

var proxyServer = require('http-proxy');

var servers = [
  {
    host: "localhost",
    port: 8081
  },
  {
    host: "localhost",
    port: 8082
  },
  {
    host: "localhost",
    port: 8083
  }
];

proxyServer.createServer(function (req, res, proxy) {
  var target = servers.shift();

  proxy.proxyRequest(req, res, target);
  servers.push(target);
}).listen(8080);




  1. 现在运行此文件。

  2. 对localhost:8080的请求将被路由到8081、8082或8083

  3. 您可以将localhost更改为计算机的IP地址(和端口号)。

向8080端口发出请求的客户端不知道8081、8082和8083服务器的存在。他们向8080发出请求的方式就像是唯一的服务器并从中获得响应。

Clients making request to 8080 port are unaware of existence of servers at 8081, 8082 and 8083. They make requests to 8080 as if it is the only server and get response from it.

现在,群集中的一台计算机将用作节点平衡器,而应用程序则托管在其他三台计算机上。负载均衡器的IP地址可以用作公用IP。

Now, one of the machines in your cluster will work as node balancer and application is hosted on other three machines. IP address of load balancer can be used as public IP.

这篇关于如何在多台机器上集群Node.js应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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