SimpleMongoDbFactory类已经失效,被SimpleMongoClientDbFactory替代

老版本的mongodbtemplate连接池的用法

 

spring:
  data:
    mongodb:
      address: 127.0.0.1:37017
      replica-set:
      database: xxx
      username: xxx
      password: xxxx
      # Configure spring.data.mongodbDB Pool
      min-connections-per-host: 10
      max-connections-per-host: 100
      threads-allowed-to-block-for-connection-multiplier: 5
      server-selection-timeout: 30000
      max-wait-time: 120000
      max-connection-idle-time: 0
      max-connection-life-time: 0
      connect-timeout: 10000
      socket-timeout: 0
      socket-keep-alive: false
      ssl-enabled: false
      ssl-invalid-host-name-allowed: false
      always-use-m-beans: false
      heartbeat-socket-timeout: 20000
      heartbeat-connect-timeout: 20000
      min-heartbeat-frequency: 500
      heartbeat-frequency: 10000
      local-threshold: 15
      authentication-database: admin

 

import java.util.List;

import lombok.Data;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Data
@Component
//@PropertySource(value = "classpath:mongo-pool.properties")
@ConfigurationProperties(prefix = "spring.data.mongodb")
public class MongoSettingsProperties {
 
    private List<String> address;
    private String replicaSet;
    private String database;
    private String username;
    private String password;
    private Integer minConnectionsPerHost = 0;
    private Integer maxConnectionsPerHost = 100;
    private Integer threadsAllowedToBlockForConnectionMultiplier = 5;
    private Integer serverSelectionTimeout = 30000;
    private Integer maxWaitTime = 120000;
    private Integer maxConnectionIdleTime = 0;
    private Integer maxConnectionLifeTime = 0;
    private Integer connectTimeout = 10000;
    private Integer socketTimeout = 0;
    private Boolean socketKeepAlive = false;
    private Boolean sslEnabled = false;
    private Boolean sslInvalidHostNameAllowed = false;
    private Boolean alwaysUseMBeans = false;
    private Integer heartbeatConnectTimeout = 20000;
    private Integer heartbeatSocketTimeout = 20000;
    private Integer minHeartbeatFrequency = 500;
    private Integer heartbeatFrequency = 10000;
    private Integer localThreshold = 15;
    private String authenticationDatabase;
 
 
}

 

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;

@Configuration
public class MongoConfig {
    // 覆盖默认的MongoDbFactory
    @Bean
    public MongoDbFactory mongoDbFactory(MongoSettingsProperties mongoSettingsProperties) {
        // 客户端配置(连接数、副本集群验证)
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        builder.connectionsPerHost(mongoSettingsProperties.getMaxConnectionsPerHost());
        builder.minConnectionsPerHost(mongoSettingsProperties.getMinConnectionsPerHost());
        if (mongoSettingsProperties.getReplicaSet() != null) {
            builder.requiredReplicaSetName(mongoSettingsProperties.getReplicaSet());
        }
        builder.threadsAllowedToBlockForConnectionMultiplier(
                mongoSettingsProperties.getThreadsAllowedToBlockForConnectionMultiplier());
        builder.serverSelectionTimeout(mongoSettingsProperties.getServerSelectionTimeout());
        builder.maxWaitTime(mongoSettingsProperties.getMaxWaitTime());
        builder.maxConnectionIdleTime(mongoSettingsProperties.getMaxConnectionIdleTime());
        builder.maxConnectionLifeTime(mongoSettingsProperties.getMaxConnectionLifeTime());
        builder.connectTimeout(mongoSettingsProperties.getConnectTimeout());
        builder.socketTimeout(mongoSettingsProperties.getSocketTimeout());
        builder.sslEnabled(mongoSettingsProperties.getSslEnabled());
        builder.sslInvalidHostNameAllowed(mongoSettingsProperties.getSslInvalidHostNameAllowed());
        builder.alwaysUseMBeans(mongoSettingsProperties.getAlwaysUseMBeans());
        builder.heartbeatFrequency(mongoSettingsProperties.getHeartbeatFrequency());
        builder.minHeartbeatFrequency(mongoSettingsProperties.getMinHeartbeatFrequency());
        builder.heartbeatConnectTimeout(mongoSettingsProperties.getHeartbeatConnectTimeout());
        builder.heartbeatSocketTimeout(mongoSettingsProperties.getHeartbeatSocketTimeout());
        builder.localThreshold(mongoSettingsProperties.getLocalThreshold());
        
        MongoClientOptions mongoClientOptions = builder.build();

        // MongoDB地址列表
        List<ServerAddress> serverAddresses = new ArrayList<>();
        for (String address : mongoSettingsProperties.getAddress()) {
            String[] hostAndPort = address.split(":");
            String host = hostAndPort[0];
            Integer port = Integer.parseInt(hostAndPort[1]);
            ServerAddress serverAddress = new ServerAddress(host, port);
            serverAddresses.add(serverAddress);
        }
        System.out.println("serverAddresses:" + serverAddresses.toString());

        // 连接认证
        //List<MongoCredential> mongoCredentialList = new ArrayList<>();
        // 创建客户端和Factory
        MongoClient mongoClient =null;
        if (mongoSettingsProperties.getUsername() != null) {
            MongoCredential mongoCredential= MongoCredential.createScramSha1Credential(
                            mongoSettingsProperties.getUsername(),
                            mongoSettingsProperties.getAuthenticationDatabase() != null ? mongoSettingsProperties
                                    .getAuthenticationDatabase() : mongoSettingsProperties.getDatabase(),
                            mongoSettingsProperties.getPassword().toCharArray());
            mongoClient= new MongoClient(serverAddresses,mongoCredential, mongoClientOptions);
        }
        else{
            mongoClient= new MongoClient(serverAddresses, mongoClientOptions);
        }


        MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient,mongoSettingsProperties.getDatabase());

        return mongoDbFactory;
    }
}

 

SimpleMongoDbFactory类已经失效,被SimpleMongoClientDbFactory替代

 

 SimpleMongoDbFactory类已经失效,被SimpleMongoClientDbFactory替代

 

springboot2.2.0升级过程

MongoDB Driver:使用正确的姿势连接复制集

 

上一篇:Java-Builder模式


下一篇:项目引用autofac