现在的位置: 首页 > 综合 > 正文

java链接mongo

2017年12月23日 ⁄ 综合 ⁄ 共 3887字 ⁄ 字号 评论关闭

一、链接方式:

1、单台服务器或主从模式:

点击(此处)折叠或打开

  1. Mongo mongo1
    =
    new Mongo(
    "127.0.0.1" );
  2. Mongo mongo2 =
    new Mongo(
    "127.0.0.1"
    , 27017
    )
    ;
  3. Mongo mongo3 =
    new Mongo(
    new
    DBAddress(
    "127.0.0.1"
    , 27017,
    "test" )
    )
    ;
  4. Mongo mongo4 =
    new Mongo(
    new
    ServerAddress(
    "127.0.0.1"
    )
    )
    ;

四种方式均可,默认的链接端口是27017。

2、复制集模式链接:请注意,单台机器上配置的复制集最好使用真实ip初始化和链接,不建议使用127.0.0.1或者localhost,否则容易链接不上,详见此篇blog

点击(此处)折叠或打开

  1. static Mongo m = null;
  2. static{
  3. try {
  4. List<ServerAddress> list= new ArrayList<ServerAddress>();
  5. ServerAddress sap0 = new ServerAddress("192.168.132.100",20011);
  6. ServerAddress sas1 = new ServerAddress("192.168.132.100",20012);
  7. ServerAddress sas2 = new ServerAddress("192.168.132.100",20013);
  8. list.add(sap0);
  9. list.add(sas1);
  10. list.add(sas2);
  11. m = new Mongo(list);
  12. } catch (UnknownHostException e) {
  13. // TODO Auto-generated catch block
  14. e.printStackTrace();
  15. }
  16. }


二、连接选项:
mongo-java-driver中提供了一个类MongoOption,用于初始化链接参数,主要有一下这些:
1、常规选项:
①、
connectionsPerHost:单个主机连接到mongo实例允许的最大连接数。这其实相当于c3p0的maxPoolSize参数,mongo是内建连接池的,功能跟c3p0等差不多,超过了就会新建链接,默认值是10,大并发的话较小。
②、threadsAllowedToBlockForConnectionMultiplier:这个参数是跟connectionsPerHost配套的,当连接数超过connectionsPerHost的时候,需要建立新的连接,连接请求会被阻塞,这个参数就代表允许阻塞请求的最大值,超过这个值之后的请求都会报错。默认值5。
③、
maxWaitTime:线程等待链接可用的最长时间,ms单位,默认120,000,两分钟。
④、connectTimeout:建立链接的超时时间。默认为0,代表永不超时。
⑤、
socketTimeout:执行io操作的超时时间,默认为0,代表不超时。
⑥、
socketKeepAlive:为防火墙设置的,保证socket存活。默认false。
⑦、
autoConnectRetry:自动重连,连接池都有的参数。但是在mongo里显的比较鸡肋,不管设置false还是true,mongo-java-driver本身就有重连机制,而且当是复制集的情况下,如果主库宕机,他只会重连宕机机器的ip,我不知道这个是怎么处理的,以后看源码吧,默认false,看来本来也不建议打开。
⑧、
maxAutoConnectRetryTime:我去,坑爹啊,竟然是时间!默认为0,代表15s。。。咋想的。
⑨、
slaveOk:用于读写分离,废弃了(mongo2.0/driver2.7),详见Mongo读写分离
使用:
2、特殊选项:
Driver对数据库的写操作分几个安全级别,均是通过
WriteConcern类控制,在MongoOption中定义了WriteConcern中使用的全局参数。
①、
WriteConcern中的几个安全级别

点击(此处)折叠或打开

  1. /** No exceptions are raised, even for network issues */
  2.     public
    final
    static WriteConcern NONE
    =
    new WriteConcern(-1);

  3.     /** Exceptions are raised for network issues, but not server errors */
  4.     public
    final
    static WriteConcern NORMAL
    =
    new WriteConcern(0);

  5.     /** Exceptions are raised for network issues, and server errors; waits on a server for the write operation */
  6.     public
    final
    static WriteConcern SAFE
    =
    new WriteConcern(1);

  7.     /** Exceptions are raised for network issues, and server errors; waits on a majority of servers for the write operation */
  8.     public
    final
    static WriteConcern MAJORITY
    =
    new Majority();

  9.     /** Exceptions are raised for network issues, and server errors; the write operation waits for the server to flush the data to disk*/
  10.     public
    final
    static WriteConcern FSYNC_SAFE
    =
    new WriteConcern(true);

  11.     /** Exceptions are raised for network issues, and server errors; the write operation waits for the server to group commit to the journal file on disk*/
  12.     public
    final
    static WriteConcern JOURNAL_SAFE
    =
    new WriteConcern( 1, 0, false, true
    );

  13.     /** Exceptions are raised for network issues, and server errors; waits for at least 2 servers for the write operation*/
  14.     public
    final
    static WriteConcern REPLICAS_SAFE
    = new WriteConcern(2);

NONE的级别最低,不管出了啥事儿,客户端一股脑的往mongo插入,连网络断了都不管。REPLICAS_SAFE的级别最高,他要等从库都同步完才返回给客户端插入成功,复制集的话要至少两台同步完才行,分布式事务啊,牛叉,但是,它没有事务。。。开玩喜了。
②、参数解释:
WriteConcern的初始化函数,一通重载之后调用:

点击(此处)折叠或打开

  1. public WriteConcern(
    int w ,
    int
    wtimeout ,
    boolean
    fsync ,
    boolean
    j,
    boolean
    continueOnInsertError){
  2.         _w = w;
  3.         _wtimeout = wtimeout;
  4.         _fsync = fsync;
  5.         _j = j;
  6.         _continueOnErrorForInsert
    =
    continueOnInsertError;
  7.     }
这个函数指定写操作需要等待的server的数量和抛出异常的行为。
w:代表server的数量:。
w=-1 不等待,不做异常检查
w=0 不等待,只返回网络错误
w=1 检查本机,并检查网络错误
w>1 检查w个server,并返回网络错误
wtimeout
:写操作超时的时间。
fsync
:是不是等待刷新数据到磁盘,参见FSYNC_SAFE的注释。
j:是不是等待提交的数据已经写入到日志,并刷新到磁盘,参见JOURNAL_SAFE的注释。
MongoOption中有全局的设置。还有一个

safe
:相当于w=1,wtimeout=0,fsync和j为false,如果这几个指定了,safe不起作用。参见SAFE的注释

三、MongoOption的使用:

点击(此处)折叠或打开

  1. MongoOptions op
    =
    new MongoOptions();
  2. op.safe=true;
  3. op.connctionPerHost=50;
  4. op.connctionTimeout=120000;
  5. ....
  6. //list是serverAddress的列表
  7. Mongo m =
    new
    Mongo(list,op);

WriteConcern设置好之后用

点击(此处)折叠或打开

  1. m.setWriteConcern(wc);


调用

博客推荐文章

转载自:http://blog.chinaunix.net/uid-15795819-id-3162162.html

 

抱歉!评论已关闭.