最近花了一些時間學習了下MongoDB數據庫,感覺還是比較全面系統的,涉及了軟件安裝、客戶端操作、安全認證、副本集和分布式集群搭建,以及使用Spring Data連接MongoDB進行數據操作,收獲很大。特此記錄,以備查看。
文章目錄:
MongoDB和Java(1):Linux下的MongoDB安裝
MongoDB和Java(2):普通用戶啟動mongod進程
MongoDB和Java(4):Spring Data整合MongoDB(XML配置)
MongoDB和Java(5):Spring Data整合MongoDB(注解配置)
MongoDB和Java(6):Spring Data整合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的節點 |
分片集群搭建請參考
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副本集(帶keyfile安全認證以及用戶權限)