如何在多台机器上集群节点应用程序 [英] How to cluster Node app in multiple machines

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

问题描述

我正在使用 Express js Node-cluster ,以利用集群我也使用 PM2 进程和内存管理。对于单个机器,它工作正常,但我的机器有2个内核,我想提供更多的内核。所以我决定加入3台机器,现在所有4台机器都使用局域网连接。我也可以使用Web浏览器中的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.

推荐答案

节点集群对于利用多核处理器是有好处的,但是当涉及到水平缩放(增加更多机器),您将需要使用负载平衡器或反向代理。对于反向代理,您可以使用任何Web服务器,如Apache或nginx。如果要依赖节点和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代理模块。

新版本:


npm install --save http-proxy

npm install --save http-proxy

版本:


npm install --save http-proxy@0.8

npm install --save http-proxy@0.8




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

对于版本1.xx(新)

For version 1.x.x (New)

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);

for 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. 对本地主机的请求:8080将被路由到8081,8082或8083

  3. 您可以将本地主机更改为计算机的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.

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

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