使用Nodejs建立基本的網站 Microblog--《Node.js開發指南》 3

來源:互聯網
上載者:User

標籤:ubuntu   etc   

  1. 使用cluster模組

      建立cluster.js,調用app.js

var cluster = require(‘cluster‘);var os = require(‘os‘);//擷取CPU數量var numCPUs = os.cpus().length;var workers = {};if(cluster.isMaster) {  //主進程分支  cluster.on(‘exit‘, function (worker) {    //當一個背景工作處理序結束時,重啟背景工作處理序    delete workers[worker.pid];    worker = cluster.fork();    workers[worker.pid] = worker;  });  //初始開啟與CPU數量相同的背景工作處理序  for(var i = 0; i < numCPUs; i++) {    var worker = cluster.fork();    workers[worker.pid] = worker;  }} else {  //背景工作處理序分支,啟動伺服器  var app = require(‘./app‘);  app.listen(3000);}//當主進程被終止時,關閉所有背景工作處理序process.on(‘SIGTERM‘, function() {  for( var pid in workers) {    process.kill = (pid);  }  process.exit(0);});

    在終端運行

$node cluster.js

    可以看到進程列表中啟動了多個node進程(4核CPU)。

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/45/3E/wKioL1PkvxiAEYmLAAObnKBzTZA089.jpg" title="2014-08-08 19:50:13 的螢幕.png" alt="wKioL1PkvxiAEYmLAAObnKBzTZA089.jpg" />    cluster.js建立與CPU核心數個數相同的伺服器處理序,以確保充分利用CPU資源。通過cluster啟動的背景工作處理序可以直接實現連接埠複用,因此所有背景工作處理序只需監聽同一個連接埠。

2.啟動指令碼

建立mynodeapp檔案

#! /bin/shNODE_ENV=productionDAEMON="node /home/nick/nodejs/examples/express_example/cluster.js"NAME=MyNodeAppDESC=MyNodeAppPIDFILE="mynodeapp.pid"case "$1" in  start)        echo "Starting $DESC: "                nohup $DAEMON > /dev/null &        echo $1 > $PIDFILE        echo "$NAME."                ;;   stop)        echo "Stopping $DESC: "                pid=‘cat $PIDFILE‘        kill $pid                rm $PIDFILE        echo "$NAME."                ;;esacexit 0

    

$chmode +x mynodeapp

    賦予執行許可權,其通過nohup啟動伺服器,使進程不會因為退出終端而關閉,同時將主進程pid寫入檔案,當調用結束時,從檔案讀取pid,終止主進程以關閉伺服器。

    運行結果卻不好,可以啟動程式,但是無法關閉。

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/45/3E/wKiom1PkxbuCYFghAAPDsGy0I8I375.jpg" style="float:none;" title="2014-08-08 20:34:56 的螢幕.png" alt="wKiom1PkxbuCYFghAAPDsGy0I8I375.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/45/3E/wKioL1PkxtTz3s-0AAOH1nVlYD8908.jpg" style="float:none;" title="2014-08-08 20:41:05 的螢幕.png" alt="wKioL1PkxtTz3s-0AAOH1nVlYD8908.jpg" />

Ubuntu 建立開機自啟動指令碼的方法

自動編號:

$ cd /etc/init.d$ sudo chmod 755 /etc/init.d/filename$ sudo update-rc.d filename defaults

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/45/3E/wKiom1PkyNPS06AZAAPB7tgGZ60439.jpg" title="2014-08-08 20:55:07 的螢幕.png" alt="wKiom1PkyNPS06AZAAPB7tgGZ60439.jpg" />

指定編號:

$ cd /etc/init.d$ sudo chmod 755 /etc/init.d/filename$ sudo update-rc.d filename defaults N

編號必須在 0 至 99 之間。

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/45/3E/wKiom1PkxxSg1LLNAATn9H7jhVE823.jpg" title="2014-08-08 20:45:51 的螢幕.png" alt="wKiom1PkxxSg1LLNAATn9H7jhVE823.jpg" />

如果要卸載啟動指令碼:

$ cd /etc/init.d$ sudo update-rc.d -f filename remove

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/45/3F/wKioL1PkyJnDsLVvAATn9H7jhVE232.jpg" title="2014-08-08 20:45:51 的螢幕.png" alt="wKioL1PkyJnDsLVvAATn9H7jhVE232.jpg" />


本文出自 “颯颯秋風” 部落格,請務必保留此出處http://xjhznick.blog.51cto.com/3608584/1537612

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.