NodeJS+Sequelize在生产环境下,最合适的建立连接池的方式是?
发布于 1 年前 作者 monkeytwins 2245 次浏览 来自 问答

看官网文档里提到关于多进程的情况下,必须为每一个进程都创建sequelize实例:

If you’re connecting to the database from multiple processes, you’ll have to create one instance per process, but each instance should have a maximum connection pool size of such that the total maximum size is respected.

生产环境下,肯定是多负载多进程的情况,并且一般用cluster模块去进行进程处理

if (cluster.isMaster) {
    for (var i = 0; i < env.CPU; i++) {
        cluster.fork();
    }

    cluster.on('fork', worker => {
        log(`worker ${worker.process.pid} forked success`, 'info', logOptions);
    });

    cluster.on('online', worker => {
        log(`worker ${worker.process.pid} responded after it was forked`, 'info', logOptions);
    });

    cluster.on('exit', function (worker, code, signal) {
        log(`worker ${worker.process.pid} died (${signal || code})`, 'warn', logOptions);
        cluster.fork();
    });
} else {
    require('../app.js');
}

想请教下这种情况下,Sequelize的实例初始化应该在什么阶段?

回到顶部