简体   繁体   English

Nodejs中的负载平衡

[英]Load Balancing in Nodejs

I recently started with node and I have been reading a lot about its limitation of it being single threaded and how it does not utilise your cores and then I read this 我最近开始使用node,我已经阅读了很多关于它是单线程的限制以及它如何不利用你的内核然后我读了这个

http://bit.ly/1n2YW68 (which talk about the new cluster module of nodejs for loadbalancing) http://bit.ly/1n2YW68 (谈论用于负载均衡的nodejs的新集群模块)

Now I'm not sure I completely agree to it :) because the first thing that I thought of before starting with node on how to make it utilise cores with proper load balancing is via web-server some like upstream module like nginx 现在我不确定我是否完全赞同它:)因为在开始使用节点之前我想到的第一件事就是如何利用具有适当负载平衡的内核来通过web-server,就像nginx这样的上游模块

like doing something like this 喜欢做这样的事情

 upstream domain1 {
   server http://nodeapp1;
   server http://nodeapp2;
   server http://nodeapp3;
 }

So my question is there an advantage to use such cluster module for load balancing to utilise the cores does it has any significant advantage over web server load balancing or is blog post too far from real use. 所以我的问题是使用这样的集群模块进行负载平衡以利用核心有一个优势它是否具有优于Web服务器负载平衡的任何显着优势,或者是博客文章与实际使用相距甚远。

Note : I'm ain't concerned about load balancing handle by various app server like passenger(passenger has nodejs support as well but something that I'm not looking for answer :)) which I already know since I'm mostly a ruby programmer 注意 :我不关心各种应用服务器(如乘客)的负载均衡处理(乘客也有nodejs支持,但我不想回答:))我已经知道因为我主要是红宝石程序员

One other option you can use to cluster NodeJs applications is to deploy the app using PM2. 可用于群集NodeJ应用程序的另一个选项是使用PM2部署应用程序。

Clustering is just easy as this, You don't need to implement clustering by hand 集群很容易,因此, 您不需要手动实现集群

pm2 start app.js -i max

PM2 is an expert to auto detect the number of available CPUs and run as many processes as possible PM2是自动检测可用CPU数量并运行尽可能多的进程的专家

Read about PM2 cluster mode here http://pm2.keymetrics.io/docs/usage/cluster-mode/ 在这里阅读PM2集群模式http://pm2.keymetrics.io/docs/usage/cluster-mode/

For controlling the load of IO operations, I wrote a library called QueueP using the memoization concept. 为了控制IO操作的负载,我使用memoization概念编写了一个名为QueueP的库。 You can even customize the memoization logic and gain speedup values of more than 10, sometimes 您甚至可以自定义记忆逻辑并获得超过10的加速值

https://www.npmjs.com/package/queuep https://www.npmjs.com/package/queuep

As far as I know, the built in node cluster is not a good solution yet (load is not evenly distributed across cores). 据我所知,内置节点集群还不是一个好的解决方案(负载不是均匀分布在核心上)。 Until v0.12: http://strongloop.com/strongblog/whats-new-in-node-js-v0-12-cluster-round-robin-load-balancing/ 直到v0.12: http ://strongloop.com/strongblog/whats-new-in-node-js-v0-12-cluster-round-robin-load-balancing/

So you should use nginx until then. 所以你应该在那之前使用nginx。 After that we will see some benchmarks comparing both options and see if the built in cluster module is a good choice. 之后我们将看到一些基准比较两个选项,看看内置的集群模块是否是一个不错的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM