有办法在Sequelize连接上设置2个独立的DB池,并启用复制吗?

问题描述 投票:1回答:1

阅读复制文档

我主要是想把我的主站和从站的连接数解耦,以最大限度地提高读取副本的可用连接数和负载。我目前正在为一个Heroku托管的应用程序(40个dynos)寻找一个快速和简单的优化方法,这个应用程序目前已经达到了连接的极限,每当请求量达到峰值时,它就会出现。

我尝试用以下方法来优化请求吞吐量 pgbouncer-unnel 都失败了,我在想是不是Sequelize的池子和pgbouncer的池子玩得不好。有一件事我没有尝试,那就是只设置了 pool: false 我用的是Sequelize 3版)。当然我还需要做其他的优化,但是增加只读连接的数量可以缓解这个应用遇到的最多的量。

下面举例说明我们是如何实例化postgres Sequelize postgres连接的。

    let seqOptions: Sequelize.Options = {
        dialect: 'postgres',
        logging: function (sql: string) {
            if (pgConfig.logging) {
                helpers.logDebug('sequelize ' + sql);
            }
        },
        pool: {
            maxConnections: pgConfig.maxConnections,
            minConnections: pgConfig.minConnections,
        },

        define: {
            timestamps: false // true by default
        },

        timezone: config.timezone
    };

    if (config.pg.useSSLDBConnection) {
        seqOptions.dialectOptions = {
            ssl: config.pg.useSSLDBConnection,
            timeout: 10,
            returning: true
        };
    };

    if (config.pg.useReadReplication && !config.pg.separateReadWrite) {
        let masterUrl = url.parse(config.pgWrite.url);
        let username = masterUrl.auth.split(':')[0];
        let password = masterUrl.auth.split(':')[1];
        let databaseName = masterUrl.path.split('/').pop();
        seqOptions.replication = {
            write: {
                host: masterUrl.hostname,
                port: masterUrl.port,
                database: databaseName
            },
            read: _.map(config.pgReadReplicas, (readUrl) => {
                let _readUrl = url.parse(readUrl);
                let databaseName = _readUrl.path.split('/').pop();
                return ({
                    host: _readUrl.hostname,
                    port: _readUrl.port,
                    database: databaseName
                });
            })
        };

        let connection = new Sequelize(null, username, password, seqOptions);
        return new SQLContext(connection);
node.js heroku optimization database-connection sequelize.js
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.