因为今天开发遇到了性能问题,可能与MongoDB的连接有关,所以稍稍深入看了一下,正好搜到原来有人写过这篇相关的内容,所以转载过来。回头有时间可以写个扩展到SpringForAll里,主体思路还是一样的。感谢这位美女程序媛的文章!
说明
Spring Boot中通过依赖 spring-boot-starter-data-mongodb ,来实现 spring-data-mongodb 的自动配置。
但是默认情况下,Spring Boot 中,并没有像使用MySQL或者Redis一样,提供了连接池配置的功能。因此,我们需要自行重写 MongoDbFactory ,实现MongoDB客户端连接的参数配置扩展。
需要说明的是,MongoDB的客户端本身就是一个连接池,因此,我们只需要配置客户端即可。
配置文件
为了统一Spring Boot的配置,我们要将重写的配置也配置到 application.yml 中,前缀为 spring.data.mongodb.custom 下(前缀可自己随意配置):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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的标准配置类似,另外, connections-per-host 为客户端的连接数, in-connections-per-host 为客户端最小连接数。
将配置包装成类
为方便调用和可读性,将上述配置包装成一个配置实体类, MongoConfig.java 代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
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 ,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
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