用NodeJS實現叢集計算
Node的單個例項執行在單個的執行緒中,要充分利用多核系統,我們可以執行Node程式叢集來處理負載。
也就是說,如果系統有8核,單個Node例項只能使用其中1核,可以利用cluster包的workers概念來充分利用所有的核。有趣的是,它們可以共享同一個埠。
該模組還處於實驗階段。
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
require('os').cpus().forEach(function(){
cluster.fork();
});
// In case the worker dies!
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
// As workers come up.
cluster.on('listening', function(worker, address) {
console.log("A worker with #"+worker.id+" is now connected to " +\
address.address +\
":" + address.port);
});
// When the master gets a msg from the worker increment the request count.
var reqCount = 0;
Object.keys(cluster.workers).forEach(function(id) {
cluster.workers[id].on('message',function(msg){
if(msg.info && msg.info == 'ReqServMaster'){
reqCount += 1;
}
});
});
// Track the number of request served.
setInterval(function() {
console.log("Number of request served = ",reqCount);
}, 1000);
} else {
// Workers can share the same port!
require('http').Server(function(req, res) {
res.writeHead(200);
res.end("Hello from Cluster!");
// Notify the master about the request.
process.send({ info : 'ReqServMaster' });
}).listen(8000);
}在一個4核的計算機上,輸出如下:
A worker with #2 is now connected to 0.0.0.0:8000
A worker with #4 is now connected to 0.0.0.0:8000
A worker with #1 is now connected to 0.0.0.0:8000
A worker with #3 is now connected to 0.0.0.0:8000
Number of request served = 0
...
Number of request served = 2
..
Number of request served = 4
...
Number of request served = 6
也就是說,如果系統有8核,單個Node例項只能使用其中1核,可以利用cluster包的workers概念來充分利用所有的核。有趣的是,它們可以共享同一個埠。
該模組還處於實驗階段。
CODE:
var cluster = require('cluster');var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
require('os').cpus().forEach(function(){
cluster.fork();
});
// In case the worker dies!
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
// As workers come up.
cluster.on('listening', function(worker, address) {
console.log("A worker with #"+worker.id+" is now connected to " +\
address.address +\
":" + address.port);
});
// When the master gets a msg from the worker increment the request count.
var reqCount = 0;
Object.keys(cluster.workers).forEach(function(id) {
cluster.workers[id].on('message',function(msg){
if(msg.info && msg.info == 'ReqServMaster'){
reqCount += 1;
}
});
});
// Track the number of request served.
setInterval(function() {
console.log("Number of request served = ",reqCount);
}, 1000);
} else {
// Workers can share the same port!
require('http').Server(function(req, res) {
res.writeHead(200);
res.end("Hello from Cluster!");
// Notify the master about the request.
process.send({ info : 'ReqServMaster' });
}).listen(8000);
}在一個4核的計算機上,輸出如下:
CODE:
Number of request served = 0A worker with #2 is now connected to 0.0.0.0:8000
A worker with #4 is now connected to 0.0.0.0:8000
A worker with #1 is now connected to 0.0.0.0:8000
A worker with #3 is now connected to 0.0.0.0:8000
Number of request served = 0
...
Number of request served = 2
..
Number of request served = 4
...
Number of request served = 6
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-746038/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 計算機叢集計算機
- 計算機叢集與網格計算計算機
- 教你用Magent實現Memcached叢集
- 雲端計算,網格計算,分散式計算,叢集計算的區別?分散式
- 實時計算框架:Spark叢集搭建與入門案例框架Spark
- Mesos計算框架:叢集管理器框架
- 叢集映象:實現高效的分散式應用交付分散式
- GO實現Redis:GO實現Redis叢集(5)GoRedis
- Apache+tomcat實現應用伺服器叢集ApacheTomcat伺服器
- nodeJS程式碼實現計算交社保程式碼例項NodeJS
- 實時計算框架:Flink叢集搭建與執行機制框架
- Redis 叢集實現原理探討Redis
- redis與叢集實用操作筆記Redis筆記
- Redis叢集實現方案選型分析Redis
- 快速實現 Tomcat 叢集 Session 共享TomcatSession
- orleans叢集及負載均衡實現負載
- Kafka 叢集如何實現資料同步?Kafka
- Apache實現weblogic叢集配置(轉)ApacheWeb
- 通過memberlist庫實現gossip管理叢集以及叢集資料互動Go
- 用c++實現淨現值的計算C++
- Spark 1.2.1 釋出,開源叢集計算系統Spark
- 開源叢集計算環境:Spark 1.1.0釋出Spark
- 計算叢集MOSIX-3.1.1.1.for_kernel-3.2.23 配置
- Linux叢集的安裝與平行計算(轉)Linux
- 實現Kubernetes跨叢集服務應用的高可用
- 用隧道協議實現不同dubbo叢集間的透明通訊協議
- 5分鐘實現用docker搭建Redis叢集模式和哨兵模式DockerRedis模式
- 雲端計算面試題筆試錦集,雲端計算實用面試題答案二面試題筆試
- 雲端計算面試題筆試錦集,雲端計算實用面試題答案一面試題筆試
- 應用級叢集系統的設計
- 11、redis使用ruby實現叢集高可用Redis
- EMR叢集上capacityscheduler的ACL實現
- 用EXCEL來實現多層計算模型Excel模型
- 用Docker搭建RabbitMq的普通叢集和映象叢集DockerMQ
- 邊緣計算,如何啃下叢集管理這塊硬骨頭?
- 雲端計算-伺服器叢集技術的分類伺服器
- 計算叢集MOSIX-3.1.1.1.for_kernel-3.2.23安裝
- 無人機叢集自組織搜尋模擬模型設計與實現無人機模型