当前位置:网站首页>Nodejs implements multi process

Nodejs implements multi process

2022-06-23 17:54:00 conanma

Nodejs The main process of is single threaded , But it has a multithreaded processing scheme ( More importantly, it is a multi process solution ), That is, the main process starts different sub processes , The main process receives all requests , It will then be distributed to other different nodejs Subprocess processing .

It generally has two implementations :

  1. The main process listens on a port , Child processes do not listen on ports , Distribute requests to child processes through the main process ;
  2. The main process and sub process listen to different ports respectively , Distribute requests to child processes through the main process .

cluster Pattern

Nodejs Of cluster Pattern The first implementation is used , It uses a main thread master And multiple child threads worker, Form a cluster , Distribute requests to child threads through the main thread .cluster Realized with child_process Encapsulation , adopt fork Method to create sub processes to implement the multi process model .

cluster Use

http and clusterprocess All are nodejs The built-in modules , No additional installation required

  1. Create a http service
// http yes nodejs Built-in module 
const http = require('http')

const server = http.createServer((req, res) => {
  res.write('hello http!')
  res.end()
})

server.listen(3030, () => {
  console.log('server is listening on http://localhost:3030')
})

// process yes node Process module of , You can get the process information from this module , And control the process 
console.log(`worker ${process.pid} start`)
  1. establish cluster In the following procedure , It will first determine whether there is a main process , without , Just create the process , It will default that the first process is the main process
  • In the source code , Is to call cluster.fork() When the method is used , Will execute setupPrimary Method to create the main process , It will use initialized Is the sign true Will determine whether it is the first time to create , If yes, create the main process , Otherwise, skip
  • Use createWorkerProcess To create child processes , This method actually uses child_process To create a child process
const cluster = require('cluster')
//  Number of child processes opened 
const workerNum = 3;
//  If it is the main process 
if(cluster.isMaster) {
  //  Create child process 
  for(let i = 0; i < workerNum; i++) {
    //  adopt cluster.fork Create child process 
    cluster.fork()
  }
//  If there are child processes , Start relevant services , Three processes are used to execute http Service Demo 
}else {
  require('./http-server')
}

The implementation process is like this : cluster Module application child_process To create child processes , Subprocesses are overwritten cluster._getServer Method , Thus in server.listen To ensure that only the main process listens to the port , The master subprocess passes IPC communicate , Secondly, the main process is different according to the platform or protocol , Apply two different modules (round_robin_handle.js and shared_handle.js) Make the request and distribute it to the child process for processing .

PM2

PM2 Is the background process manager , It is a mature application of multi process solution , Can help manage and keep applications online .

Basic use

Global installation :npm install [email protected] -g It's also very simple to use :

  • Turn on (http-server.js Is the program to start ):pm2 start http-server.js
  • restart ( Program ):restart app_name
  • heavy load ( Configuration and procedures ):reload app_name
  • stop it :pm2 stop app_name
  • Delete :pm2 delete app_name
  • Monitor mode :pm2 start xx.js --watch

Load balancing :

PM2 Yes nodejs application , Load balancing can be realized automatically according to the system :pm2 start http-server.js -i max

PM2 To configure

We definitely don't want to start every time , You have to manually input a bunch of instructions , So we can use the configuration file to manage these configurations , Be careful js The file name must be xxx.config.js, I use it here. ecosystem.config.jsapps Array , You can place multiple objects , Perform different configurations for multiple files

// ecosystem.config.js
module.exports = {
  apps : [{
    name: "http-server", //  Start process name 
    script: "./src/http-server.js", //  Startup file 
    instances: 4, //  Number of start processes 
    exec_mode: 'cluster', //  Multi process and multi instance 
    //  Set the environment configuration for different environments 
    //  development environment , Corresponding --env  Later parameters 
    env_development: {
      NODE_ENV: "dev",
      watch: true, //  Development environment use  true, Other settings are  false
    },
    //  Test environment 
    env_testing: {
      NODE_ENV: "test",
      watch: false, //  Development environment use  true, Other settings are  false
    },
    //  Production environment 
    env_production: {
      NODE_ENV: "prod",
      watch: false, //  Development environment use  true, Others must be set to  false
    },
    //  Log date format 
    log_date_format: 'YYYY-MM-DD HH:mm Z',
    //  Error log file , Must be set in a directory outside the project , This is for testing 
    error_file: '~/Desktop/logs/err.log', 
    //   Water logging , Include  console.log  journal , Must be set in a directory outside the project , This is for testing 
    out_file: '~/Desktop/logs/info.log', 
    //  Maximum restart data , When the application is considered continuous n From unstable restart , Restart again 
    max_restarts: 10,
  },{
    name: "express-test", //  Start process name 
    script: "./src/express-test.js", //  Startup file 
    instances: 4, //  Number of start processes 
    exec_mode: 'cluster', //  Multi process and multi instance 
  }]
}

Perform configuration :pm2 start ecosystem.config.js --env dev

You can see that after startup , Pipelining and error logs are generated on the desktop :

journal

Water logging

Reference resources : Node Process modular API:http://nodejs.cn/api/process.html pm2 Official website : https://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/

原网站

版权声明
本文为[conanma]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/01/202201041953251879.html