您好,欢迎访问一九零五行业门户网

springboot中怎么配置读写分离

springboot中配置读写分离的方法:首先配置“spring.data.mongodb.uri”;然后创建一个rest风格的“indexcontroller”;最后验证读写分离是否生效即可。
mongodb的读写分离
一、readpreference读偏好
在副本集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>
二、springboot中配置读写分离
再重申下在副本集replica set中才涉及到readpreference的设置才有意义。
请仔细看好 spring.data.mongodb.uri 的配置,他的格式如下,可以参考mongodb连接:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostn[:portn]]][/[database][?options]]
例子:
# mongodb uri配置 重要,添加了用户名和密码验证spring.data.mongodb.uri=mongodb://zhuyu:zhuyu@192.168.68.138:27017,192.168.68.137:27017,192.168.68.139:27017/ai?slaveok=true&replicaset=zypcy&write=1&readpreference=secondarypreferred&connecttimeoutms=300000#每个主机的连接数spring.data.mongodb.connections-per-host=50#线程队列数,它以上面connectionsperhost值相乘的结果就是线程队列最大值spring.data.mongodb.threads-allowed-to-block-for-connection-multiplier=50spring.data.mongodb.connect-timeout=5000spring.data.mongodb.socket-timeout=3000spring.data.mongodb.max-wait-time=1500#控制是否在一个连接时,系统会自动重试spring.data.mongodb.auto-connect-retry=truespring.data.mongodb.socket-keep-alive=true
验证读写分离是否生效:
创建一个 rest风格的 indexcontroller ,提供:添加与查询接口,访问这2个接口,看控制台输出,是否查操作自动分配到从库,写操作分配到主库
@requestmapping("/index")@restcontrollerpublic class indexcontroller { @autowired private mongotemplate mongotemplate; @requestmapping("/getlist") public list<testmodel> getlist(){ list<testmodel> list = mongotemplate.findall(testmodel.class,"test"); return list; } @requestmapping("/add") public string add(){ testmodel model = new testmodel("zhuyu" + system.currenttimemillis()); mongotemplate.insert(model , "test"); return "success"; }}
更多相关技术知识,请访问!
其它类似信息

推荐信息