Mongodb distributed cluster (2. Replica set)
Overview
Replica set is a type of master-slave replication. It is a master-slave replication that comes with the Failover function. It solves the disadvantages of the master-slave replication and does not require human intervention when the master server fails, the system automatically selects a new master server.
Deployment Diagram
TheThe figure is a copy of someone else's blog. If you are interested, you can view this person's blog. It is very good.
Replica Set Configuration
// Start the server (log on to each server) mongod -- dbpath d:/data/-- replSet repset // initialize the replica set (log on to any server) config = {_ id: "repset", members: [// _ id = replSet parameter {_ id: 0, host: "192.168.1.136: 27017"}, {_ id: 1, host: "192.168.1.20.: 27017", priority: 10}, {_ id: 2, host: "192.168.1.138: 27017", priority: 20}, // arbitration node: used for voting only, prevent the master node from being elected. Arbitration and non-Arbitration (can only be set during initialization) {_ id: 3, host: "192.168.1.139: 27017", arbiterOnly: true}]} rs. initiate (config );
The replica set configuration is also very simple. The above configuration can be implemented. many different types of nodes are involved in the replica set. You can refer to the corresponding annotations. If you have any questions, you can check them again, the following are some other operations on the replica set.
Node operation rs. status (); // view the cluster node status (log on to any server) rs. add ("192.168.1.140: 27017"); // add a replica node rs. remove ("192.168.1.140: 27017"); // delete node attribute operation (log on to the master node server) cfg = rs. conf (); // hidden node: supports voting, data backup, and cannot be used by the client (not the master node) cfg. members [1]. priority = 0; cfg. members [1]. hidden = 1; // delayed replication node: used for backup. Data is synchronized from the master node to avoid incorrect cfg. members [2]. priority = 0; cfg. members [2]. slaveDelay = 3600; // permanent copy node: prevents a node with low performance from becoming the master node cfg. members [3]. priority = 0; // backup copy node: no voting cfg. members [4]. votes = 0; rs. reconfig (cfg); read/write splitting // sets the replica node readable (replica node server) db. getMongo (). setSlaveOk ();
After the replica set is configured, we cannot read the content of the replica node server. We need to make the following settings to implement read/write splitting. Of course, the master-slave server in the previous article can achieve master-slave replication without the need for this setting.
Read/write splitting // set replica node readable (replica node server) db. getMongo (). setSlaveOk ();
For Java client operations to implement read/write splitting of replica sets, we need to make corresponding settings on the java client. The following is the core code of the java client. Of course, first, what we need to do is download the corresponding mongodb java driver
// Distributed cluster (configure multiple servers as long as one can be run) List
ServerAddressList = new ArrayList
(); ServerAddressList. add (new ServerAddress ("192.168.24.1", 10000); serverAddressList. add (new ServerAddress ("192.168.24.2", 10000); your client implements client = new Alibaba client (serverAddressList );
// Set ReadPreference readPreference = ReadPreference. secondaryPreferred (); database. setReadPreference (readPreference); primary: default parameter. Read only from the master node primaryPreferred: most of the data is read from the master node. Only when the master node is unavailable, the data is read from the secondary node. secondary: read operations are performed only on the secondary node. The problem is that the data on the secondary node is "old" than the data on the primary node. secondaryPreferred: Read operations are performed on the secondary node first, when a secondary node is unavailable, it reads data from the master node nearest: whether it is the master node or the secondary node, it reads data from the node with the lowest network latency.
The above code is not a complete code. It is some of the core code. If you want to implement this, you can write it from a single server for read/write operations, the following is an example of a simple java client on the server. If you are interested, download it and take a look. Instance code. The instance code implements operations on the fixed collection and files of mongodb.
Advantages and disadvantages of replica set
Advantages
Read/write Splitting: the master node reads and writes data. By default, the replica node cannot directly read/write data (read-only can be set)
Data backup: The copy node reads the oplog (Operation Log) of the master node and runs
Failover: when the master node goes down, the system automatically resends the master node (the higher the priority is 0-, the larger the alternative opportunity)
Disadvantages
Only clusters, not distributed
Description
Number of replica set nodes
It is better to have an odd number: Do not elect the master node
Up to 12: high replication costs
Up to 7 participating in the election: Increase the election time