Spring Boot配置MongoDB连接池
<p>Spring Boot中通过依赖 spring-boot-starter-data-mongodb ,来实现 spring-data-mongodb 的自动配置。</p><p>但是默认情况下,Spring Boot 中,并没有像使用MySQL或者Redis一样,提供了连接池配置的功能。因此,我们需要自行重写 MongoDbFactory ,实现MongoDB客户端连接的参数配置扩展。</p>
<p>需要说明的是,MongoDB的客户端本身就是一个连接池,因此,我们只需要配置客户端即可。</p>
<p><strong>配置文件</strong></p>
<p>为了统一Spring Boot的配置,我们要将重写的配置也配置到 application.yml 中,前缀为 spring.data.mongodb.custom 下(前缀可自己随意配置):</p>
<div class="jb51code">
<div id="highlighter_872541" class="syntaxhighlighterplain">
<div class="toolbar">
<div class="cnblogs_code">
<pre>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</pre>
</div>
</div>
</div>
</div>
<p>该配置例子中,配置了副本集,其中包含了主机 10.0.5.1:27017 和 10.0.5.1:27018 ,其它配置与Spring Boot的标准配置类似,另外, connections-per-host 为客户端的连接数, in-connections-per-host 为客户端最小连接数。</p>
<p><strong>将配置包装成类</strong></p>
<p>为方便调用和可读性,将上述配置包装成一个配置实体类, MongoConfig.java 代码如下:</p>
<p> </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> com.feidiao.jancee.fdiot.api.config.mongo;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.hibernate.validator.constraints.NotBlank;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.hibernate.validator.constraints.NotEmpty;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.springframework.stereotype.Component;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.springframework.validation.annotation.Validated;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.List;
@Component
@Validated
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> MongoSettingsProperties {
@NotBlank
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String database;
@NotEmpty
</span><span style="color: rgba(0, 0, 255, 1)">private</span> List<String><span style="color: rgba(0, 0, 0, 1)"> hosts;
@NotEmpty
</span><span style="color: rgba(0, 0, 255, 1)">private</span> List<Integer><span style="color: rgba(0, 0, 0, 1)"> ports;
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String replicaSet;
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String username;
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String password;
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String authenticationDatabase;
</span><span style="color: rgba(0, 0, 255, 1)">private</span> Integer minConnectionsPerHost = 10<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">private</span> Integer connectionsPerHost = 2<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> MongoSettingsProperties() {
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getDatabase() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> database;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setDatabase(String database) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.database =<span style="color: rgba(0, 0, 0, 1)"> database;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> List<String><span style="color: rgba(0, 0, 0, 1)"> getHosts() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> hosts;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setHosts(List<String><span style="color: rgba(0, 0, 0, 1)"> hosts) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.hosts =<span style="color: rgba(0, 0, 0, 1)"> hosts;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> List<Integer><span style="color: rgba(0, 0, 0, 1)"> getPorts() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> ports;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setPorts(List<Integer><span style="color: rgba(0, 0, 0, 1)"> ports) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.ports =<span style="color: rgba(0, 0, 0, 1)"> ports;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getReplicaSet() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> replicaSet;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setReplicaSet(String replicaSet) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.replicaSet =<span style="color: rgba(0, 0, 0, 1)"> replicaSet;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getUsername() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> username;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setUsername(String username) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.username =<span style="color: rgba(0, 0, 0, 1)"> username;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getPassword() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> password;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setPassword(String password) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.password =<span style="color: rgba(0, 0, 0, 1)"> password;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getAuthenticationDatabase() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> authenticationDatabase;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setAuthenticationDatabase(String authenticationDatabase) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.authenticationDatabase =<span style="color: rgba(0, 0, 0, 1)"> authenticationDatabase;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Integer getMinConnectionsPerHost() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> minConnectionsPerHost;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setMinConnectionsPerHost(Integer minConnectionsPerHost) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.minConnectionsPerHost =<span style="color: rgba(0, 0, 0, 1)"> minConnectionsPerHost;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Integer getConnectionsPerHost() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> connectionsPerHost;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setConnectionsPerHost(Integer connectionsPerHost){
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.connectionsPerHost =<span style="color: rgba(0, 0, 0, 1)"> connectionsPerHost;
}
}</span></pre>
</div>
<p><strong>覆盖MongoDbFactory</strong></p>
<p>接下来,就是覆盖Spring Boot原有的 MongoDbFactory Bean,新建文件 MongoConfig.java ,代码如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.mongodb.MongoClient;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.mongodb.MongoClientOptions;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.mongodb.MongoCredential;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.mongodb.ServerAddress;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.springframework.beans.factory.annotation.Autowired;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.springframework.boot.context.properties.ConfigurationProperties;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.springframework.context.annotation.Bean;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.springframework.context.annotation.Configuration;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.springframework.data.mongodb.MongoDbFactory;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.springframework.data.mongodb.core.SimpleMongoDbFactory;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.ArrayList;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.List;
@Configuration
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> MongoConfig {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 注入配置实体</span>
<span style="color: rgba(0, 0, 0, 1)"> @Autowired
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> MongoSettingsProperties mongoSettingsProperties;
@Bean
@ConfigurationProperties(
prefix </span>= "spring.data.mongodb.custom"<span style="color: rgba(0, 0, 0, 1)">)
MongoSettingsProperties mongoSettingsProperties() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MongoSettingsProperties();
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 覆盖默认的MongoDbFactory</span>
<span style="color: rgba(0, 0, 0, 1)"> @Bean
MongoDbFactory mongoDbFactory() {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">客户端配置(连接数、副本集群验证)</span>
MongoClientOptions.Builder builder = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MongoClientOptions.Builder();
builder.connectionsPerHost(mongoSettingsProperties.getConnectionsPerHost());
builder.minConnectionsPerHost(mongoSettingsProperties.getMinConnectionsPerHost());
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (mongoSettingsProperties.getReplicaSet() != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
builder.requiredReplicaSetName(mongoSettingsProperties.getReplicaSet());
}
MongoClientOptions mongoClientOptions </span>=<span style="color: rgba(0, 0, 0, 1)"> builder.build();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> MongoDB地址列表</span>
List<ServerAddress> serverAddresses = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">();
</span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String host : mongoSettingsProperties.getHosts()) {
Integer index </span>=<span style="color: rgba(0, 0, 0, 1)"> mongoSettingsProperties.getHosts().indexOf(host);
Integer port </span>=<span style="color: rgba(0, 0, 0, 1)"> mongoSettingsProperties.getPorts().get(index);
ServerAddress serverAddress </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServerAddress(host, port);
serverAddresses.add(serverAddress);
}
System.out.println(</span>"serverAddresses:" +<span style="color: rgba(0, 0, 0, 1)"> serverAddresses.toString());
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接认证</span>
List<MongoCredential> mongoCredentialList = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">();
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (mongoSettingsProperties.getUsername() != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
mongoCredentialList.add(MongoCredential.createScramSha1Credential(
mongoSettingsProperties.getUsername(),
mongoSettingsProperties.getAuthenticationDatabase() </span>!= <span style="color: rgba(0, 0, 255, 1)">null</span> ?<span style="color: rgba(0, 0, 0, 1)"> mongoSettingsProperties.getAuthenticationDatabase() : mongoSettingsProperties.getDatabase(),
mongoSettingsProperties.getPassword().toCharArray()));
}
System.out.println(</span>"mongoCredentialList:" +<span style="color: rgba(0, 0, 0, 1)"> mongoCredentialList.toString());
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建客户端和Factory</span>
MongoClient mongoClient = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MongoClient(serverAddresses, mongoCredentialList, mongoClientOptions);
MongoDbFactory mongoDbFactory </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SimpleMongoDbFactory(mongoClient, mongoSettingsProperties.getDatabase());
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> mongoDbFactory;
}
}</span></pre>
</div>
<p>在这里,实现了MongoDB连接时,前面配置的参数的设置,按照自己的实际情况,可以在 new SimpleMongoDbFactory 时,增加修改自己需要的配置参数。</p>
<p>至此,就完成了全部配置。</p>
<p> </p>
<p>转载来源:http://blog.didispace.com/spring-boot-mongodb-connection-pool/</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/taich-flute/p/11755254.html
頁:
[1]