欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mongoDB 实现主从读写分离实现的实例代码

程序员文章站 2022-03-22 13:41:47
mongodb主从读写分离 mongodb官方已经不建议使用主从模式了,替代方案是采用副本集的模式, 点击查看。如果您的环境不符合副本集模式可参考本文,来实现主从读写分离...

mongodb主从读写分离

mongodb官方已经不建议使用主从模式了,替代方案是采用副本集的模式, 点击查看。如果您的环境不符合副本集模式可参考本文,来实现主从读写分离。

resources.properties

mongodb_read.host=10.0.0.45
mongodb_read.port=27017
mongodb_read.apname=ecsp
mongodb_read.username=
mongodb_read.password=

mongodb_write.host=10.0.0.46
mongodb_write.port=27017
mongodb_write.apname=ecsp
mongodb_write.username=
mongodb_write.password=

mongo_config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mongo="http://www.springframework.org/schema/data/mongo"
  xsi:schemalocation="http://www.springframework.org/schema/context   
     http://www.springframework.org/schema/context/spring-context-2.0.xsd   
     http://www.springframework.org/schema/data/mongo   
     http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd   
     http://www.springframework.org/schema/beans   
     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

  <!-- 连接数据库信息 -->
  <bean id="propertyconfigurer"
    class="org.springframework.beans.factory.config.propertyplaceholderconfigurer">
    <property name="locations">
      <list>
        <value>classpath:resources.properties</value>
      </list>
    </property>
  </bean>
  <!-- 读start -->
  <bean id="mongoserverreadaddress" class="com.mongodb.serveraddress">
    <constructor-arg value="${mongodb_read.host}"></constructor-arg>
    <constructor-arg value="${mongodb_read.port}"></constructor-arg>
  </bean>
  <bean id="chexunmongoreadoptions" class="com.ecsp.chexun.mongo.chexunmongooptions">
    <property name="username" value="${mongodb_read.username}" />
    <property name="password" value="${mongodb_read.password}" />
    <property name="dbname" value="${mongodb_read.apname}" />
    <property name="connectionsperhost" value="100" />
    <property name="threadsallowedtoblockforconnectionmultiplier"
      value="5" />
    <property name="slaveok" value="true" />
  </bean>
  <bean id="mongo_read" class="com.mongodb.mongo">
    <constructor-arg ref="mongoserverreadaddress"></constructor-arg>
    <constructor-arg ref="chexunmongoreadoptions"></constructor-arg>
  </bean>
  <!-- 读end -->

  <!-- 写start -->
  <bean id="mongoserverwriteaddress" class="com.mongodb.serveraddress">
    <constructor-arg value="${mongodb_write.host}"></constructor-arg>
    <constructor-arg value="${mongodb_write.port}"></constructor-arg>
  </bean>
  <bean id="chexunmongowriteoptions" class="com.ecsp.chexun.mongo.chexunmongooptions">
    <property name="username" value="${mongodb_write.username}" />
    <property name="password" value="${mongodb_write.password}" />
    <property name="dbname" value="${mongodb_write.apname}" />
    <property name="connectionsperhost" value="100" />
    <property name="threadsallowedtoblockforconnectionmultiplier"
      value="5" />
  </bean>
  <bean id="mongo_write" class="com.mongodb.mongo">
    <constructor-arg ref="mongoserverwriteaddress"></constructor-arg>
    <constructor-arg ref="chexunmongowriteoptions"></constructor-arg>
  </bean>
  <!-- 写end -->

  <!-- 链接对象注入start -->
  <bean id="mongohelper" class="com.ecsp.chexun.mongo.mongohelper">
    <constructor-arg ref="mongo_read"></constructor-arg>
    <constructor-arg ref="mongo_write"></constructor-arg>
  </bean>

  <!-- 链接对象注入end -->
</beans>

web.xml

<context-param>
  <param-name>contextconfiglocation</param-name>
  <param-value>
    /web-inf/classes/mongo_config.xml
  </param-value>
</context-param>

实体层chexunmongooptions.java

package com.ecsp.chexun.mongo;

import javax.net.socketfactory;

import com.mongodb.dbdecoderfactory;
import com.mongodb.mongooptions;

/**
 * mongo配置类
 * 
 * @author vincent.he
 * @version 1.0, created on 2012-3-12
 * 
 */
public class chexunmongooptions extends mongooptions{
  private string username;
  private string password;
  private string dbname;

  public chexunmongooptions(){
    super();
  }

  public void setdescription(string description) {
    this.description = description;
  }

  public void setconnectionsperhost(int connectionsperhost) {
    this.connectionsperhost = connectionsperhost;
  }

  public void setthreadsallowedtoblockforconnectionmultiplier(
      int threadsallowedtoblockforconnectionmultiplier) {
    this.threadsallowedtoblockforconnectionmultiplier = threadsallowedtoblockforconnectionmultiplier;
  }

  public void setmaxwaittime(int maxwaittime) {
    this.maxwaittime = maxwaittime;
  }

  public void setconnecttimeout(int connecttimeout) {
    this.connecttimeout = connecttimeout;
  }

  public void setsockettimeout(int sockettimeout) {
    this.sockettimeout = sockettimeout;
  }

  public void setsocketkeepalive(boolean socketkeepalive) {
    this.socketkeepalive = socketkeepalive;
  }

  public void setautoconnectretry(boolean autoconnectretry) {
    this.autoconnectretry = autoconnectretry;
  }

  public void setmaxautoconnectretrytime(long maxautoconnectretrytime) {
    this.maxautoconnectretrytime = maxautoconnectretrytime;
  }

  public void setslaveok(boolean slaveok) {
    this.slaveok = slaveok;
  }

  public void setdbdecoderfactory(dbdecoderfactory dbdecoderfactory) {
    this.dbdecoderfactory = dbdecoderfactory;
  }

  public void setsafe(boolean safe) {
    this.safe = safe;
  }

  public void setw(int w) {
    this.w = w;
  }

  public void setwtimeout(int wtimeout) {
    this.wtimeout = wtimeout;
  }

  public void setfsync(boolean fsync) {
    this.fsync = fsync;
  }

  public void setj(boolean j) {
    this.j = j;
  }

  public void setsocketfactory(socketfactory socketfactory) {
    this.socketfactory = socketfactory;
  }

  public string getusername() {
    return username;
  }

  public void setusername(string username) {
    this.username = username;
  }

  public string getpassword() {
    return password;
  }

  public void setpassword(string password) {
    this.password = password;
  }

  public string getdbname() {
    return dbname;
  }

  public void setdbname(string dbname) {
    this.dbname = dbname;
  }
}

java dao层

package com.ecsp.chexun.mongo;
import java.util.list;


import com.mongodb.basicdbobject;
import com.mongodb.commandresult;
import com.mongodb.db;
import com.mongodb.dbcollection;
import com.mongodb.dbobject;
import com.mongodb.mongo;

/**
 * mongo 操做方法
 * @author zhanglibing
 * 
 */
public class mongohelper {

  private mongo mongo;
  private mongo mongo_read;
  private mongo mongo_write;
  private string databasename = "cxec";
  public mongohelper(){}
  public mongohelper(mongo mongo,mongo mongo_read,mongo mongo_write){
    this.mongo = mongo;
    this.mongo_read = mongo_read;
    this.mongo_write = mongo_write;
  }
// public mongohelper(string dbname){
//   databasename = dbname;   
// }
  public int add(string collectionname,basicdbobject dbobject){
//   db db = mongomanager.getdb(databasename); 
    db db = mongo_write.getdb(databasename);
    dbcollection dbc = db.getcollection(collectionname); 
    return dbc.save(dbobject).getn();
  }
  //批量添加
  public int addall( string collectionname,list<dbobject> list){
//   db db = mongomanager.getdb(databasename); 
    db db = mongo_write.getdb(databasename);
    dbcollection dbc = db.getcollection(collectionname); 
    return dbc.insert(list).getn();
  }

  public int isexists(string collectionname,basicdbobject query){
//   db db = mongomanager.getdb(databasename); 
    db db = mongo_read.getdb(databasename);
    dbcollection dbc = db.getcollection(collectionname);
    return dbc.find(query).size();   
  }

  public list<dbobject> get(string collectionname,basicdbobject query){
//   db db = mongomanager.getdb(databasename);
    db db = mongo_read.getdb(databasename);
    dbcollection dbc = db.getcollection(collectionname);
    return dbc.find(query).toarray();
  }

  public int getcount(string collectionname,basicdbobject query){    
//   db db = mongomanager.getdb(databasename);
    db db = mongo_read.getdb(databasename);
    dbcollection dbc = db.getcollection(collectionname);
    return dbc.find(query).count();    
  }

  public list<dbobject> get(string collectionname,basicdbobject query,basicdbobject keys){
//   db db = mongomanager.getdb(databasename);
    db db = mongo_read.getdb(databasename);
    dbcollection dbc = db.getcollection(collectionname);
    return dbc.find(query, keys).toarray();
  }

  /***
   * 获取mongo 中 collection 的值
   * @param collectionname
   * @param query 查询条件
   * @param keys  查询字段
   * @param batchsize 返回个数
   * @return
   */
  public list<dbobject> get(string collectionname,basicdbobject query,basicdbobject keys,basicdbobject orderby,int batchsize){
//   db db = mongomanager.getdb(databasename);
    db db = mongo_read.getdb(databasename);
    dbcollection dbc = db.getcollection(collectionname);
    if(orderby != null){
      return dbc.find(query, keys).sort(orderby).limit(batchsize).toarray(); 
    }
    return dbc.find(query, keys).limit(batchsize).toarray();
  }

  public list<dbobject> get(string collectionname,basicdbobject query,basicdbobject keys,basicdbobject orderby,int batchsize,int n){
//   db db = mongomanager.getdb(databasename);
    db db = mongo_read.getdb(databasename);
    dbcollection dbc = db.getcollection(collectionname);
    if(orderby != null){
      return dbc.find(query, keys).sort(orderby).limit(batchsize).skip(n).toarray(); 
    }
    return dbc.find(query, keys).limit(batchsize).toarray();
  }

  public list<dbobject> get(string collectionname,dbobject query,int batchsize){
//   db db = mongomanager.getdb(databasename);
    db db = mongo_read.getdb(databasename);
    dbcollection dbc = db.getcollection(collectionname);      
    return dbc.find(query).limit(batchsize).toarray();
  }
  public list<dbobject> get(string collectionname,int number){
    db db = mongo_read.getdb(databasename);
    dbcollection dbc = db.getcollection(collectionname);  
    list<dbobject> dbolist = dbc.find().skip(0).limit(number).toarray();
    return dbolist;
  }
  public list<dbobject> get(string collectionname,basicdbobject query,basicdbobject orderby,int n,int pagesize ){
    db db = mongo_read.getdb(databasename);
    dbcollection dbc = db.getcollection(collectionname);  
    list<dbobject> dbolist = dbc.find(query).sort(orderby).skip(n).limit(pagesize).toarray();
    return dbolist;
  }
  public list<dbobject> get(string collectionname,basicdbobject query,int top ){
    db db = mongo_read.getdb(databasename);
    dbcollection dbc = db.getcollection(collectionname);  
    list<dbobject> dbolist = dbc.find(query).skip(0).limit(top).toarray();

    return dbolist;
  }
  public list<dbobject> get(string collectionname,dbobject query,dbobject orderby,int batchsize){
//   db db = mongomanager.getdb(databasename);
    db db = mongo_read.getdb(databasename);
    dbcollection dbc = db.getcollection(collectionname);
    return dbc.find(query).sort(orderby).limit(batchsize).toarray();
  }

  public int reomve(string collectionname,basicdbobject o){
//   db db = mongomanager.getdb(databasename);
    db db = mongo_write.getdb(databasename);
    dbcollection dbc = db.getcollection(collectionname);
    return dbc.remove(o).getn();
  }

  public int edit(string collectionname,basicdbobject query,basicdbobject update){
//   db db = mongomanager.getdb(databasename);
    db db = mongo_write.getdb(databasename);
    dbcollection dbc = db.getcollection(collectionname);
    return dbc.update(query, update).getn();
    //return dbc.updatemulti(query, update).getn();
  }

  public int edit(string cmd){
//   db db = mongomanager.getdb(databasename);
    db db = mongo.getdb(databasename);
    commandresult cmdresult = db.command(cmd);
    if(cmdresult.ok())
    {
      return 1;
    }
    return 0;
  }

// public dbcollection get(string collectionname){
////    db db = mongomanager.getdb(databasename);
//   db db = mongo.getdb(databasename);
//   return db.getcollection(collectionname);
// }
  public void setmongo(mongo mongo) {
    this.mongo = mongo;
  }
  public void setmongo_read(mongo mongo_read) {
    this.mongo_read = mongo_read;
  }
  public void setmongo_write(mongo mongo_write) {
    this.mongo_write = mongo_write;
  }



}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!