因為今天開發(fā)遇到了性能問題,可能與MongoDB的連接有關(guān),所以稍稍深入看了一下,正好搜到原來有人寫過這篇相關(guān)的內(nèi)容,所以轉(zhuǎn)載過來?;仡^有時間可以寫個擴展到SpringForAll里,主體思路還是一樣的。感謝這位美女程序媛的文章!
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:國際域名空間、虛擬主機、營銷軟件、網(wǎng)站建設(shè)、方正網(wǎng)站維護、網(wǎng)站推廣。
說明
Spring Boot中通過依賴 spring-boot-starter-data-mongodb ,來實現(xiàn) spring-data-mongodb 的自動配置。
但是默認情況下,Spring Boot 中,并沒有像使用MySQL或者redis一樣,提供了連接池配置的功能。因此,我們需要自行重寫 MongoDbFactory ,實現(xiàn)MongoDB客戶端連接的參數(shù)配置擴展。
需要說明的是,MongoDB的客戶端本身就是一個連接池,因此,我們只需要配置客戶端即可。
配置文件
為了統(tǒng)一Spring Boot的配置,我們要將重寫的配置也配置到 application.yml 中,前綴為 spring.data.mongodb.custom 下(前綴可自己隨意配置):
spring: data: mongodb: custom: hosts: - 10.0.5.1 - 10.0.5.1 ports: - 27017 - 27018 replica-set: mgset-3590061 username: jancee password: abc123 database: jancee authentication-database: admin connections-per-host: 20 min-connections-per-host: 20
該配置例子中,配置了副本集,其中包含了主機 10.0.5.1:27017 和 10.0.5.1:27018 ,其它配置與Spring Boot的標(biāo)準(zhǔn)配置類似,另外, connections-per-host 為客戶端的連接數(shù), in-connections-per-host 為客戶端最小連接數(shù)。
將配置包裝成類
為方便調(diào)用和可讀性,將上述配置包裝成一個配置實體類, MongoConfig.java 代碼如下:
package com.feidiao.jancee.fdiot.api.config.mongo; import org.hibernate.validator.constraints.NotBlank; import org.hibernate.validator.constraints.NotEmpty; import org.springframework.stereotype.Component; import org.springframework.validation.annotation.Validated; import java.util.List; @Component @Validated public class MongoSettingsProperties { @NotBlank private String database; @NotEmpty private List<String> hosts; @NotEmpty private List<Integer> ports; private String replicaSet; private String username; private String password; private String authenticationDatabase; private Integer minConnectionsPerHost = 10; private Integer connectionsPerHost = 2; public MongoSettingsProperties() { } public String getDatabase() { return database; } public void setDatabase(String database) { this.database = database; } public List<String> getHosts() { return hosts; } public void setHosts(List<String> hosts) { this.hosts = hosts; } public List<Integer> getPorts() { return ports; } public void setPorts(List<Integer> ports) { this.ports = ports; } public String getReplicaSet() { return replicaSet; } public void setReplicaSet(String replicaSet) { this.replicaSet = replicaSet; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getAuthenticationDatabase() { return authenticationDatabase; } public void setAuthenticationDatabase(String authenticationDatabase) { this.authenticationDatabase = authenticationDatabase; } public Integer getMinConnectionsPerHost() { return minConnectionsPerHost; } public void setMinConnectionsPerHost(Integer minConnectionsPerHost) { this.minConnectionsPerHost = minConnectionsPerHost; } public Integer getConnectionsPerHost() { return connectionsPerHost; } public void setConnectionsPerHost(Integer connectionsPerHost) { this.connectionsPerHost = connectionsPerHost; } }
覆蓋MongoDbFactory
接下來,就是覆蓋Spring Boot原有的 MongoDbFactory Bean,新建文件 MongoConfig.java ,代碼如下:
import com.mongodb.MongoClient; import com.mongodb.MongoClientOptions; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import java.util.ArrayList; import java.util.List; @Configuration public class MongoConfig { // 注入配置實體 @Autowired private MongoSettingsProperties mongoSettingsProperties; @Bean @ConfigurationProperties( prefix = "spring.data.mongodb.custom") MongoSettingsProperties mongoSettingsProperties() { return new MongoSettingsProperties(); } // 覆蓋默認的MongoDbFactory @Bean MongoDbFactory mongoDbFactory() { //客戶端配置(連接數(shù)、副本集群驗證) MongoClientOptions.Builder builder = new MongoClientOptions.Builder(); builder.connectionsPerHost(mongoSettingsProperties.getConnectionsPerHost()); builder.minConnectionsPerHost(mongoSettingsProperties.getMinConnectionsPerHost()); if (mongoSettingsProperties.getReplicaSet() != null) { builder.requiredReplicaSetName(mongoSettingsProperties.getReplicaSet()); } MongoClientOptions mongoClientOptions = builder.build(); // MongoDB地址列表 List<ServerAddress> serverAddresses = new ArrayList<>(); for (String host : mongoSettingsProperties.getHosts()) { Integer index = mongoSettingsProperties.getHosts().indexOf(host); Integer port = mongoSettingsProperties.getPorts().get(index); ServerAddress serverAddress = new ServerAddress(host, port); serverAddresses.add(serverAddress); } System.out.println("serverAddresses:" + serverAddresses.toString()); // 連接認證 List<MongoCredential> mongoCredentialList = new ArrayList<>(); if (mongoSettingsProperties.getUsername() != null) { mongoCredentialList.add(MongoCredential.createScramSha1Credential( mongoSettingsProperties.getUsername(), mongoSettingsProperties.getAuthenticationDatabase() != null ? mongoSettingsProperties.getAuthenticationDatabase() : mongoSettingsProperties.getDatabase(), mongoSettingsProperties.getPassword().toCharArray())); } System.out.println("mongoCredentialList:" + mongoCredentialList.toString()); //創(chuàng)建客戶端和Factory MongoClient mongoClient = new MongoClient(serverAddresses, mongoCredentialList, mongoClientOptions); MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, mongoSettingsProperties.getDatabase()); return mongoDbFactory; } }
在這里,實現(xiàn)了MongoDB連接時,前面配置的參數(shù)的設(shè)置,按照自己的實際情況,可以在 new SimpleMongoDbFactory 時,增加修改自己需要的配置參數(shù)。
至此,就完成了全部配置,運行測試即可。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)頁名稱:SpringBoot中使用MongoDB的連接池配置的方法
文章源于:http://aaarwkj.com/article48/pegsep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、標(biāo)簽優(yōu)化、域名注冊、網(wǎng)站制作、品牌網(wǎng)站建設(shè)、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)