Readpreference settings are involved in the replica set replica set, by default, both read and write are distributed primary node execution, but for less read, we want to read and write separate to share the pressure, So I want to use the secondary node to read, primary only assume the responsibility of writing (actually write can only be distributed to primary node, not modifiable).
MongoDB has 5 modes of readpreference:
Master node, default mode, read operation only in the primary node, if the primary node is not available, error or throw an exception.
The primary node is preferred, in most cases read operations on the primary node, if the primary node is unavailable, such as failover, read operations on the slave node.
From the node, the read operation is only in the slave node, if the slave node is unavailable, an error is raised, or an exception is thrown.
It is preferred to read operations from nodes, in most cases, from nodes, special cases (such as single master node schemas) on the master node.
The nearest Neighbor node, the read operation in the nearest member, may be the primary node or the slave node.
Settings in spring readpreference:
<!-- mongodb configuration --><mongo:mongo id= "Mongo" host= "${mongo.host}" port= "$ {Mongo.port} " write-concern=" NORMAL " > <mongo:options connections-per-host= "${mongo.connectionsperhost}" threads-allowed-to-block-for-connection-multiplier= "${ Mongo.threadsallowedtoblockforconnectionmultiplier} " connect-timeout= "${mongo.connecttimeout}" max-wait-time= " ${mongo.maxwaittime} " auto-connect-retry=" ${ Mongo.autoconnectretry} " socket-keep-alive=" ${ mongo.socketkeepalive} " socket-timeout=" ${mongo.socketTimeout} " slave-ok= "${mongo.slaveok}" &Nbsp; write-number= "1" write-timeout= "0" write-fsync= "false" /></mongo:mongo><!-- mongo Factory, through it to get MONGO instances, dbname for MongoDB database name, no words will be automatically created --><mongo:db-factory id= "Mongodbfactory" dbname= "Uba" mongo-ref= "MONGO" /> <!-- Read and write separation level configuration --><!-- Preferred primary node, mostly read operations on the primary node, if the primary node is unavailable, such as failover, read operations on the slave node. --><bean id= "Primarypreferredreadpreference" class= " Com.mongodb.TaggableReadPreference.PrimaryPreferredReadPreference " /><!-- Nearest neighbor, read operations in the nearest member, may be the primary node or the slave node. --><bean id= "Nearestreadpreference" class= " Com.mongodb.TaggableReadPreference.NearestReadPreference " /><!-- slave node, read operation only from node, if slave node is not available, Error or throw an exception. The problem is that the data of the secondary node is older than the primary node data. --><bean id= "Secondaryreadpreference" class=" com.mongodb.TaggableReadPreference.SecondaryReadPreference " /><!-- Read from the secondary node is preferred, and the secondary node reads the data from the master node when it is not available --><bean id= " Secondarypreferredreadpreference " class=" Com.mongodb.TaggableReadPreference.SecondaryPreferredReadPreference " /><!--the main objects of mongodb, All of the additions and deletions to MongoDB are performed through it --><bean id= "Mongotemplate" class= " Org.springframework.data.mongodb.core.MongoTemplate "> <constructor-arg name= "Mongodbfactory" ref= "Mongodbfactory" /> <property name= " Readpreference " ref=" Primarypreferredreadpreference " /></bean>
This article is from the architect's path blog, so be sure to keep this source http://lizhuquan0769.blog.51cto.com/2591147/1763287
"MongoDB" readpreference Read preferences