java程序连接单机版的mongodb:

参考:http://www.runoob.com/mongodb/mongodb-java.html

https://www.yiibai.com/mongodb/mongodb_java.html

Mongodb JDBC驱动程序,下载地址:http://mongodb.github.io/mongo-java-driver/

需要引用的jar包为:mongo-java-driver-3.9.1.jar

需要注意的是,需要将安装的单机的mongodb的配置中bind_ip=0.0.0.0 否则会连接不上,参考https://www.cnblogs.com/lindsay-chh/p/4734568.html

测试代码:

    public static void main(String[] args) {
try {
@SuppressWarnings("resource")
MongoClient mongoClient=new MongoClient("10.3.13.213" ,27017);
MongoDatabase mongoDatabase=mongoClient.getDatabase("yldb");
mongoDatabase.createCollection("yyCollection");
MongoCollection<Document> collection=mongoDatabase.getCollection("yyCollection");
Document document=new Document("title","111").append("ttt", 111).append("eee", 111222);
collection.insertOne(document);
} catch (Exception e) {
// TODO: handle exception
}

 java连接副本集模式下的MongoDB

在搭建的mongodb环境中,即使有一个节点挂了,也不会影响应用程序客户端对整个副本集的读写。

package test_mongodb;
import java.util.ArrayList;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class test2 {
public static void main(String[] args) {
java.util.List<ServerAddress> serverAddresses=new ArrayList<>();
ServerAddress address1=new ServerAddress("10.3.13.213",27017);
ServerAddress address2=new ServerAddress("10.3.2.33",27017);
ServerAddress address3=new ServerAddress("10.3.2.34",27017);
serverAddresses.add(address1);
serverAddresses.add(address2);
serverAddresses.add(address3);
MongoClient client=new MongoClient(serverAddresses);
try {
//连接数据库test
MongoDatabase mongoDatabase=client.getDatabase("test");
//连接集合testcc
MongoCollection<Document> collection=mongoDatabase.getCollection("testcc");
FindIterable<Document> docs=collection.find();
//将集合中的文档内容转换成字符串打印出来
for(Document document:docs){
String string=document.toString();
System.out.println(string);
}
} catch (Exception e) {
System.out.println(e);
}
client.close();
}
}

按上面对的部署与连接下,其实所有的读写操作都是在主节点上进行的。

读写分离:

可以设置只在副本节点上进行读,来减轻主节点的读写压力

首先需要设置副本节点的可读(默认副本节点是不可读写的) repset:SECONDARY> db.getMongo().setSlaveOK()

如下:在获取集合时设置

            //连接集合testcc
ReadPreference readPreference=ReadPreference.secondary();
MongoCollection<Document> collection=mongoDatabase.getCollection("testcc").withReadPreference(readPreference);

也可以在连接数据库时设置,方法相类似

其中:ReadPreference的选项可以选择

primary:默认参数,只从主节点上进行读取操作;

primaryPreferred:大部分从主节点上读取数据,只有主节点不可用时从secondary节点读取数据。

secondary:只从secondary节点上进行读取操作,存在的问题是secondary节点的数据会比primary节点数据“旧”。

secondaryPreferred:优先从secondary节点进行读取操作,secondary节点不可用时从主节点读取数据;

nearest:不管是主节点、secondary节点,从网络延迟最低的节点上读取数据。

参考:https://blog.csdn.net/u013066244/article/details/73835967/

https://blog.csdn.net/bao0682/article/details/47104973

最新文章

  1. setAlpha与View Layer关系
  2. asp.net 时间操作
  3. 译文---C#堆VS栈(Part Three)
  4. http gzip 解压缩
  5. 在OpenResty中使用lua-zlib的方法
  6. 线上mysql内存持续增长直至内存溢出被killed分析(已解决)
  7. java JDK8 学习笔记——第15章 通用API
  8. **apache环境下 禁止显示 index of/ 目录下(如何禁止访问网站根目录)
  9. SSL与TLS的区别以及介绍
  10. 问题分析探讨 --&gt; 大约有700W数据的表,把当天的10W数据select导入新表,整个原来的表就锁死
  11. Android动画之二:View Animation
  12. 【JQuery的选择器】
  13. 【Tomcat】重新獲得war包
  14. About Pull Strings 英语走后门议论文
  15. wxPyhon 的控件(摘抄)
  16. 打包JAR,MANIFEST.MF格式
  17. .NET开源Protobuf-net组件修炼手册
  18. 查看kubernets上的image信息
  19. 使用web3+solc编译发布以太坊智能合约
  20. linux下安装jenkins实现自动化部署

热门文章

  1. MINST手写数字识别(三)—— 使用antirectifier替换ReLU激活函数
  2. DS博客作业08--课程总结
  3. python_110_反射
  4. Bootstrap 响应式表格
  5. sha1、base64、ase加密
  6. abaqus二次开发概述
  7. [POJ] 3362 Telephone Lines
  8. virtualenvwrapper.sh报错: There was a problem running the initialization hooks.解决
  9. (62)zabbix客户端自动注册
  10. DNS服务-主从架构搭建