发布于 2017-07-03 06:28:30 | 128 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Node.js教程,程序狗速度看过来!

Node.js 服务器端的JavaScript

Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台, 用来方便地搭建快速的 易于扩展的网络应用· Node.js 借助事件驱动, 非阻塞I/O 模型变得轻量和高效, 非常适合 运行在分布式设备 的 数据密集型 的实时应用


这篇文章主要介绍了node.js中cluster的使用教程,分别介绍使用NODE中cluster利用多核CPU、通过消息传递来监控工作进程状态以及杀死僵尸进程等功能,给出了详细的示例代码供大家参考学习,需要的朋友们下面来一起看看吧。

本文主要给大家介绍了关于node.js中cluster使用的相关教程,分享出来供大家参考学习,下面来看看详细的介绍:

一、使用NODE中cluster利用多核CPU


var cluster = require('cluster'); 
var http = require('http'); 
var numCPUs = require('os').cpus().length; 
if (cluster.isMaster) { 
// 创建工作进程 
for (var i = 0; i < numCPUs; i++) { 
cluster.fork(); 
} 
cluster.on('death', function(worker) { 
console.log('worker ' + worker.pid + ' died'); 
cluster.fork();//重启子进程 
}); 
} else { 
// 工作进程创建http 服务器 
http.Server(function(req, res) { 
res.writeHead(200); 
res.end("hello world\n"); 
}).listen(8000); 
} 

二、通过消息传递来监控工作进程状态


var cluster = require('cluster'); 
var http = require('http'); 
var numCPUs = require('os').cpus().length; 
var rssWarn = (12 * 1024 * 1024) 
  , heapWarn = (10 * 1024 * 1024) 
if(cluster.isMaster) { 
  for(var i=0; i<numCPUs; i++) { 
    var worker = cluster.fork(); 
    worker.on('message', function(m) { 
      if (m.memory) { 
        console.log(m.memory.rss,rssWarn) 
        if(m.memory.rss > rssWarn) { 
          console.log('Worker ' + m.process + ' using too much memory.') 
        } 
      } 
 
    }) 
  } 
} else { 
// 服务器 
  http.createServer(function(req,res) { 
    res.writeHead(200); 
    res.end('hello world\n') 
  }).listen(8000) 
// 每秒报告一次状态 
  setInterval(function report(){ 
    process.send({memory: process.memoryUsage(), process: process.pid}); 
  }, 1000) 
} 

三、杀死僵尸进程


var cluster = require('cluster'); 
var http = require('http'); 
var numCPUs = require('os').cpus().length; 
var rssWarn = (50 * 1024 * 1024) 
  , heapWarn = (50 * 1024 * 1024) 
var workers = {} 
if(cluster.isMaster) { 
  for(var i=0; i<numCPUs; i++) { 
    createWorker() 
  } 
  setInterval(function() { 
    var time = new Date().getTime() 
    for(pid in workers) { 
      if(workers.hasOwnProperty(pid) && 
        workers[pid].lastCb + 5000 < time) { 
        console.log('Long running worker ' + pid + ' killed') 
        workers[pid].worker.kill() 
        delete workers[pid] 
        createWorker() 
      } 
    } 
  }, 1000) 
} else { 
// 服务器 
  http.Server(function(req,res) { 
// 打乱200 个请求中的1 个 
    if (Math.floor(Math.random() * 200) === 4) { 
      console.log('Stopped ' + process.pid + ' from ever finishing') 
      while(true) { continue } 
    } 
    res.writeHead(200); 
    res.end('hello world from ' + process.pid + '\n') 
  }).listen(8000) 
// 每秒钟报告一次状态 
  setInterval(function report(){ 
    process.send({cmd: "reportMem", memory: process.memoryUsage(), 
      process: process.pid}) 
  }, 1000) 
} 
function createWorker() { 
  var worker = cluster.fork() 
  console.log('Created worker: ' + worker.pid) 
// 允许开机时间 
  workers[worker.pid] = {worker:worker, lastCb: new Date().getTime()-1000} 
  worker.on('message', function(m) { 
    if(m.cmd === "reportMem") { 
      workers[m.process].lastCb = new Date().getTime() 
      if(m.memory.rss > rssWarn) { 
        console.log('Worker ' + m.process + ' using too much memory.') 
      } 
    } 
  }) 
} 

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对phperz的支持。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务