Spring Boot中經過依賴 spring-boot-starter-data-mongodb ,來實現 spring-data-mongodb 的自動配置。java
可是默認狀況下,Spring Boot 中,並無像使用MySQL或者Redis同樣,提供了鏈接池配置的功能。所以,咱們須要自行重寫 MongoDbFactory ,實現MongoDB客戶端鏈接的參數配置擴展。spring
須要說明的是,MongoDB的客戶端自己就是一個鏈接池,所以,咱們只須要配置客戶端便可。mongodb
配置文件api
爲了統一Spring Boot的配置,咱們要將重寫的配置也配置到 application.yml 中,前綴爲 spring.data.mongodb.custom 下(前綴可本身隨意配置):app
該配置例子中,配置了副本集,其中包含了主機 10.0.5.1:27017 和 10.0.5.1:27018 ,其它配置與Spring Boot的標準配置相似,另外, connections-per-host 爲客戶端的鏈接數, in-connections-per-host 爲客戶端最小鏈接數。spring-boot
將配置包裝成類ui
爲方便調用和可讀性,將上述配置包裝成一個配置實體類, MongoConfig.java 代碼以下:this
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; } }
覆蓋MongoDbFactoryspa
接下來,就是覆蓋Spring Boot原有的 MongoDbFactory Bean,新建文件 MongoConfig.java ,代碼以下:hibernate
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() { //客戶端配置(鏈接數、副本集羣驗證) 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()); //建立客戶端和Factory MongoClient mongoClient = new MongoClient(serverAddresses, mongoCredentialList, mongoClientOptions); MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, mongoSettingsProperties.getDatabase()); return mongoDbFactory; } }
在這裏,實現了MongoDB鏈接時,前面配置的參數的設置,按照本身的實際狀況,能夠在 new SimpleMongoDbFactory 時,增長修改本身須要的配置參數。
至此,就完成了所有配置。
轉載來源:http://blog.didispace.com/spring-boot-mongodb-connection-pool/