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

 

最近花了一些時間學習了下MongoDB數據庫,感受仍是比較全面系統的,涉及了軟件安裝、客戶端操做、安全認證、副本集和分佈式集羣搭建,以及使用Spring Data鏈接MongoDB進行數據操做,收穫很大。特此記錄,以備查看。
html

 

文章目錄:linux

MongoDB和Java(1):Linux下的MongoDB安裝mongodb

MongoDB和Java(2):普通用戶啓動mongod進程數據庫

MongoDB和Java(3):Java操做MongoB安全

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

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

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

 

一、副本集環境

主:10.10.13.195:27017
從:10.10.13.195:27018, 10.10.13.195:27019

 

副本集搭建請參考

生產環境部署MongoDB副本集(帶keyfile安全認證以及用戶權限)

 

下面咱們就簡單介紹一下Spring data整合副本集

 

二、修改鏈接屬性文件

首先修改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

 

三、修改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對象

 

作了以上改動以後,咱們就能夠測試了,很簡單。

 

四、分片集羣整合

軟件系統環境

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中存在且已經配置了分片鍵。

 

五、參考資料

MongoDB 分片集羣技術

生產環境部署MongoDB副本集(帶keyfile安全認證以及用戶權限)

相關文章
相關標籤/搜索