如何将Mongo客户端与高可用性连接

时间:2018-04-10 12:19:41

标签: node.js mongodb mongoose

我可以使用MongoDB以高可用性连接到Mongoose。请看一下我的代码。

function connectMongoose() {
    mongoose.connect(db_url, {
        server: {
            ha: true, // Make sure the high availability checks are on
            haInterval: 5000,
            auto_reconnect: true,
            socketOptions: {
                keepAlive: 1,
                connectTimeoutMS: 30000,
                socketTimeoutMS: 30000,
            }
        },
        replset: {
            ha: true, // Make sure the high availability checks are on
            haInterval: 5000,
            auto_reconnect: true,
            socketOptions: {
                keepAlive: 1,
                connectTimeoutMS: 30000,
                socketTimeoutMS: 30000,
            }
        }
    }, function(err) {
        if (err) console.log(err);
        if (mongoose.connection.db.serverConfig.s.replset) {
            console.log("Adding replicaSet ha listener...");
            mongoose.connection.db.serverConfig.s.replset.on('ha', function(type, data) {
                console.log('replset ha ' + type);
            })
        }
        if (mongoose.connection.db.serverConfig.s.server) {
            console.log("Adding server ha listener...");
            mongoose.connection.db.serverConfig.s.server.on('ha', function(type, data) {
                console.log('server ha ' + type);
            })
        }
    });
}

connectMongoose();

mongoose.connection.on('connecting', function() {
    console.log('Connecting to MongoDB...');
});
mongoose.connection.on('connected', function() {
    console.log('MongoDB connected!');
});
mongoose.connection.on('open', function() {
    console.log('MongoDB connection opened!');
});
mongoose.connection.on('error', function(err) {
    console.error('Error in MongoDb connection: ' + err.stack);
    console.log(err);
    mongoose.disconnect();
});

mongoose.connection.on('disconnected', function() {
    console.log('MongoDB disconnected!');
    connectMongoose();
});
mongoose.connection.on('reconnected', function() {
    console.log('MongoDB reconnected!');
});
mongoose.connection.on('close', function() {
    console.log('MongoDB closed');
});

如何使用MongoClient实现此目的。

目前我这样做没有高可用性检查。

mongodb.MongoClient.connect(db_url, function(err, client) {
    if (err) { return console.dir(err); }
    global.db = client.db(config.mongo.db);
    console.log("Database connection 2 ready ==> Using MongoClient");
});

我这样做是因为我希望有2个mongodb连接。通过第一次连接,我可以使用Schemas进行查询。通过第二次连接,我可以在没有架构的情况下查询集合。

1 个答案:

答案 0 :(得分:0)

这样的事情:

const mongoClient = new MongoClient(db_url, {
    ha: true, // Make sure the high availability checks are on
    haInterval: 5000,
    auto_reconnect: true,
    keepAlive: 1,
    connectTimeoutMS: 30000,
    socketTimeoutMS: 30000
});
mongoClient.connect(function(err, client) {....});

或使用示例中的静态方法:

mongodb.MongoClient.connect(
    db_url, 
    {
        ha: true, // Make sure the high availability checks are on
        haInterval: 5000,
        auto_reconnect: true,
        keepAlive: 1,
        connectTimeoutMS: 30000,
        socketTimeoutMS: 30000
    },
    function(err, client) {....}
);

有关详细信息和更多示例,请参阅https://github.com/mongodb/node-mongodb-native/blob/3.0.0/lib/mongo_client.js