负载平衡与节点和Heroku [英] Load Balancing with Node and Heroku

查看:91
本文介绍了负载平衡与节点和Heroku的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接受来自ios应用程序的api请求的web应用程序。我的网站应用程序使用Heroku免费的dyno托管在Heroku上,每个请求可以处理512 MB的数据。由于节点是单线程应用程序,一旦我们开始从ios端到Web服务器获得更高级别的流量,这将会成为问题。我也不是世界上最富有的人,所以我想知道创建另一个免费的heroku应用程序并使用循环法来平衡从ios应用程序收到的负载是否明智?

我只需要指出正确的方向。垂直缩放在经济上不是一个真正的选择。

解决方案

正如丹尼尔所说,它违背了Heroku的规则。说了大概有其他服务可以让你这样做。
解决此问题的一种方法是使用带有 ZeroMQ 的集群模块(您需要在使用之前安装ZeroMQ该模块 - 请参阅模块描述)。

  var cluster = require('cluster'); 
var zmq = require('zmq');

var ROUTER_SOCKET ='tcp://127.0.0.1:5555';
var DEALER_SOCKET ='tcp://127.0.0.1:7777';

if(cluster.isMaster){
//这是主要过程 - 创建路由器和代理套接字
var router = zmq.socket('router')。bind ROUTER_SOCKET);
var dealer = zmq.socket('dealer')。bind(DEALER_SOCKET);

//在路由器和经销商之间转发消息
router.on('message',function(){
var frames = Array.prototype.slice.cal(arguments);
dealer.send(frames);
});
$ b dealer.on('message',function(){
var frames = Array.prototype.slice.cal(arguments);
router.send(frames);
});

//监听工作进程上线
cluster.on('online',function(){
//对新工作人员进行操作,可能会保留一个数组的工人
});

// fork工作进程
for(var i = 0,i <100; i ++){
cluster.fork();
}
} else {
//工作进程 - 连接到经销商
let responder = zmq.socket('rep')。connect(DEALER_SOCKET);
$ b responder.on('message',function(data){
//做一些处理数据
})
}

这只是为了指引您正确的方向。如果你仔细想想,你可以用一个参数来创建一个脚本,它会告诉它它是一个主进程还是一个工作进程。然后在主服务器上运行它,并在其他服务器上使用worker标志运行它,这将强制它连接到主要经销商。



现在您的主应用程序需要将请求发送到路由器,后者将被转发给工作进程: );
var requester = zmq.socket('req');

var ROUTER_SOCKET ='tcp://127.0.0.1:5555';

//处理回复 - 例如来自工作进程的完成状态
requester.on('message',function(data){
//对重播做些事
});

requester.connect(ROUTER_SOCKET);

//向路由器发送请求
requester.send({
//描述任务的某个对象
});


I have a web app that accepts api requests from an ios app. My web app is hosted on Heroku using their free dyno which is able to process 512 mb of data per request. Because node is a single threaded application this will be a problem once we start getting higher levels of traffic from the ios end to the web server. I'm also not the richest person in the world so i'm wondering if it would be smart to create another free heroku app and use a round robin approach to balance the load received from the ios app?

I just need to be pointed into the right direction. Vertical scaling is not really an option financially.

解决方案

As mentioned by Daniel it's against Heroku rules. Having said that there are probably other services that would allow you to do that. One way to approach this problem is to use cluster module with ZeroMQ (you need to have ZeroMQ installed before using the module - see module description).

var cluster = require('cluster');
var zmq = require('zmq');

var ROUTER_SOCKET = 'tcp://127.0.0.1:5555';
var DEALER_SOCKET = 'tcp://127.0.0.1:7777';

if (cluster.isMaster) {
  // this is the main process - create Router and Dealer sockets
  var router = zmq.socket('router').bind(ROUTER_SOCKET);
  var dealer = zmq.socket('dealer').bind(DEALER_SOCKET);

  // forward messages between router and dealer
  router.on('message', function() {
    var frames = Array.prototype.slice.cal(arguments);
    dealer.send(frames);
  });

  dealer.on('message', function() {
    var frames = Array.prototype.slice.cal(arguments);
    router.send(frames);
  });

  // listen for workers processes to come online
  cluster.on('online', function() {
    // do something with a new worker, maybe keep an array of workers
  });

  // fork worker processes
  for (var i = 0, i < 100; i++) {
    cluster.fork();
  }
} else {
  // worker process - connect to Dealer
  let responder = zmq.socket('rep').connect(DEALER_SOCKET);

  responder.on('message', function(data) {
    // do something with incomming data
  })
}

This is just to point you in the right direction. If you think about it you can create a script with a parameter that will tell it if it's a master or a worker process. Then on the main server run it as is, and on additional servers run it using worker flag which will force it to connect to the main dealer.

Now your main app needs to send the requests to the router, which will be later forwarded to the worker processes:

var zmq = require('zmq');
var requester = zmq.socket('req');

var ROUTER_SOCKET = 'tcp://127.0.0.1:5555';

// handle replies - for example completion status from the worker processes
requester.on('message', function(data) {
  // do something with the replay
});

requester.connect(ROUTER_SOCKET);

// send requests to the router
requester.send({
  // some object describing the task
});

这篇关于负载平衡与节点和Heroku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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