使用pm2管理node项目,使用cluster模式利用多核cpu。
使用配置文件
xxx.json文件
[{
"name" : "server_name",
"script" : "./index.js",
"cwd" : "./",
"exec_mode": "cluster", // 使用的模式
"instances": 10,// 开启的线程数
"error_file" : "app-err.log",
"out_file" : "app-out.log",
"env": {
"NODE_ENV": "production",
"MICRO_CACHE":true
}
}]
启动命令: pm2 start xxx.json
使用命令启动
pm2 start ./index.js -i 4 //使用cluster模式启用4个线程
成功启动了多线程,使用pm2 进行cluster 模式多线程,不用修改node项目中的代码,就能实现