+-
springboot配置mongodb连接池
首页 专栏 数据库 文章详情
0

springboot配置mongodb连接池

初窥门径 发布于 今天 01:51

application.yml 配置

mongodb:
  address: localhost:27017
  database: soms
  username: admin
  password: 123456
  # 连接池配置
  clientName: soms-task # 客户端的标识,用于定位请求来源等
  connectionTimeoutMs: 10000     # TCP连接超时,毫秒
  readTimeoutMs: 15000       # TCP读取超时,毫秒
  poolMaxWaitTimeMs: 3000        #当连接池无可用连接时客户端阻塞等待的时长,单位毫秒
  connectionMaxIdleTimeMs: 60000   #TCP连接闲置时间,单位毫秒
  connectionMaxLifeTimeMs: 120000    #TCP连接最多可以使用多久,单位毫秒
  heartbeatFrequencyMs: 20000      #心跳检测发送频率,单位毫秒
  minHeartbeatFrequencyMs: 8000    #最小的心跳检测发送频率,单位毫秒
  heartbeatConnectionTimeoutMs: 10000  #心跳检测TCP连接超时,单位毫秒
  heartbeatReadTimeoutMs: 15000    #心跳检测TCP连接读取超时,单位毫秒
  connectionsPerHost: 100       # 每个host的TCP连接数
  minConnectionsPerHost: 5     #每个host的最小TCP连接数
 #计算允许多少个线程阻塞等待可用TCP连接时的乘数,算法: threadsAllowedToBlockForConnectionMultiplier*connectionsPerHost,当前配置允许10*20个线程阻塞   
  threadsAllowedToBlockForConnectionMultiplier: 10

用来获取配置参数的类

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;
@Data
@Validated
@Component
@ConfigurationProperties(prefix = "mongodb")
public class MongoClientOptionProperties {
    /** 基础连接参数 */
 private String database;
 private String username;
 private String password;
 @NotNull
 private List<String> address;
 /** 客户端连接池参数 */
 @NotNull
 @Size(min = 1)
    private String clientName;
 /** socket连接超时时间 */
 @Min(value = 1)
    private int connectionTimeoutMs;
 /** socket读取超时时间 */
 @Min(value = 1)
    private int readTimeoutMs;
 /** 连接池获取链接等待时间 */
 @Min(value = 1)
    private int poolMaxWaitTimeMs;
 /** 连接闲置时间 */
 @Min(value = 1)
    private int connectionMaxIdleTimeMs;
 /** 连接最多可以使用多久 */
 @Min(value = 1)
    private int connectionMaxLifeTimeMs;
 /** 心跳检测发送频率 */
 @Min(value = 2000)
    private int heartbeatFrequencyMs;
 /** 最小的心跳检测发送频率 */
 @Min(value = 300)
    private int minHeartbeatFrequencyMs;
 /** 计算允许多少个线程阻塞等待时的乘数,算法:threadsAllowedToBlockForConnectionMultiplier*connectionsPerHost */
 @Min(value = 1)
    private int threadsAllowedToBlockForConnectionMultiplier;
 /** 心跳检测连接超时时间 */
 @Min(value = 200)
    private int heartbeatConnectionTimeoutMs;
 /** 心跳检测读取超时时间 */
 @Min(value = 200)
    private int heartbeatReadTimeoutMs;
 /** 每个host最大连接数 */
 @Min(value = 1)
    private int connectionsPerHost;
 /** 每个host的最小连接数 */
 @Min(value = 1)
    private int minConnectionsPerHost;
}

配置类

package com.alkin.soms.common.mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.*;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class MongoConfig {
    private final Logger log = LoggerFactory.getLogger(MongoConfig.class);
 /**
 * 自定义mongo连接池 * * @param properties
 * @return
 */
 @Bean
 @Autowired public MongoDbFactory mongoDbFactory(MongoClientOptionProperties properties) {
        //创建客户端参数
 MongoClientOptions options = mongoClientOptions(properties);
 //创建客户端和Factory
 List<ServerAddress> serverAddresses = new ArrayList<>();
 for (String address : properties.getAddress()) {
            String[] hostAndPort = address.split(":");
 String host = hostAndPort[0];
 int port = Integer.parseInt(hostAndPort[1]);
 ServerAddress serverAddress = new ServerAddress(host, port);
 serverAddresses.add(serverAddress);
 }
        String username = properties.getUsername();
 String password = properties.getPassword();
 String database = properties.getDatabase();
 MongoClient mongoClient;
 if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) {
            //创建认证客户端
 MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(
                    username,
 database,
 password.toCharArray());
 mongoClient = new MongoClient(serverAddresses.get(0), mongoCredential, options);
 } else {
            //创建非认证客户端
 mongoClient = new MongoClient(serverAddresses, options);
 }
        SimpleMongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, database);
 log.info("mongodb注入成功");
 return mongoDbFactory;
 }
    @Bean(name = "mongoTemplate")
    @Autowired
 public MongoTemplate getMongoTemplate(MongoDbFactory mongoDbFactory) {
        return new MongoTemplate(mongoDbFactory);
 }
    /**
 * mongo客户端参数配置 * * @return
 */
 public MongoClientOptions mongoClientOptions(MongoClientOptionProperties properties) {
        return MongoClientOptions.builder()
                .connectTimeout(properties.getConnectionTimeoutMs())
                .socketTimeout(properties.getReadTimeoutMs()).applicationName(properties.getClientName())
                .heartbeatConnectTimeout(properties.getHeartbeatConnectionTimeoutMs())
                .heartbeatSocketTimeout(properties.getHeartbeatReadTimeoutMs())
                .heartbeatFrequency(properties.getHeartbeatFrequencyMs())
                .minHeartbeatFrequency(properties.getMinHeartbeatFrequencyMs())
                .maxConnectionIdleTime(properties.getConnectionMaxIdleTimeMs())
                .maxConnectionLifeTime(properties.getConnectionMaxLifeTimeMs())
                .maxWaitTime(properties.getPoolMaxWaitTimeMs())
                .connectionsPerHost(properties.getConnectionsPerHost())
                .threadsAllowedToBlockForConnectionMultiplier(
                        properties.getThreadsAllowedToBlockForConnectionMultiplier())
                .minConnectionsPerHost(properties.getMinConnectionsPerHost()).build();
 }
    @Bean
 public MappingMongoConverter mappingMongoConverter(MongoDbFactory factory, MongoMappingContext context, BeanFactory beanFactory) {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
 MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
 try {
            mappingConverter.setCustomConversions(beanFactory.getBean(MongoCustomConversions.class));
 } catch (NoSuchBeanDefinitionException ignore) {
        }
        // Don't save _class to dao
 mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
 return mappingConverter;
 }
}
mongodb 数据库
阅读 31 发布于 今天 01:51
收藏
分享
本作品系原创, 采用《署名-非商业性使用-禁止演绎 4.0 国际》许可协议
avatar
初窥门径

java程序猿

3 声望
2 粉丝
关注作者
0 条评论
得票 时间
提交评论
avatar
初窥门径

java程序猿

3 声望
2 粉丝
关注作者
宣传栏

application.yml 配置

mongodb:
  address: localhost:27017
  database: soms
  username: admin
  password: 123456
  # 连接池配置
  clientName: soms-task # 客户端的标识,用于定位请求来源等
  connectionTimeoutMs: 10000     # TCP连接超时,毫秒
  readTimeoutMs: 15000       # TCP读取超时,毫秒
  poolMaxWaitTimeMs: 3000        #当连接池无可用连接时客户端阻塞等待的时长,单位毫秒
  connectionMaxIdleTimeMs: 60000   #TCP连接闲置时间,单位毫秒
  connectionMaxLifeTimeMs: 120000    #TCP连接最多可以使用多久,单位毫秒
  heartbeatFrequencyMs: 20000      #心跳检测发送频率,单位毫秒
  minHeartbeatFrequencyMs: 8000    #最小的心跳检测发送频率,单位毫秒
  heartbeatConnectionTimeoutMs: 10000  #心跳检测TCP连接超时,单位毫秒
  heartbeatReadTimeoutMs: 15000    #心跳检测TCP连接读取超时,单位毫秒
  connectionsPerHost: 100       # 每个host的TCP连接数
  minConnectionsPerHost: 5     #每个host的最小TCP连接数
 #计算允许多少个线程阻塞等待可用TCP连接时的乘数,算法: threadsAllowedToBlockForConnectionMultiplier*connectionsPerHost,当前配置允许10*20个线程阻塞   
  threadsAllowedToBlockForConnectionMultiplier: 10

用来获取配置参数的类

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;
@Data
@Validated
@Component
@ConfigurationProperties(prefix = "mongodb")
public class MongoClientOptionProperties {
    /** 基础连接参数 */
 private String database;
 private String username;
 private String password;
 @NotNull
 private List<String> address;
 /** 客户端连接池参数 */
 @NotNull
 @Size(min = 1)
    private String clientName;
 /** socket连接超时时间 */
 @Min(value = 1)
    private int connectionTimeoutMs;
 /** socket读取超时时间 */
 @Min(value = 1)
    private int readTimeoutMs;
 /** 连接池获取链接等待时间 */
 @Min(value = 1)
    private int poolMaxWaitTimeMs;
 /** 连接闲置时间 */
 @Min(value = 1)
    private int connectionMaxIdleTimeMs;
 /** 连接最多可以使用多久 */
 @Min(value = 1)
    private int connectionMaxLifeTimeMs;
 /** 心跳检测发送频率 */
 @Min(value = 2000)
    private int heartbeatFrequencyMs;
 /** 最小的心跳检测发送频率 */
 @Min(value = 300)
    private int minHeartbeatFrequencyMs;
 /** 计算允许多少个线程阻塞等待时的乘数,算法:threadsAllowedToBlockForConnectionMultiplier*connectionsPerHost */
 @Min(value = 1)
    private int threadsAllowedToBlockForConnectionMultiplier;
 /** 心跳检测连接超时时间 */
 @Min(value = 200)
    private int heartbeatConnectionTimeoutMs;
 /** 心跳检测读取超时时间 */
 @Min(value = 200)
    private int heartbeatReadTimeoutMs;
 /** 每个host最大连接数 */
 @Min(value = 1)
    private int connectionsPerHost;
 /** 每个host的最小连接数 */
 @Min(value = 1)
    private int minConnectionsPerHost;
}

配置类

package com.alkin.soms.common.mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.*;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class MongoConfig {
    private final Logger log = LoggerFactory.getLogger(MongoConfig.class);
 /**
 * 自定义mongo连接池 * * @param properties
 * @return
 */
 @Bean
 @Autowired public MongoDbFactory mongoDbFactory(MongoClientOptionProperties properties) {
        //创建客户端参数
 MongoClientOptions options = mongoClientOptions(properties);
 //创建客户端和Factory
 List<ServerAddress> serverAddresses = new ArrayList<>();
 for (String address : properties.getAddress()) {
            String[] hostAndPort = address.split(":");
 String host = hostAndPort[0];
 int port = Integer.parseInt(hostAndPort[1]);
 ServerAddress serverAddress = new ServerAddress(host, port);
 serverAddresses.add(serverAddress);
 }
        String username = properties.getUsername();
 String password = properties.getPassword();
 String database = properties.getDatabase();
 MongoClient mongoClient;
 if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) {
            //创建认证客户端
 MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(
                    username,
 database,
 password.toCharArray());
 mongoClient = new MongoClient(serverAddresses.get(0), mongoCredential, options);
 } else {
            //创建非认证客户端
 mongoClient = new MongoClient(serverAddresses, options);
 }
        SimpleMongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, database);
 log.info("mongodb注入成功");
 return mongoDbFactory;
 }
    @Bean(name = "mongoTemplate")
    @Autowired
 public MongoTemplate getMongoTemplate(MongoDbFactory mongoDbFactory) {
        return new MongoTemplate(mongoDbFactory);
 }
    /**
 * mongo客户端参数配置 * * @return
 */
 public MongoClientOptions mongoClientOptions(MongoClientOptionProperties properties) {
        return MongoClientOptions.builder()
                .connectTimeout(properties.getConnectionTimeoutMs())
                .socketTimeout(properties.getReadTimeoutMs()).applicationName(properties.getClientName())
                .heartbeatConnectTimeout(properties.getHeartbeatConnectionTimeoutMs())
                .heartbeatSocketTimeout(properties.getHeartbeatReadTimeoutMs())
                .heartbeatFrequency(properties.getHeartbeatFrequencyMs())
                .minHeartbeatFrequency(properties.getMinHeartbeatFrequencyMs())
                .maxConnectionIdleTime(properties.getConnectionMaxIdleTimeMs())
                .maxConnectionLifeTime(properties.getConnectionMaxLifeTimeMs())
                .maxWaitTime(properties.getPoolMaxWaitTimeMs())
                .connectionsPerHost(properties.getConnectionsPerHost())
                .threadsAllowedToBlockForConnectionMultiplier(
                        properties.getThreadsAllowedToBlockForConnectionMultiplier())
                .minConnectionsPerHost(properties.getMinConnectionsPerHost()).build();
 }
    @Bean
 public MappingMongoConverter mappingMongoConverter(MongoDbFactory factory, MongoMappingContext context, BeanFactory beanFactory) {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
 MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
 try {
            mappingConverter.setCustomConversions(beanFactory.getBean(MongoCustomConversions.class));
 } catch (NoSuchBeanDefinitionException ignore) {
        }
        // Don't save _class to dao
 mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
 return mappingConverter;
 }
}