首页 > 其他 > 详细

node主从模式由浅入深

时间:2015-06-03 00:58:27      阅读:281      评论:0      收藏:0      [点我收藏+]


详细代码可以看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:~$ 

基本的多进程架构就介绍到这里啦,这只是最基本的,需要完善和处理成稳定的的服务架构。

node主从模式由浅入深

原文:http://blog.csdn.net/whynottrythis/article/details/46337041

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!