时间:2021-07-01 10:21:17 帮助过:3人阅读
覆盖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 时,增加修改自己需要的配置参数。
至此,就完成了全部配置。
转载来源:http://blog.didispace.com/spring-boot-mongodb-connection-pool/
Spring Boot配置MongoDB连接池
标签:java ack pass cti data 客户端 地址 tool 配置文件