Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const http = require('http'); | ||
/** | ||
* | ||
* 创建 http 服务,简单返回 | ||
*/ | ||
const server = http.createServer((req, res) => { | ||
res.write(`hello world, start with cluster ${process.pid}`); | ||
res.end(); | ||
}); | ||
/** | ||
* | ||
* 启动服务 | ||
*/ | ||
server.listen(3000, () => { | ||
console.log('server start http://127.0.0.1:3000'); | ||
}); | ||
|
||
console.log(`Worker ${process.pid} started`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const cluster = require('cluster'); | ||
const instances = 2; // 启动进程数量 | ||
|
||
if (cluster.isMaster) { | ||
for(let i = 0;i<instances;i++) { // 使用 cluster.fork 创建子进程 | ||
cluster.fork(); | ||
} | ||
} else { | ||
require('./app.js'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const http = require('http'); | ||
/** | ||
* | ||
* 创建 http 服务,简单返回 | ||
*/ | ||
const server = http.createServer((req, res) => { | ||
res.write('hello world, start with pm2'); | ||
res.end(); | ||
}); | ||
|
||
/** | ||
* | ||
* 启动服务 | ||
*/ | ||
server.listen(3000, () => { | ||
console.log('server start http://127.0.0.1:3000'); | ||
}); |
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module.exports = { | ||
apps : [{ | ||
name: "nodejs-column", // 启动进程名 | ||
script: "./app.js", // 启动文件 | ||
instances: 2, // 启动进程数 | ||
exec_mode: 'cluster', // 多进程多实例 | ||
env_development: { | ||
NODE_ENV: "development", | ||
watch: true, // 开发环境使用 true,其他必须设置为 false | ||
}, | ||
env_testing: { | ||
NODE_ENV: "testing", | ||
watch: false, // 开发环境使用 true,其他必须设置为 false | ||
}, | ||
env_production: { | ||
NODE_ENV: "production", | ||
watch: false, // 开发环境使用 true,其他必须设置为 false | ||
}, | ||
log_date_format: 'YYYY-MM-DD HH:mm Z', | ||
error_file: '~/data/err.log', // 错误日志文件,必须设置在项目外的目录,这里为了测试 | ||
out_file: '~/data/info.log', // 流水日志,包括 console.log 日志,必须设置在项目外的目录,这里为了测试 | ||
max_restarts: 10, | ||
|
||
}] | ||
} |