最简单的连接字符串就是:mongodb://127.0.0.1
当然,在真正在项目开发中,没人会这么用!!!!!!~~~回到主题,在MongoClient中,如何使用连接字符串进行连接呢?MongoDB Java Driver提供了一个 com.mongodb.MongoClientURI类型,使用方式如下:
client = new MongoClient(new MongoClientURI("mongodb://kwiner:test123@127.0.0.1/test?authMechanism=MONGODB-CR&maxPoolSize=500"));
client.close();
5.安全连接方式
MongoClient也提供了用户名和密码连接到指定数据库的方式,需要用到com.mongodb.MongoCredential,该类在mongo-java-driver的2.11.0版本中才开始提供,请注意!
MongoClientOptions clientOptions=new MongoClientOptions.Builder().connectionsPerHost(poolSize).threadsAllowedToBlockForConnectionMultiplier(200).build();
List
<MongoCredential> lstCredentials = Arrays.asList(MongoCredential.createMongoCRCredential("admin", "myinfo", "123456".toCharArray()));
client
= new MongoClient(new ServerAddress("127.0.0.1"),lstCredentials, clientOptions);
client.close();
或者
MongoClientOptions mongoOptions
=new MongoClientOptions.Builder().connectionsPerHost(poolSize).threadsAllowedToBlockForConnectionMultiplier(200).build();
MongoClient mongoClient
= new MongoClient(new ServerAddress(server, port), mongoOptions); // 每个MongoClient实例维护一个连接池 try {
db = mongoClient.getDatabase(database);
collection = db.getCollection(table);
catch (Exception e) {
mongoClient.close();
throw e;
}
6.连接池
MongoClient本身就使用了连接池,如果我们使用了MongoClientOptions,则默认是100个连接
MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
MongoClientOptions options
= builder.build();
assertEquals(
100, options.getConnectionsPerHost());//最大连接数
assertEquals(0, options.getMinConnectionsPerHost());//最小连接数
assertEquals(0, options.getMaxConnectionIdleTime());//连接的最大闲置时间
assertEquals(0, options.getMaxConnectionLifeTime());//连接的最大生存时间
assertEquals(120000, options.getMaxWaitTime());//最大等待可用连接的时间
assertEquals(10000, options.getConnectTimeout());//连接超时时间
MongoClient client = new MongoClient("127.0.0.1", customClientOptions);
client.close();
其中, 闲置时间和生存时间为0,表示无限制。
MongoClient的close方法会关闭底层连接,MongoClient的实例将变得不可用,应该根据程序的需要,适当的调用该方法,释放资源。 7.连接副本集
使用MongoDB作为数据库,基本上都会使用副本集,在这个集里面,有primary节点,又有其他secondary节点,并使用了读写分离,这个时候,使用java连接MongoDB服务应该怎么做呢? 其实很简单,就是使用一个ServerAddress集合保存副本集中的所有节点,然后作为MongoClient的构造函数的参数,并使用ReadPreference设置读写策略,注意,ReadPreference的读写策略既可以在MongoClient上设置,作用与使用MongoClient连接的所有操作,也可以设置到每次具体的集合操作上,作用域该次操作。代码如下:
在MongoClient上设置读写策略:
List<ServerAddress> addresses = new ArrayList<ServerAddress>();
ServerAddress address1
= new ServerAddress("192.168.1.136" , 27017);
ServerAddress address2
= new ServerAddress("192.168.1.137" , 27017);
ServerAddress address3
= new ServerAddress("192.168.1.138" , 27017);
addresses.add(address1);
addresses.add(address2);
addresses.add(address3);
mongoClient
= new MongoClient(lstAddrs);
mongoClient.setReadPreference (ReadPreference .primary ());
在某次操作上设置读写策略:
List<ServerAddress> addresses = new ArrayList<ServerAddress>();
ServerAddress address1
= new ServerAddress("192.168.1.136" , 27017);
ServerAddress address2
= new ServerAddress("192.168.1.137" , 27017);
ServerAddress address3
= new ServerAddress("192.168.1.138" , 27017);
addresses.add(address1);
addresses.add(address2);
addresses.add(address3);
MongoClient client
= new MongoClient(addresses);
DB db
= client.getDB( "test" );
DBCollection coll
= db.getCollection( "test" );
BasicDBObject object
= new BasicDBObject();
object.append(
"test2" , "testval2" );
//读操作从副本节点读取
ReadPreference preference =ReadPreference .secondary ();
DBObject dbObject = coll.findOne(object, null , preference);
System.out.println(dbObject);
8.mongo连接池属性设置
Mongo的实例其实就是一个数据库连接池,这个连接池里默认有10个链接。我们没有必要重新实现这个链接池,但是我们可以更改这个连接池的配置。因为Mongo的实例就是一个连接池,所以,项目中最好只存在一个Mongo的实例。
常见的配置参数:
connectionsPerHost :每个主机的连接数
threadsAllowedToBlockForConnectionMultiplier :线程队列数,它以上面connectionsPerHost值相乘的结果就是线程队列最大值。如果连接线程排满了队列就会抛出“Out of semaphores to get db”错误。
maxWaitTime :最大等待连接的线程阻塞时间
connectTimeout :连接超时的毫秒。0是默认和无限
socketTimeout :socket超时。0是默认和无限
autoConnectRetry :这个控制是否在一个连接时,系统会自动重试
还有许多配置,可以参见mongodb的API。
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com