MongoDB随记
在副本集Replica Set中才涉及到ReadPreference的设置,默认情况下,读写都是分发都Primary节点执行,但是对于写少读多的情况,我们希望进行读写分离来分摊压力,所以希望使用Secondary节点来进行读取,Primary只承担写的责任(实际上写只能分发到Primary节点,不可修改)。
MongoDB有5种ReadPreference模式:
-
primary
主节点,默认模式,读操作只在主节点,如果主节点不可用,报错或者抛出异常。
-
primaryPreferred
首选主节点,大多情况下读操作在主节点,如果主节点不可用,如故障转移,读操作在从节点。
-
secondary
从节点,读操作只在从节点, 如果从节点不可用,报错或者抛出异常。
-
secondaryPreferred
首选从节点,大多情况下读操作在从节点,特殊情况(如单主节点架构)读操作在主节点。
-
nearest
最邻近节点,读操作在最邻近的成员,可能是主节点或者从节点。
Spring中的设置ReadPreference:
<!-- mongodb配置 --> <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}" write-number="1" write-timeout="0" write-fsync="false" /> </mongo:mongo> <!-- mongo的工厂,通过它来取得mongo实例,dbname为mongodb的数据库名,没有的话会自动创建 --> <mongo:db-factory id="mongoDbFactory" dbname="uba" mongo-ref="mongo" /> <!-- 读写分离级别配置 --> <!-- 首选主节点,大多情况下读操作在主节点,如果主节点不可用,如故障转移,读操作在从节点。 --> <bean id="primaryPreferredReadPreference" class="com.mongodb.TaggableReadPreference.PrimaryPreferredReadPreference" /> <!-- 最邻近节点,读操作在最邻近的成员,可能是主节点或者从节点。 --> <bean id="nearestReadPreference" class="com.mongodb.TaggableReadPreference.NearestReadPreference" /> <!-- 从节点,读操作只在从节点, 如果从节点不可用,报错或者抛出异常。存在的问题是secondary节点的数据会比primary节点数据旧。 --> <bean id="secondaryReadPreference" class="com.mongodb.TaggableReadPreference.SecondaryReadPreference" /> <!-- 优先从secondary节点进行读取操作,secondary节点不可用时从主节点读取数据 --> <bean id="secondaryPreferredReadPreference" class="com.mongodb.TaggableReadPreference.SecondaryPreferredReadPreference" /> <!-- mongodb的主要操作对象,所有对mongodb的增删改查的操作都是通过它完成 --> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> <property name="readPreference" ref="primaryPreferredReadPreference" /> </bean>
- public class MongoClientOption {
- private String description;
- private ReadPreference readPreference = ReadPreference.primary();//读写分离
- private WriteConcern writeConcern = WriteConcern.ACKNOWLEDGED;//write concern 来让用户自己衡量性能和写安全 Acknowledged 中等级别,能够拿到mongodb返回的信息。默认是这个设置
- private CodecRegistry codecRegistry = MongoClient.getDefaultCodecRegistry();
- private int minConnectionsPerHost;
- private int maxConnectionsPerHost = 100; //最大的连接数
- private int threadsAllowedToBlockForConnectionMultiplier = 5;//线程队列数,它以上面connectionsPerHost值相乘的结果就是线程队列最大值。如果连接线程排满了队列就会抛出“Out of semaphores to get db”错误。
- private int serverSelectionTimeout = 1000 * 30;
- private int maxWaitTime = 1000 * 60 * 2;//最大等待连接的线程阻塞时间2分钟
- private int maxConnectionIdleTime;
- private int maxConnectionLifeTime;//最大连接存活数
- private int connectTimeout = 1000 * 10;//连接超时的毫秒。0是默认和无限
- private int socketTimeout = 0;//socket超时。0是默认和无限
- private boolean socketKeepAlive = false;
- private boolean sslEnabled = false;
- private boolean sslInvalidHostNameAllowed = false;
- private boolean alwaysUseMBeans = false;
- private int heartbeatFrequency = 10000;
- private int minHeartbeatFrequency = 500;
- private int heartbeatConnectTimeout = 20000;
- private int heartbeatSocketTimeout = 20000;
- private int localThreshold = 15;
- private boolean autoConnectRetry = true;//是否自动连接
- private String requiredReplicaSetName;
- private DBDecoderFactory dbDecoderFactory = DefaultDBDecoder.FACTORY;//Mongo Java驱动程序配置
- private DBEncoderFactory dbEncoderFactory = DefaultDBEncoder.FACTORY;//mongo java 编码工厂
- private SocketFactory socketFactory = SocketFactory.getDefault();//socket 工厂
- private boolean cursorFinalizerEnabled = true;