zoukankan      html  css  js  c++  java
  • MongoDB和Java(6):Spring Data整合MongoDB副本集、分片集群

    最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装、客户端操作、安全认证、副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很大。特此记录,以备查看。

    文章目录:

    MongoDB和Java(1):Linux下的MongoDB安装

    MongoDB和Java(2):普通用户启动mongod进程

    MongoDB和Java(3):Java操作MongoB

    MongoDB和Java(4):Spring Data整合MongoDB(XML配置)

    MongoDB和Java(5):Spring Data整合MongoDB(注解配置)

    MongoDB和Java(6):Spring Data整合MongoDB副本集、分片集群

    MongoDB和Java(7):MongoDB用户管理

    本文记录如何整合Spring data和MongoDB副本集、分片集群。

    Java客户端这边的开发环境和《MongoDB和Java(5):Spring Data整合MongoDB(注解配置)》是一样的,实体类、数据层接口、测试类代码都不需要太多的改动,主要需要改的就是mongo的连接属性文件、MongoClient的创建方式。

    源代码下载
    MongoDB和Java学习代码.zip

    1、副本集环境

    主:10.10.13.195:27017
    从:10.10.13.195:27018, 10.10.13.195:27019

    副本集搭建请参考

    生产环境部署MongoDB副本集(带keyfile安全认证以及用户权限)

    下面我们就简单介绍一下Spring data整合副本集

    2、修改连接属性文件

    首先修改mongodb.properties

     1 # 副本集环境
     2 mongo.rs=10.10.13.195:27017,10.10.13.195:27018,10.10.13.195:27019
     3 
     4 # 单机环境
     5 mongo.host=10.10.13.195
     6 mongo.port=27017
     7 
     8 # 数据库和验证信息
     9 mongo.dbname=test
    10 mongo.username=xugf
    11 mongo.password=123456
    12 
    13 mongo.connectionsPerHost=8
    14 mongo.minConnectionsPerHost=3
    15 mongo.threadsAllowedToBlockForConnectionMultiplier=4
    16 mongo.connectTimeout=1000
    17 mongo.maxWaitTime=1500
    18 mongo.socketKeepAlive=true
    19 mongo.socketTimeout=1500

    增加的内容

    # 副本集环境
    mongo.rs=10.10.13.195:27017,10.10.13.195:27018,10.10.13.195:27019

    3、修改MongoConfiguration配置类

    首先修改MongoProperties类,主要是添加获取List<ServerAddress>的方法,在此只写了与副本集环境相关的代码片段:

     1 @Component
     2 @PropertySource(value = "classpath:mongodb.properties")
     3 public class MongoProperties {
     4 
     5     @Value("${mongo.rs}")
     6     private String rs;
     7 
     8     private List<ServerAddress> rsServers = new ArrayList<ServerAddress>();
     9 
    10     public String getRs() {
    11         return rs;
    12     }
    13 
    14     public void setRs(String rs) {
    15         this.rs = rs;
    16     }
    17 
    18     public List<ServerAddress> getRsServers() {
    19         try {
    20             if (rs != null && rs.trim().length() > 0) {
    21                 String[] hosts = rs.split(",");
    22                 for (String host : hosts) {
    23                     String[] ipAndPort = host.split(":");
    24                     if (ipAndPort.length == 0) {
    25                         continue;
    26                     }
    27                     if (ipAndPort.length == 1) {
    28                         rsServers.add(new ServerAddress(ipAndPort[0], 27017));
    29                     } else {
    30                         rsServers.add(new ServerAddress(ipAndPort[0], Integer
    31                                 .parseInt(ipAndPort[1])));
    32                     }
    33                 }
    34             } else {
    35                 rsServers.add(new ServerAddress("localhost", 27017));
    36                 return rsServers;
    37             }
    38         } catch (UnknownHostException e) {
    39         }
    40         return rsServers;
    41     }
    42 }

    然后修改MongoConfiguration的mongoClient方法,不再使用host + port方式创建MongoClient,而是传入一个List<ServerAddress>,MongoClient会去判断这是一个副本集环境

     1 public MongoClient mongoClient() throws Exception {
     2 
     3     List<MongoCredential> credentialsList = new ArrayList<MongoCredential>();
     4     credentialsList.add(MongoCredential.createScramSha1Credential(
     5             mongoProperties.getUsername(), mongoProperties.getDbname(),
     6             mongoProperties.getPassword().toCharArray()));
     7 
     8     Builder builder = MongoClientOptions.builder();
     9 
    10     builder.connectionsPerHost(mongoProperties.getConnectionsPerHost());
    11     builder.threadsAllowedToBlockForConnectionMultiplier(mongoProperties
    12             .getThreadsAllowedToBlockForConnectionMultiplier());
    13     builder.connectTimeout(mongoProperties.getConnectTimeout());
    14     builder.maxWaitTime(mongoProperties.getMaxWaitTime());
    15     builder.socketKeepAlive(mongoProperties.isSocketKeepAlive());
    16     builder.socketTimeout(mongoProperties.getSocketTimeout());
    17     builder.minConnectionsPerHost(mongoProperties
    18             .getMinConnectionsPerHost());
    19 
    20     // 获取副本集服务器集合
    21     List<ServerAddress> rsServers = mongoProperties.getRsServers();
    22 
    23     System.out.println("Rs Servers: " + rsServers);
    24 
    25     return new MongoClient(rsServers, credentialsList, builder.build());
    26 }

    第21行:使用MongoProperties获取服务器地址的列表

    第25行:使用public MongoClient(List<ServerAddress> seeds, List<MongoCredential> credentialsList, MongoClientOptions options)构造方法创建MongoClient对象

    做了以上改动之后,我们就可以测试了,很简单。

    4、分片集群整合

    软件系统环境

    MongoDB版本 mongodb-linux-x86_64-rhel62-4.0.2
    服务器操作系统 CentOS 6.5
    服务器IP 10.10.13.195

    端口分配

    27016 Mongos服务器
    27020 副本集sh1,分片1的节点
    27021 副本集sh1,分片1的节点
    27022 副本集sh1,分片1的节点
    27023 副本集configSet,配置服务器
    27024 副本集configSet,配置服务器
    27025 副本集configSet,配置服务器
    27026 副本集sh2,分片2的节点
    27027 副本集sh2,分片2的节点
    27028 副本集sh2,分片2的节点

    分片集群搭建请参考

    MongoDB 分片集群技术

    Spring Data整合分片集群的方式更加简单,只需要修改连接属性文件即可,配置如下:

     1 # 分布式集群环境
     2 mongo.rs=10.10.13.195:27016
     3 
     4 # 数据库和验证信息
     5 mongo.dbname=test
     6 mongo.username=xugf
     7 mongo.password=123456
     8 
     9 mongo.connectionsPerHost=8
    10 mongo.minConnectionsPerHost=3
    11 mongo.threadsAllowedToBlockForConnectionMultiplier=4
    12 mongo.connectTimeout=1000
    13 mongo.maxWaitTime=1500
    14 mongo.socketKeepAlive=true
    15 mongo.socketTimeout=1500

    第二行,只需要去连接mongos服务就可以。

    其余的都不需要修改,前提是程序操作的实体类在mogos中存在且已经配置了分片键。

    5、参考资料

    MongoDB 分片集群技术

    生产环境部署MongoDB副本集(带keyfile安全认证以及用户权限)

  • 相关阅读:
    【转贴】Cookie + Session + OAuth + SSO
    zz淘宝商品库MySQL优化实践
    HIVE 数据倾斜调优总结zz
    数据挖掘笔记(一)
    hive函数参考手册
    hive QL(HQL)简明指南zz
    数据挖掘笔记(二)
    python format string (转)
    hive 中转义符使用问题
    关于文档管理
  • 原文地址:https://www.cnblogs.com/xugf/p/9776311.html
Copyright © 2011-2022 走看看