查看: 1333|回复: 0

[Java语言] Spring Boot中使用MongoDB的连接池配置的方法

发表于 2018-4-16 08:00:05

因为今天开发遇到了性能问题,可能与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. spring:
  2. data:
  3. mongodb:
  4. custom:
  5. hosts:
  6. - 10.0.5.1
  7. - 10.0.5.1
  8. ports:
  9. - 27017
  10. - 27018
  11. replica-set: mgset-3590061
  12. username: jancee
  13. password: abc123
  14. database: jancee
  15. authentication-database: admin
  16. connections-per-host: 20
  17. 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. package com.feidiao.jancee.fdiot.api.config.mongo;
  2. import org.hibernate.validator.constraints.NotBlank;
  3. import org.hibernate.validator.constraints.NotEmpty;
  4. import org.springframework.stereotype.Component;
  5. import org.springframework.validation.annotation.Validated;
  6. import java.util.List;
  7. @Component
  8. @Validated
  9. public class MongoSettingsProperties {
  10. @NotBlank
  11. private String database;
  12. @NotEmpty
  13. private List<String> hosts;
  14. @NotEmpty
  15. private List<Integer> ports;
  16. private String replicaSet;
  17. private String username;
  18. private String password;
  19. private String authenticationDatabase;
  20. private Integer minConnectionsPerHost = 10;
  21. private Integer connectionsPerHost = 2;
  22. public MongoSettingsProperties() {
  23. }
  24. public String getDatabase() {
  25. return database;
  26. }
  27. public void setDatabase(String database) {
  28. this.database = database;
  29. }
  30. public List<String> getHosts() {
  31. return hosts;
  32. }
  33. public void setHosts(List<String> hosts) {
  34. this.hosts = hosts;
  35. }
  36. public List<Integer> getPorts() {
  37. return ports;
  38. }
  39. public void setPorts(List<Integer> ports) {
  40. this.ports = ports;
  41. }
  42. public String getReplicaSet() {
  43. return replicaSet;
  44. }
  45. public void setReplicaSet(String replicaSet) {
  46. this.replicaSet = replicaSet;
  47. }
  48. public String getUsername() {
  49. return username;
  50. }
  51. public void setUsername(String username) {
  52. this.username = username;
  53. }
  54. public String getPassword() {
  55. return password;
  56. }
  57. public void setPassword(String password) {
  58. this.password = password;
  59. }
  60. public String getAuthenticationDatabase() {
  61. return authenticationDatabase;
  62. }
  63. public void setAuthenticationDatabase(String authenticationDatabase) {
  64. this.authenticationDatabase = authenticationDatabase;
  65. }
  66. public Integer getMinConnectionsPerHost() {
  67. return minConnectionsPerHost;
  68. }
  69. public void setMinConnectionsPerHost(Integer minConnectionsPerHost) {
  70. this.minConnectionsPerHost = minConnectionsPerHost;
  71. }
  72. public Integer getConnectionsPerHost() {
  73. return connectionsPerHost;
  74. }
  75. public void setConnectionsPerHost(Integer connectionsPerHost) {
  76. this.connectionsPerHost = connectionsPerHost;
  77. }
  78. }
复制代码

覆盖MongoDbFactory

接下来,就是覆盖Spring Boot原有的 MongoDbFactory Bean,新建文件 MongoConfig.java ,代码如下:

  1. import com.mongodb.MongoClient;
  2. import com.mongodb.MongoClientOptions;
  3. import com.mongodb.MongoCredential;
  4. import com.mongodb.ServerAddress;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.context.properties.ConfigurationProperties;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.data.mongodb.MongoDbFactory;
  10. import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. @Configuration
  14. public class MongoConfig {
  15. // 注入配置实体
  16. @Autowired
  17. private MongoSettingsProperties mongoSettingsProperties;
  18. @Bean
  19. @ConfigurationProperties(
  20. prefix = "spring.data.mongodb.custom")
  21. MongoSettingsProperties mongoSettingsProperties() {
  22. return new MongoSettingsProperties();
  23. }
  24. // 覆盖默认的MongoDbFactory
  25. @Bean
  26. MongoDbFactory mongoDbFactory() {
  27. //客户端配置(连接数、副本集群验证)
  28. MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
  29. builder.connectionsPerHost(mongoSettingsProperties.getConnectionsPerHost());
  30. builder.minConnectionsPerHost(mongoSettingsProperties.getMinConnectionsPerHost());
  31. if (mongoSettingsProperties.getReplicaSet() != null) {
  32. builder.requiredReplicaSetName(mongoSettingsProperties.getReplicaSet());
  33. }
  34. MongoClientOptions mongoClientOptions = builder.build();
  35. // MongoDB地址列表
  36. List<ServerAddress> serverAddresses = new ArrayList<>();
  37. for (String host : mongoSettingsProperties.getHosts()) {
  38. Integer index = mongoSettingsProperties.getHosts().indexOf(host);
  39. Integer port = mongoSettingsProperties.getPorts().get(index);
  40. ServerAddress serverAddress = new ServerAddress(host, port);
  41. serverAddresses.add(serverAddress);
  42. }
  43. System.out.println("serverAddresses:" + serverAddresses.toString());
  44. // 连接认证
  45. List<MongoCredential> mongoCredentialList = new ArrayList<>();
  46. if (mongoSettingsProperties.getUsername() != null) {
  47. mongoCredentialList.add(MongoCredential.createScramSha1Credential(
  48. mongoSettingsProperties.getUsername(),
  49. mongoSettingsProperties.getAuthenticationDatabase() != null ? mongoSettingsProperties.getAuthenticationDatabase() : mongoSettingsProperties.getDatabase(),
  50. mongoSettingsProperties.getPassword().toCharArray()));
  51. }
  52. System.out.println("mongoCredentialList:" + mongoCredentialList.toString());
  53. //创建客户端和Factory
  54. MongoClient mongoClient = new MongoClient(serverAddresses, mongoCredentialList, mongoClientOptions);
  55. MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, mongoSettingsProperties.getDatabase());
  56. return mongoDbFactory;
  57. }
  58. }
复制代码

在这里,实现了MongoDB连接时,前面配置的参数的设置,按照自己的实际情况,可以在 new SimpleMongoDbFactory 时,增加修改自己需要的配置参数。

至此,就完成了全部配置,运行测试即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报