node-主从模式
具体代码能够看https://github.com/GitHubSi/中的app代码:
1. 多进程
root@ubuntu:/home/fuhui/Public/node# vi master.js var fork = require('child_process').fork; var cpus = require('os').cpus(); //for(var i = 0; i<cpus.length; i++){ for(var i = 0; i<2; i++){ fork('./worker.js'); // fork('./worker.js'); } ~
root@ubuntu:/home/fuhui/Public/node# vi worker.js var http =require('http'); http.createServer(function(req,res){ res.writeHead(200, {'Content-Type':'text/plain'}); res.end('Hello World'); }).listen(Math.random*10000+1,'127.0.0.1');
2. 进程间通信
~ ~ root@ubuntu:/home/fuhui/Public/node# vi parent.js var cp = require('child_process'); var n = cp.fork(__dirname + '/sub.js'); n.on('message' , function(m){ console.log('PARENT got message:',m); }); n.send({hello:'world'}); ~
~ ~ root@ubuntu:/home/fuhui/Public/node# vi sub.js process.on('message',function(m){ console.log('CHILD got mesage',m); }); process.send({foo:'bar'}); ~
3. 句柄传递
改动worker.js
root@ubuntu:/home/fuhui/Public/node# vi worker.js var http =require('http'); var ser = http.createServer(function(req,res){ res.writeHead(200, {'Content-Type':'text/plain'}); res.end('Hello World'); });//.listen(Math.random*10000+1,'127.0.0.1'); ser.listen(8888,'127.0.0.1');
执行的效果:
root@ubuntu:/home/fuhui/Public/node# node master.js events.js:85 throw er; // Unhandled 'error' event ^ Error: listen EADDRINUSE at exports._errnoException (util.js:746:11) at Server._listen2 (net.js:1129:14) at listen (net.js:1155:10) at net.js:1253:9 at dns.js:85:18 at process._tickCallback (node.js:355:11) at Function.Module.runMain (module.js:503:11) at startup (node.js:129:16) at node.js:814:3
改动后的代码:
root@ubuntu:/home/fuhui/Public/node# vi master.js var fork = require('child_process').fork; var child = fork('worker.js'); var child1 = fork('worker.js'); var server = require('net').createServer(); server.listen(1337,function(){ child.send('server',server); child1.send('server',server); server.close(); }); //var cpus = require('os').cpus(); //for(var i = 0; i<cpus.length; i++){ //for(var i = 0; i<2; i++){ // fork('./worker.js'); // fork('./worker.js'); //} ~
~ root@ubuntu:/home/fuhui/Public/node# vi worker.js var http =require('http'); var ser = http.createServer(function(req,res){ res.writeHead(200, {'Content-Type':'text/plain'}); res.end('handle by child ,pid is ' +process.pid +'\n'); });//.listen(Math.random*10000+1,'127.0.0.1'); //ser.listen(8888,'127.0.0.1'); process.on('message',function(m,tcp){ if(m === 'server'){ tcp.on('connection',function(socket){ ser.emit('connection',socket); }); } }); ~ ~ ~
执行效果:
fuhui@ubuntu:~$ curl 127.0.0.1:1337 handle by child ,pid is 4337 fuhui@ubuntu:~$ curl 127.0.0.1:1337 handle by child ,pid is 4336 fuhui@ubuntu:~$ curl 127.0.0.1:1337 handle by child ,pid is 4337 fuhui@ubuntu:~$ curl 127.0.0.1:1337 handle by child ,pid is 4336 fuhui@ubuntu:~$ curl 127.0.0.1:1337 handle by child ,pid is 4337 fuhui@ubuntu:~$ curl 127.0.0.1:1337 handle by child ,pid is 4337 fuhui@ubuntu:~$ curl 127.0.0.1:1337 handle by child ,pid is 4336 fuhui@ubuntu:~$
主要的多进程架构就介绍到这里啦,这仅仅是最主要的。须要完好和处理成稳定的的服务架构。