简体   繁体   中英

MongoDB C# driver: connection string for sharding over replica set

I need to setup sharding over replica set as recommended in MongoDB reference for high availability & scalability. I have few questions about connection string and its behavior for C# driver in that scenario (code snippet below):

  1. Is the connection string below looks right for connecting to mongos instances: mongos1, mongos2 & mongos3?

  2. What happens to client if one of the mongos instance crashes? Will the failed call handled gracefully by retrying to second mongos instance? Does the client blacklist the failed mongos instance and try after sometime?

  3. If I want to set readpreference, will the driver be aware of replica set existence and honor setting ReadPreference?

Code snippet:

        MongoUrlBuilder bldr = new MongoUrlBuilder();
        List<MongoServerAddress> servers = new List<MongoServerAddress>();
        servers.Add(new MongoServerAddress("mongos1:27016"));
        servers.Add(new MongoServerAddress("mongos2:27016"));
        servers.Add(new MongoServerAddress("mongos3:27016"));

        bldr.Username = "myuser";
        bldr.Password = "mypwd";
        bldr.Servers = servers;
        bldr.DatabaseName = "mydb";

        bldr.ReadPreference = ReadPreference.Primary;

        var server = MongoServer.Create(bldr.ToMongoUrl());

1) Yes, this is just fine. Note that all of this could be put in an actual connection string as well. mongodb://myuser:mypwd@mongos1:27016,mongos2:27016,mongos3:27016/mydb/?readPreference=primary

2) The way your connection string is built, you'll be load balancing across the 3 mongos. If one goes down, then the other two will simply begin to receive more traffic. Errors, however, will happen and nothing gets retried automatically. You'll need to handle the errors and make decisions based on each query/write whether it is safe to retry.

3) The driver, when talking to a sharded system, will simply forward the read preference to mongos. Note that mongos version 2.2 had some difficulty with read preferences. I'd advise you to be on the 2.4 line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM