Mongodb distributed cluster (2. Replica set) and mongodb cluster copy

Source: Internet
Author: User
Tags failover node server

Mongodb distributed cluster (2. Replica set) and mongodb cluster copy
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
<Strong> </strong> // 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.0000: 27017", priority: 10}, {_ id: 2, host: "192.168.1.138: 27017", priority: 20 }, // arbitration node: used only for voting, prevent the selection of the master node, arbitration and non-Arbitration (can only be set in 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 (); <strong> </strong>

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
<Span style = "font-size: 18px;"> // distributed cluster (multiple servers can be configured as long as one server can be run) list <ServerAddress> serverAddressList = new ArrayList <ServerAddress> (); 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); </span>

<Span style = "font-size: 18px;"> // 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 secondary node, the lowest latency from the network . </Span>
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


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.