多核机器上的Node.js

本文翻译自:Node.js on multi-core machines

Node.js looks interesting, BUT I must miss something - isn't Node.js tuned only to run on a single process and thread? Node.js看起来很有趣, 我必须错过一些东西 - 不是Node.js只调整为在单个进程和线程上运行吗?

Then how does it scale for multi-core CPUs and multi-CPU servers? 那么它如何扩展多核CPU和多CPU服务器? After all, it is all great to make fast as possible single-thread server, but for high loads I would want to use several CPUs. 毕竟,尽可能快速地制作单线程服务器,但是对于高负载我想要使用多个CPU。 And the same goes for making applications faster - seems today the way is use multiple CPUs and parallelize the tasks. 同样可以使应用程序更快 - 似乎今天的方式是使用多个CPU并并行化任务。

How does Node.js fit into this picture? Node.js如何适应这张图片? Is its idea to somehow distribute multiple instances or what? 它的想法是以某种方式分发多个实例或什么?


#1楼

参考:https://stackoom.com/question/A19g/多核机器上的Node-js


#2楼

You can use cluster module. 您可以使用群集模块。 Check this . 检查一下

var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
    // Fork workers.
    for (var i = 0; i < numCPUs; i++) {
        cluster.fork();
    }

    cluster.on('exit', function(worker, code, signal) {
        console.log('worker ' + worker.process.pid + ' died');
    });
} else {
    // Workers can share any TCP connection
    // In this case its a HTTP server
    http.createServer(function(req, res) {
        res.writeHead(200);
        res.end("hello world\n");
    }).listen(8000);
}

#3楼

一种方法是在服务器上运行node.js的多个实例,然后在它们前面放置一个负载均衡器(最好是非阻塞的,如nginx)。


#4楼

Future version of node will allow you to fork a process and pass messages to it and Ryan has stated he wants to find some way to also share file handlers, so it won't be a straight forward Web Worker implementation. 未来版本的节点将允许您分叉一个进程并将消息传递给它,Ryan已经声明他想找到一些方法来共享文件处理程序,因此它不会是一个直接的Web Worker实现。

At this time there is not an easy solution for this but it's still very early and node is one of the fastest moving open source projects I've ever seen so expect something awesome in the near future. 目前还没有一个简单的解决方案,但它仍然很早,节点是我见过的最快的移动开源项目之一,所以期待在不久的将来有一些很棒的东西。


#5楼

It's also possible to design the web-service as several stand alone servers that listen to unix sockets, so that you can push functions like data processing into seperate processes. 也可以将Web服务设计为几个监听unix套接字的独立服务器,这样就可以将数据处理等功能推送到单独的进程中。

This is similar to most scrpting/database web server architectures where a cgi process handles business logic and then pushes and pulls the data via a unix socket to a database. 这类似于大多数scrpting /数据库Web服务器体系结构,其中cgi进程处理业务逻辑,然后通过unix套接字将数据推送到数据库。

the difference being that the data processing is written as a node webserver listening on a port. 不同之处在于数据处理被写为侦听端口的节点web服务器。

it's more complex but ultimately its where multi-core development has to go. 它更复杂,但最终还是多核开发必须要去的地方。 a multiprocess architecture using multiple components for each web request. 为每个Web请求使用多个组件的多进程体系结构。


#6楼

As mentioned above, Cluster will scale and load-balance your app across all cores. 如上所述, Cluster将跨所有核心扩展和负载均衡您的应用。

adding something like 添加类似的东西

cluster.on('exit', function () {
  cluster.fork();
});

Will restart any failing workers. 将重启任何失败的工人。

These days, a lot of people also prefer PM2 , which handles the clustering for you and also provides some cool monitoring features . 现在,很多人也更喜欢PM2 ,它可以为您处理群集,并提供一些很酷的监控功能

Then, add Nginx or HAProxy in front of several machines running with clustering and you have multiple levels of failover and a much higher load capacity. 然后,在运行集群的多台计算机前添加Nginx或HAProxy,您可以实现多级故障转移和更高的负载容量。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值