diff --git a/5/cluster/app.js b/5/cluster/app.js new file mode 100644 index 0000000..b549262 --- /dev/null +++ b/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`); \ No newline at end of file diff --git a/5/cluster/cluster.js b/5/cluster/cluster.js new file mode 100644 index 0000000..847c8b0 --- /dev/null +++ b/5/cluster/cluster.js @@ -0,0 +1,10 @@ +const cluster = require('cluster'); +const instances = 2; // 启动进程数量 + +if (cluster.isMaster) { + for(let i = 0;i { + res.write('hello world, start with pm2'); + res.end(); +}); + +/** + * + * 启动服务 + */ +server.listen(3000, () => { + console.log('server start http://127.0.0.1:3000'); +}); \ No newline at end of file diff --git a/5/pm2/data/err-0.log b/5/pm2/data/err-0.log new file mode 100644 index 0000000..e69de29 diff --git a/5/pm2/data/err-1.log b/5/pm2/data/err-1.log new file mode 100644 index 0000000..e69de29 diff --git a/5/pm2/data/info-0.log b/5/pm2/data/info-0.log new file mode 100644 index 0000000..e69de29 diff --git a/5/pm2/data/info-1.log b/5/pm2/data/info-1.log new file mode 100644 index 0000000..e69de29 diff --git a/5/pm2/pm2.config.js b/5/pm2/pm2.config.js new file mode 100644 index 0000000..a65e306 --- /dev/null +++ b/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, + + }] + } \ No newline at end of file