java连接多个mongoDB数据库

开发需求:

1.客户端聊天过程中需要根据玩家uid和所在聊天室ID,去对应服的游戏数据库查询该玩家的用户信息,并返回给客户端。

2.PVP记录、离线消息需要记录到另一个数据库。

之前的mongoDB工具类只考虑到连接一个数据库,现在需要根据配置文件,连接多个数据库


稍微修改了之前的mongoDB工具类,让他变得更灵活。


#db params  
  
#server=Chinese  
server=English  
  
#test数据库  
DBUrl=***  
DBPort=***  
DBName=new_game1,new_game2,new_game3,new_game4,new_game5
serverAdminDbName=server_admin
  
#简体服务器阿里云数据库  
#DBUrl=dds-***************.mongodb.rds.aliyuncs.com  
#DBPort=****  
#DefaultDB=admin  
#DBName=***  
#username=***  
#password=***  
  
#Collection  
PVPRecordCollectionName=pvp_record  
PVPDataCollectionName=pvp_data  
#最大连接数  
connectionsPerHost=100  
#最大等待时间  
maxWaitTime=300000  
#连接超时时间  
socketTimeout=100000  
#连接池最长生命时间  
maxConnectionLifeTime=150000  
#连接超时时间  
connectTimeout=25000  
#IP地址查询接口  
#ipSearchUrl=http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=  
ipSearchUrl=http://ip.taobao.com/service/getIpInfo.php?ip=  
#ipSearchUrl=http://ip-api.com/json/  

package util;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

import entity.Share;

public class MongoDBUtil {
	private static MongoDatabase mongoDBAdmin;
//	private static MongoDatabase mongoDBGame;
	private static Properties p = Share.p;
	private static List<MongoDatabase> mongoDBs = new ArrayList<MongoDatabase>();
	
	
	private static MongoDBUtil instance = new MongoDBUtil();
	
	public static MongoDBUtil getInstance(){
		
		return instance;
	}
	static {
		//获取数据库1
		String DBName = p.getProperty("DBName");
		System.out.println("-----DBName :"+DBName+"-----");
		String[] DBNames = DBName.split(",");
		MongoClient client = getConnect();
		
		for(String key : DBNames){
			MongoDatabase mongoDB = client.getDatabase(key);
//			System.out.println(mongoDB.getName());
			mongoDBs.add(mongoDB);
		}
		
		//获取数据库2
		mongoDBAdmin = client.getDatabase(p.getProperty("serverAdminDbName"));
	}
	
	public List<MongoDatabase> getGameDB(){
		return mongoDBs;
		
	}
	
	public static String getSystemParamKeyValue(String key){
		return p.getProperty(key);
	}
	
	public MongoDatabase getMongoDataBaseAdmin(){
		return mongoDBAdmin;
	}
	
	//离线消息集合
	public MongoCollection<Document> getOffCollection(){
		return mongoDBAdmin.getCollection(p.getProperty("OffCollectionName"));
	}
	//PVP单条记录集合
	public MongoCollection<Document> getPVPRecordCollection(){
		return mongoDBAdmin.getCollection(p.getProperty("PVPRecordCollectionName"));
	}
	//PVP计数集合
	public MongoCollection<Document> getPVPDataCollection(){
		return mongoDBAdmin.getCollection(p.getProperty("PVPDataCollectionName"));
	}
	//user集合
//	public MongoCollection<Document> getUserCollection(){
//		return mongoDBGame.getCollection(p.getProperty("UserCollectionName"));
//	}
	
	private static MongoClient getConnect(){	
		//String myFilePath = MongoInit.class.getResource("/").getPath() + "db.properties";
		MongoClient mongoClient = null;
		try {
//			p.load(MongoDBUtil.class.getClassLoader().getResourceAsStream("db.properties"));
//			p.load(ClassLoader.getSystemClassLoader().getResourceAsStream("resource/db.properties"));
//			System.out.println("-----p :"+p.toString());
			String DBUrl = p.getProperty("DBUrl");
//			String DBName = p.getProperty("DBName");
			int DBPort = Integer.parseInt(p.getProperty("DBPort"));
			String server = p.getProperty("server");
			
			MongoClientOptions.Builder buide = new MongoClientOptions.Builder();
			buide.connectionsPerHost(Integer.parseInt(p.getProperty("connectionsPerHost")));// 与目标数据库可以建立的最大链接数
			buide.connectTimeout(Integer.parseInt(p.getProperty("connectTimeout")));// 与数据库建立链接的超时时间
			buide.maxWaitTime(Integer.parseInt(p.getProperty("maxWaitTime")));// 一个线程成功获取到一个可用数据库之前的最大等待时间
			buide.threadsAllowedToBlockForConnectionMultiplier(100);
			buide.maxConnectionIdleTime(0);
			buide.maxConnectionLifeTime(Integer.parseInt(p.getProperty("maxConnectionLifeTime")));
			buide.socketTimeout(Integer.parseInt(p.getProperty("socketTimeout")));
			buide.socketKeepAlive(true);
			MongoClientOptions myOptions = buide.build();
			
			if(server.equals("Chinese")){
				
				mongoClient = createMongoDBClient(DBUrl, DBPort);
				
			}else{
				ServerAddress serverAddress = new ServerAddress(DBUrl, DBPort);
				mongoClient = new MongoClient(serverAddress, myOptions);
			}
			
		} catch (Exception e) {
		   e.printStackTrace();
		   System.out.println("连接超时!");
		}
		return mongoClient;
	}
	
	/*
	 * 阿里云,第一种办法,目前用的第一种
	 */
	private static MongoClient createMongoDBClient(String DBUrl, int DBPort){
		ServerAddress serverAddress = new ServerAddress(DBUrl, DBPort);  
        List<ServerAddress> addrs = new ArrayList<ServerAddress>();  
        addrs.add(serverAddress);  
          
        String username = p.getProperty("username");
		String password = p.getProperty("password");
		String defaultDB = p.getProperty("DefaultDB");
        //MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码  
        MongoCredential credential = MongoCredential.createScramSha1Credential(username, defaultDB, password.toCharArray());
        List<MongoCredential> credentials = new ArrayList<MongoCredential>();  
        credentials.add(credential);  
        
        return new MongoClient(addrs,credentials);  
	}
	
	/*
	 * 阿里云,第二种办法
	 */
	public MongoClient createMongoDBClientWithURI(String DBUrl, int DBPort) {
		ServerAddress serverAddress = new ServerAddress(DBUrl, DBPort);  
		String username = p.getProperty("username");
		String password = p.getProperty("password");
		String defaultDB = p.getProperty("DefaultDB");
        //另一种通过URI初始化
        //mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
		String url = "mongodb://" + username + ":" + password + "@" + serverAddress + "/" + defaultDB;
		System.out.println(url);
        MongoClientURI connectionString = new MongoClientURI(url);
        return new MongoClient(connectionString);
    }
	
}


但总感觉还有别的更方便的方案,先留着以后改。。。

发布了15 篇原创文章 · 获赞 6 · 访问量 4万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览