In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to use cluster to achieve multi-process in nodejs, concise and easy to understand, absolutely can make your eyes shine, through the detailed introduction of this article I hope you can gain something.
cluster
node module for multi-process;
properties and methods
isMaster property, returns whether it is the main process, boolean value;
isWorker property, returns whether the process is a worker process;
fork() method, which can only be called by the main process to spawn a new worker child process and return the worker object;
setupMaster([setting]) method, used to modify the default behavior of fork, once called, will be set according to cluster.settings.
settings Properties; used for configuration;
parameter exec:worker file path,
args: parameters passed to worker;
execArgv: a list of parameters passed to the nodejs executable;
const cluster = require('cluster');const cpuNums = require('os').cpus().length;const http = require('http');if (cluster.isMaster) { console.log(cpuNums); for (var i = 0; i
< cpuNums; i++) { cluster.fork(); // 相当于node main.js,重新执行自己 // 和process_child相比,不用重新创建child.js, } cluster.on('fork', worker =>{ console.log(`主进程fork了一个worker,pid为${worker.process.pid}`) }) cluster.on('listening', worker => { console.log(`主进程fork了一个worker,pid为${worker.process.pid}`) }) cluster.on('message', data => { console.log('主进程接收到了子进程消息为:',data) }) Object.keys(cluster.workers).forEach(item => { cluster.workers[item].on('message', data => { console.log(data); }); }); cluster.on('disconnect', (worker) => { console.log('有工作进程退出了',worker.process.pid) })} else { http.createServer((req, res) => { res.end('hello') }).listen(8001, () =>{ console.log('child server is runing') }) console.log('我是子进程'); process.send('子说:你好');}事件
fork事件,当新的工作进程被fork时触发,可以用来记录工作进程活动;
listening事件,当一个工作进程调用listen()后触发,事件处理器两个参数:worker:工作进程对象, address:包含了链接属性
只有http的服务的listen事件才能触发此事件
message事件,监听子进程的消息;当cluster主进程接收任何工作进程发送的消息时触发;
比较特殊需要在单独的worker上监听;
online事件,
disconnect事件,当工作进程断开时调用;
exit事件,
setup事件,cluster.setupMaster()执行后触发;
cluster多进程模型
每个worker进程通过使用cluster.fork()函数,基于IPC(Inter-Process-Communication),实现与master进程间通信;
那通过child_process.fork()直接创建不就可以了,为什么要通过cluster
这种方式只实现了多进程,多进程运行还涉及父子进程通信,子进程管理,以及负载均衡等问题,这些特性cluster已经做了处理了;
惊群现象
多个进程间会竞争一个accept连接,产生惊群现象,效率比较低;
由于无法控制一个新的连接由哪个进程来处理,导致worker进程间负载不均衡;
master.js
const net = require('net'); // 是最基础的网络模块,http的基础就是网络模块,最底层是socketconst fork = require('child_process').fork; // 惊群var handle = net._createServerHandle('0.0.0.0', 5000); // net模块创建一个服务,绑定到3000端口,返回一个callbackfor (var i = 0; i
< 4; i++) { console.log('fork', i); fork('./worket.js').send({}, handle); // 主进程fork子进程,send信息} worker.js const net = require('net');process.on('message', (m, handle) =>{ //child process receives master message// master receives client request, worker responds to start(handle);});var buf = 'hello nodejs';var res =<$'HTTP/1.1 200 OK','content-length' + buf.length].join ('\r\n')+ ' \r\n' + buf;var data = {}; function start(server) { //Response logic, Focus on crowd effect, Count server.listen(); server.onconnection = function(err, hand) { var pid = process.pid; if (! data[pid]) { data[pid] = 0; } data[pid]++; console.log('get a connection on worker,pid = %d', process.pid, data[pid]); var socket = net.Socket({ handle: hand }); socket.readable = socket.writable = true; //modify socket read-write properties socket.end(res); };}nginx proxy
Nginx is a very lightweight http server written by Russians. It is a high-performance HTTP and reverse proxy server, asynchronous non-blocking I/O, and capable of high concurrency.
Forward proxy: client is proxy, server does not know who proxy is;
Reverse proxy: The server is a proxy, and the client does not know who the proxy is.
nginx practical application scenarios: more suitable for stable services
Static resource server: js, css, html
Enterprise cluster
Daemon process: After exiting the command window, the service is always running;
Cluster multi-process scheduling model
Cluster is monitored by the master and distributed to each worker through round-robin algorithm to avoid the occurrence of panic phenomenon;
Round-robin scheduling algorithm is based on the principle that each request from the user is assigned to the internal server in turn;
cluster-model.js
const net = require ('net ');const fork = require ('child_process').fork; // cluster Simple version, cluster is based on child_process decapsulation;var workers = [];for (var i = 0; i
< 4; i++) { workers.push(fork('./child')); // cluster workers}var handle = net._createServerHandle('0.0.0.0', 3001); // masterhandle.listen();handle.onconnection = function(err, handle) { var worker = workers.pop(); worker.send({}, handle); workers.unshift(worker); // 通过pop 和 unshift实现一个简单的轮询}; child.js const net = require('net');process.on('message', (m, handle) =>{ debugger; start(handle);});var buf = 'hello cluster';var res = ['HTTP/1.1 200 OK', 'content-length' + buf.length].join('\r\n') + '\r\n\r\n' + buf;function start(handle) { console.log('get a worker on server,pid = ' + process.pid); var socket = net.Socket({ handle }); socket.readable = socket.writable = true; // 修改socket的读写属性 socket.end(res);}cluster中的优雅退出
关闭异常worker进程所有的TCP server(将已有的快速断开,且不再接受新的连接),断开和Master的IPC通道,不再接受新的用户请求;
Master立刻fork一个新的worker进程,保证总的进程数量不变;
异常worker等待一段时间,处理完已接受的请求后退出;
if(cluster.isMaster){ cluster.fork(); }else { // 出错之后 try{ res.end(dddd); // 报错,整个线程挂掉,不能提供服务, }catch(err){ // 断开连接,断开和Master的连接,守护进程其实就是重启; process.disconnect(); // or exit() } }进程守护
Master进程除了接收新的连接,分发给各worker处理之外,还像天使一样默默守护着这些进程,保障应用的稳定性,一旦某个worker进程退出就fork一个新的子进程顶替上去;
这一切cluster模块已经处理好了,当某个worker进程发生异常退出或者与Master进程失去联系(disconnected)时,master进程都会收到相应的事件通知;
cluster.on('exit',function(){ cluster.fork();})cluster.on('disconnect',function(){ cluster.fork();})IPC通信
IPC通信就是进程间的通信;
虽然每个worker进程是相对独立的,但是他们之间还是需要通信的;叫进程间通信(IPC)通信;
worker和worker之间的通信通过Master转发:通过worker的pid
const cluster = require('cluster');if(cluster.isMaster){ var worker = cluster.fork(); worker.send('hi, i am master'); worker.on('message', (msg) =>{ console.log(`${msg} is from worker ${worker.id}`) })}else if(cluster.isWorker){ process.on('message', (msg) =>{ process.send(msg); }) }上述内容就是nodejs中怎么利用cluster实现多进程,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.