主頁 > 知識庫 > mongoDB 實(shí)現(xiàn)主從讀寫分離實(shí)現(xiàn)的實(shí)例代碼

mongoDB 實(shí)現(xiàn)主從讀寫分離實(shí)現(xiàn)的實(shí)例代碼

熱門標(biāo)簽:電銷機(jī)器人是什么軟件 蘋果汽車租賃店地圖標(biāo)注 濟(jì)南電銷機(jī)器人加盟公司 云南外呼系統(tǒng) 怎么投訴地圖標(biāo)注 杭州人工電銷機(jī)器人價(jià)格 廣州長安公司怎樣申請400電話 呼和浩特電銷外呼系統(tǒng)加盟 老虎洗衣店地圖標(biāo)注

mongoDB主從讀寫分離

MongoDB官方已經(jīng)不建議使用主從模式了,替代方案是采用副本集的模式, 點(diǎn)擊查看。如果您的環(huán)境不符合副本集模式可參考本文,來實(shí)現(xiàn)主從讀寫分離。

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">

  !-- 連接數(shù)據(jù)庫信息 -->
  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>

實(shí)體層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,ListDBObject> 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 ListDBObject> 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 ListDBObject> 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 返回個(gè)數(shù)
   * @return
   */
  public ListDBObject> 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 ListDBObject> 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 ListDBObject> 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 ListDBObject> get(String collectionName,int number){
    DB db = mongo_read.getDB(dataBaseName);
    DBCollection dbc = db.getCollection(collectionName);  
    ListDBObject> dboList = dbc.find().skip(0).limit(number).toArray();
    return dboList;
  }
  public ListDBObject> get(String collectionName,BasicDBObject query,BasicDBObject orderBy,int n,int pageSize ){
    DB db = mongo_read.getDB(dataBaseName);
    DBCollection dbc = db.getCollection(collectionName);  
    ListDBObject> dboList = dbc.find(query).sort(orderBy).skip(n).limit(pageSize).toArray();
    return dboList;
  }
  public ListDBObject> get(String collectionName,BasicDBObject query,int top ){
    DB db = mongo_read.getDB(dataBaseName);
    DBCollection dbc = db.getCollection(collectionName);  
    ListDBObject> dboList = dbc.find(query).skip(0).limit(top).toArray();

    return dboList;
  }
  public ListDBObject> 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;
  }



}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

您可能感興趣的文章:
  • Python實(shí)現(xiàn)讀取SQLServer數(shù)據(jù)并插入到MongoDB數(shù)據(jù)庫的方法示例
  • Python實(shí)現(xiàn)批量讀取圖片并存入mongodb數(shù)據(jù)庫的方法示例
  • python讀取json文件并將數(shù)據(jù)插入到mongodb的方法
  • 了不起的node.js讀書筆記之mongodb數(shù)據(jù)庫交互
  • mongodb的寫操作
  • mongodb與sql關(guān)系型數(shù)據(jù)比較
  • mongodb使用c#驅(qū)動(dòng)數(shù)據(jù)插入demo
  • c#操作mongodb插入數(shù)據(jù)效率
  • mongodb實(shí)現(xiàn)數(shù)組對象求和方法實(shí)例
  • Mongodb讀數(shù)據(jù)操作

標(biāo)簽:自貢 無錫 廈門 遼陽 雞西 玉林 泰安 興安盟

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《mongoDB 實(shí)現(xiàn)主從讀寫分離實(shí)現(xiàn)的實(shí)例代碼》,本文關(guān)鍵詞  mongoDB,實(shí)現(xiàn),主從,讀寫,分離,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《mongoDB 實(shí)現(xiàn)主從讀寫分離實(shí)現(xiàn)的實(shí)例代碼》相關(guān)的同類信息!
  • 本頁收集關(guān)于mongoDB 實(shí)現(xiàn)主從讀寫分離實(shí)現(xiàn)的實(shí)例代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推薦文章