探索 PM2 Cluster 模式下 Log4js 日誌丟失

Node 應用爲單線程應用,JS 雖可利用異步 I/O 避免線程阻塞,但沒法利用多核 CPU 的優點提高運行效率,提升吞吐量仍需多線程。Node Cluster 可產生多個工做線程共享同一 TCP 鏈接,主線程經過 IPC 通道與工做線程通信,並使用 Round-robin 負載均衡極好的處理線程間壓力。html

PM2 Cluster 使得 Node 操做集羣更加容易,PM2 會根據服務器 CPU 核數產生相應的工做線程,只需按以下方式啓動應用:前端

pm2 start app.js -i 0

但 PM2 Cluster 與 Log4js 相撞時,砸出了大坑,本人踩了進去。git

踩坑通過:某日服務端同窗上報了一線上請求參數異常日誌,爲追蹤異常產生緣由,我在全部線上服務器翻查均未尋到相關日誌。服務端異常日誌並不是捏造,前端日誌丟失並不是偶然。爲統計日誌丟失率,在線下環境定量發起 100 條請求,結果僅產生 25 條日誌,屢次實驗發現丟失率穩定在 3/4 使人髮指!熱(好)愛(奇)技(心)術(重)的我查閱了 Log4js 源碼:github

configuration.addListener((config) => {
    // clear out the listeners, because configure has been called.
    listeners.length = 0;

    disabled = config.disableClustering;
    pm2 = config.pm2;
    pm2InstanceVar = config.pm2InstanceVar || 'NODE_APP_INSTANCE';

    debug(`clustering disabled ? ${disabled}`);
    debug(`cluster.isMaster ? ${cluster.isMaster}`);
    debug(`pm2 enabled ? ${pm2}`);
    debug(`pm2InstanceVar = ${pm2InstanceVar}`);
    debug(`process.env[${pm2InstanceVar}] = ${process.env[pm2InstanceVar]}`);

    // just in case configure is called after shutdown.
    if (pm2) {
        process.removeListener('message', receiver);
    }
    if (cluster.removeListener) {
        cluster.removeListener('message', receiver);
    }

    if (config.disableClustering) {
        debug('Not listening for cluster messages, because clustering disabled.');
    } else if (isPM2Master()) {
        // PM2 cluster support
        // PM2 runs everything as workers - install pm2-intercom for this to work.
        // we only want one of the app instances to write logs.
        debug('listening for PM2 broadcast messages');
        process.on('message', receiver);
    } else if (cluster.isMaster) {
        debug('listening for cluster messages');
        cluster.on('message', receiver);
    } else {
        debug('not listening for messages, because we are not a master process.');
    }
});

請注意:bash

PM2 runs everything as workers - install pm2-intercom for this to work.

Log4js 在 Cluster 模式下,worker 將日誌發送至 master,master 實現日誌寫入文件。但在 PM2 Cluster 模式下,全部進程皆爲 worker:服務器

因而按照 Log4js 源碼的指引安裝 pm2-intercom 進程間通信模塊:多線程

仍不奏效,又注意到 isPM2Master():app

const isPM2Master = () => pm2 && process.env[pm2InstanceVar] === '0';
const isMaster = () => disabled || cluster.isMaster || isPM2Master();

isPM2Master 經過 Log4js configure 中 pm2 及 pm2InstanceVar 參數肯定,因而修改 Log4js 配置以下:負載均衡

Log4JS.configure({
    // ...
    pm2: true,
    pm2InstanceVar: 'INSTANCE_ID'
});

終於解決了 PM2 Cluster 模式下 Log4js 日誌丟失問題。koa


補充一下:

自行實現 Node Cluster:

const OS = require('os');
const Cluster = require('cluster');
const Koa = require('koa');
const App = new Koa();
if (Cluster.isMaster) {
    for (let i = 0; i < OS.cpus().length; i++) Cluster.fork();
    console.log('master', process.pid);
} else {
    App.listen(3000);
    console.log('worker', process.pid);
}

端口 PID 與控制檯顯示的 PID List 關係:

使用 PM2 Cluster 啓動 Node 應用,端口 PID 與 PM2 控制檯顯示的 PID List 關係:


做者:呆戀小喵

個人後花園:https://sunmengyuan.github.io...

個人 github:https://github.com/sunmengyuan

原文連接:https://sunmengyuan.github.io...

相關文章
相關標籤/搜索