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.
Synopsis. For a sharded cluster, the mongos instances provide the interface between the client applications and the sharded cluster. The mongos instances route queries and write operations to the shards. From the perspective of the application, a mongos instance behaves identically to any other MongoDB instance.
The mongos process, shown in the center of figure 1, is a router that directs all reads, writes, and commands to the appropriate shard. In this way, mongos provides clients with a single point of contact with the cluster, which is what enables a sharded cluster to present the same interface as an unsharded one.
Three students from University of Saarland in Germany at the Centre for IT Security – Kai Greshake, Eric Petryka and Jens Heyens – discovered that MongoDB databases running at TCP port 27017 as a service on several thousands of commercial web servers are easily accessible on the Internet.
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)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With