简体   繁体   English

MongoDB C#驱动程序:用于在副本集上进行分片的连接字符串

[英]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. 我需要按照MongoDB参考中的建议在副本集上设置分片,以实现高可用性和可伸缩性。 I have few questions about connection string and its behavior for C# driver in that scenario (code snippet below): 在这种情况下,我对C#驱动程序的连接字符串及其行为有一些疑问(下面的代码段):

  1. Is the connection string below looks right for connecting to mongos instances: mongos1, mongos2 & mongos3? 下面的连接字符串看起来是否适合连接到mongos实例:mongos1,mongos2和mongos3?

  2. What happens to client if one of the mongos instance crashes? 如果其中一个mongos实例崩溃,客户端会怎样? Will the failed call handled gracefully by retrying to second mongos instance? 通过重试第二个mongos实例是否可以很好地处理失败的呼叫? Does the client blacklist the failed mongos instance and try after sometime? 客户端是否将失败的mongos实例列入黑名单并在一段时间后尝试?

  3. If I want to set readpreference, will the driver be aware of replica set existence and honor setting ReadPreference? 如果我想设置readpreference,驱动程序是否会知道副本集的存在以及是否支持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. 1)是的,这很好。 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 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. 2)建立连接字符串的方式,您将在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. 3)驱动程序在与分片系统通信时,将简单地将读取首选项转发给mongos。 Note that mongos version 2.2 had some difficulty with read preferences. 请注意,mongos 2.2版在读取首选项方面有些困难。 I'd advise you to be on the 2.4 line. 我建议您使用2.4行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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