简体   繁体   中英

Can I have more than 1 'mongos' instance?

I am using Java to insert data into mongodb cluster. Can I have more than 1 mongos instance so that I have a backup when 1 of my mongos is down?

Here is my java code to connect to mongos.

MongoClient mongoClient = new MongoClient("10.4.0.121",6001);
DB db = mongoClient.getDB("qbClientDB");
DBCollection collection = db.getCollection("clientInfo");

How can I specify my second mongos instance in my Java code? (If possible).

Thanks in advance.

The MongoClient docs say that you can, something similar to (the dummy addresses);

MongoClient mongoClient = new MongoClient(Arrays.asList(
   new ServerAddress("10.4.0.121",6001),
   new ServerAddress("10.4.0.122",6001),
   new ServerAddress("10.4.0.123",6001)));

MongoClient will auto-detect whether the servers are a list of replica set members or a list of mongos servers.

   public MongoClient(List<ServerAddress> seeds,
               MongoClientOptions options)


//Creates a Mongo based on a list of replica set members or a list of mongos. It will find all members (the master will be used by default). If you pass in a single server in the list, the driver will still function as if it is a replica set. If you have a standalone server, use the Mongo(ServerAddress) constructor.

//If this is a list of mongos servers, it will pick the closest (lowest ping time) one to send all requests to, and automatically fail over to the next server if the closest is down. 

  MongoClient mongoClient = new MongoClient(Arrays.asList(
  new ServerAddress("10.4.0.121",6001),
  new ServerAddress("10.4.0.122",6001),
  new ServerAddress("10.4.0.123",6001)));

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