mongoDB主从读写分离怎样实现,具体操作是什么

Admin 2022-11-16 群英技术资讯 385 次浏览

这篇文章主要介绍“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

  
   
   
    
     
      
       classpath:resources.properties
      
     
    
   
   
   
    
    
   
   
    
    
    
    
    
    
   
   
    
    
   
   
   
   
    
    
   
   
    
    
    
    
    
   
   
    
    
   
   
   
   
    
    
   
   
  

web.xml

  
   
    contextConfigLocation
   
   
     /WEB-INF/classes/mongo_config.xml 
   
  

实体层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
  
    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
   
     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
    
      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
     
       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
      
        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
       
         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
        
          get(String collectionName,int number){ DB db = mongo_read.getDB(dataBaseName); DBCollection dbc = db.getCollection(collectionName); List
         
           dboList = dbc.find().skip(0).limit(number).toArray(); return dboList; } public List
          
            get(String collectionName,BasicDBObject query,BasicDBObject orderBy,int n,int pageSize ){ DB db = mongo_read.getDB(dataBaseName); DBCollection dbc = db.getCollection(collectionName); List
           
             dboList = dbc.find(query).sort(orderBy).skip(n).limit(pageSize).toArray(); return dboList; } public List
            
              get(String collectionName,BasicDBObject query,int top ){ DB db = mongo_read.getDB(dataBaseName); DBCollection dbc = db.getCollection(collectionName); List
             
               dboList = dbc.find(query).skip(0).limit(top).toArray(); return dboList; } public List
              
                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; } } 
              
             
            
           
          
         
        
       
      
     
    
   
  

感谢各位的阅读,以上就是“mongoDB主从读写分离怎样实现,具体操作是什么”的内容了,经过本文的学习后,相信大家对mongoDB主从读写分离怎样实现,具体操作是什么都有更深刻的体会了吧。这里是群英网络,小编将为大家推送更多相关知识点的文章,欢迎关注! 群英智防CDN,智能加速解决方案

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

猜你喜欢

成为群英会员,开启智能安全云计算之旅

立即注册
专业资深工程师驻守
7X24小时快速响应
一站式无忧技术支持
免费备案服务
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
在线客服
微信公众号
返回顶部
返回顶部 返回顶部
在线客服
在线客服