SpringBoot 整合mongoDB並自定義鏈接池

SpringBoot 整合mongoDB並自定義鏈接池

得力於SpringBoot的特性,整合mongoDB是很容易的,咱們整合mongoDB的目的就是想用它給咱們提供的mongoTemplate,它能夠很容易的操做mongoDB數據庫。html

爲了自定義鏈接池,咱們在配置類中主要與MongoClientOptions、MongoCredential、MongoClient、MongoDbFactory打交道。最終的目的就是配置好一個MongoDbFactory的bean交由Spring管理。java

Maven 依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

配置文件

mongodb:
  database: bfa_mongo
  username: "xxx"
  password: "xxxxx"
  address: "host:port"
  authenticationDatabase: [設置你的認證數據庫,若是有的話]
  # 鏈接池配置
  clientName: ${spring.application.name} # 客戶端的標識,用於定位請求來源等
  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: 20       # 每一個host的TCP鏈接數
  minConnectionsPerHost: 5     #每一個host的最小TCP鏈接數
  #計算容許多少個線程阻塞等待可用TCP鏈接時的乘數,算法:threadsAllowedToBlockForConnectionMultiplier*connectionsPerHost,當前配置容許10*20個線程阻塞
  threadsAllowedToBlockForConnectionMultiplier: 10

MongoConfig配置類

配置類中使用了lombok,若是你沒有用lombok依賴和IDE插件,你要重寫getter、Setter方法: 代碼稍長,能夠複製在IDEA中查看:算法

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Configuration
public class MongoConfig {

    /**
     * monogo 轉換器
     * @return
     */
    @Bean
    public MappingMongoConverter mappingMongoConverter(MongoDbFactory factory,
                                                       MongoMappingContext context, BeanFactory beanFactory, MongoCustomConversions conversions) {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
        MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
        // 刪除 _class field
//    mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
        mappingConverter.setCustomConversions(conversions);
        return mappingConverter;
    }

    /**
     * 自定義mongo鏈接池
     * @param properties
     * @return
     */
    @Bean
    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];
            Integer port = Integer.parseInt(hostAndPort[1]);
            ServerAddress serverAddress = new ServerAddress(host, port);
            serverAddresses.add(serverAddress);
        }

        //建立認證客戶端
        MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(properties.getUsername(),
                properties.getAuthenticationDatabase() != null ? properties.getAuthenticationDatabase() : properties.getDatabase(),
                properties.getPassword().toCharArray());

        MongoClient mongoClient = new MongoClient(serverAddresses, mongoCredential, options);

        /** ps: 建立非認證客戶端*/
        //MongoClient mongoClient = new MongoClient(serverAddresses, mongoClientOptions);
        return new SimpleMongoDbFactory(mongoClient, properties.getDatabase());
    }

    /**
     * mongo客戶端參數配置
     * @return
     */
    @Bean
    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();
    }

    @Primary
    @Bean
    @ConfigurationProperties(prefix = "mongodb")
    public MongoClientOptionProperties mongoClientOptionProperties() {
        return new MongoClientOptionProperties();
    }

    @Getter
    @Setter
    @Validated
    @Configuration
    public static class MongoClientOptionProperties {

        /** 基礎鏈接參數 */
        private String database;
        private String username;
        private String password;
        @NotNull
        private List<String> address;
        private String authenticationDatabase;

        /** 客戶端鏈接池參數 */
        @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;
    }
}

MappingMongoConverter能夠自定義mongo轉換器,主要自定義存取mongo數據時的一些操做,例如 mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null)) 方法會將mongo數據中的_class字段去掉。spring

最後經過 new SimpleMongoDbFactory(mongoClient, properties.getDatabase())方法配置了一個MongoDbFactory交由Spring管理,Springboot會拿這個MongoDbFactory工廠bean來new一個MongoTemplate,在MongoDbFactoryDependentConfiguration類下能夠看到SpringBoot幫你作得事:mongodb

/**
 * Configuration for Mongo-related beans that depend on a {@link MongoDbFactory}.
 *
 * @author Andy Wilkinson
 */
@Configuration
@ConditionalOnBean(MongoDbFactory.class)
class MongoDbFactoryDependentConfiguration {

	private final MongoProperties properties;

	MongoDbFactoryDependentConfiguration(MongoProperties properties) {
		this.properties = properties;
	}
    
    //SpringBoot建立MongoTemplate實例
	@Bean
	@ConditionalOnMissingBean
	public MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory, MongoConverter converter) {
		return new MongoTemplate(mongoDbFactory, converter);
	}

	@Bean
	@ConditionalOnMissingBean(MongoConverter.class)
	public MappingMongoConverter mappingMongoConverter(MongoDbFactory factory, MongoMappingContext context,
			MongoCustomConversions conversions) {
		DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
		MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
		mappingConverter.setCustomConversions(conversions);
		return mappingConverter;
	}
	
	//...
}

SpringBoot利用咱們配置好的MongoDbFactory在配置類中生成一個MongoTemplate,以後咱們就能夠在項目代碼中直接@Autowired了。由於用於生成MongoTemplate的MongoDbFactory是咱們本身在MongoConfig配置類中生成的,因此咱們自定義的鏈接池參數也就生效了。數據庫

原文出處:https://www.cnblogs.com/keeya/p/11992267.htmlapp

相關文章
相關標籤/搜索