Spring Boot中使用MongoDB的连接池配置的方法
因为今天开发遇到了性能问题,可能与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 下(前缀可自己随意配置):
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 代码如下:
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() { //客户端配置(连接数、副本集群验证) 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 时,增加修改自己需要的配置参数。
至此,就完成了全部配置,运行测试即可。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: Java构造函数与普通函数用法详解
推荐阅读
-
spring-boot读取props和yml配置文件的方法
-
详解Spring Boot配置使用Logback进行日志记录的实战
-
vue2中使用sass并配置全局的sass样式变量的方法
-
在Spring Boot中使用swagger-bootstrap-ui的方法
-
详解Spring Boot下Druid连接池的使用配置分析
-
Spring boot redis cache的key的使用方法
-
Spring Boot 与 Kotlin 使用JdbcTemplate连接MySQL数据库的方法
-
Spring Boot使用yml格式进行配置的方法
-
spring boot 中设置默认网页的方法
-
Spring Boot 与 Kotlin 使用Redis数据库的配置方法