Skip to content

Commit

Permalink
add code
Browse files Browse the repository at this point in the history
  • Loading branch information
applejian committed Nov 16, 2021
1 parent 57747be commit eed111e
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 0 deletions.
18 changes: 18 additions & 0 deletions 5/cluster/app.js
@@ -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`);
10 changes: 10 additions & 0 deletions 5/cluster/cluster.js
@@ -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');
}
17 changes: 17 additions & 0 deletions 5/pm2/app.js
@@ -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 added 5/pm2/data/err-0.log
Empty file.
Empty file added 5/pm2/data/err-1.log
Empty file.
Empty file added 5/pm2/data/info-0.log
Empty file.
Empty file added 5/pm2/data/info-1.log
Empty file.
25 changes: 25 additions & 0 deletions 5/pm2/pm2.config.js
@@ -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,

}]
}

0 comments on commit eed111e

Please sign in to comment.