因为Spring Boot中默认没有提供配置MongoDB连接池的属性,所以需要自己向Spring容器中注入mongoDbFactory
1. 添加依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath/> </parent> <dependencies> <!-- .. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> </dependencies>
2. 在配置文件中添加属性
在application.properties
中或者自定义的属性文件中添加属性。
# 这里添加在了application.properties中 mongo.address=localhost:27001,localhost:27002,localhost:27003 mongo.replica-set=reolicaName mongo.database=databaseName mongo.username=sherry mongo.password=123456 mongo.options.min-connections-per-host=20 mongo.options.max-connections-per-host=20 mongo.options.threads-allowed-to-block-for-connection-multiplier=5 mongo.options.server-selection-timeout=30000 mongo.options.max-wait-time=120000 mongo.options.max-connection-idel-time=0 mongo.options.max-connection-life-time=0 mongo.options.connect-timeout=10000 mongo.options.socket-timeout=0 mongo.options.socket-keep-alive=false mongo.options.ssl-enabled=false mongo.options.ssl-invalid-host-name-allowed=false mongo.options.always-use-m-beans=false mongo.options.heartbeat-socket-timeout=20000 mongo.options.heartbeat-connect-timeout=20000 mongo.options.min-heartbeat-frequency=500 mongo.options.heartbeat-frequency=10000 mongo.options.local-threshold=15
3. 把配置文件映射为Java Bean
@Component @Validated @PropertySource(value = "classpath:application.properties") @ConfigurationProperties(prefix = "mongo") @Data public class MongoSettingsProperties { @NotBlank private String database; @NotEmpty private Listaddress; private String replicaSet; 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 heartbeatFrequency = 10000; private Integer minHeartbeatFrequency = 500; private Integer heartbeatConnectTimeout = 20000; private Integer heartbeatSocketTimeout = 20000; private Integer localThreshold = 15; private String authenticationDatabase; }
4. 配置mongoDbFactory并向spring容器中注入
@Configuration public class MongoConfig { //覆盖默认的MongoDbFacotry @Bean @Autowired public MongoDbFactory mongoDbFactory(MongoSettingsProperties properties) { //客户端配置(连接数,副本集群验证) MongoClientOptions.Builder builder = new MongoClientOptions.Builder(); builder.connectionsPerHost(properties.getMaxConnectionsPerHost()); builder.minConnectionsPerHost(properties.getMinConnectionsPerHost()); if (properties.getReplicaSet() != null) { builder.requiredReplicaSetName(properties.getReplicaSet()); } builder.threadsAllowedToBlockForConnectionMultiplier( properties.getThreadsAllowedToBlockForConnectionMultiplier()); builder.serverSelectionTimeout(properties.getServerSelectionTimeout()); builder.maxWaitTime(properties.getMaxWaitTime()); builder.maxConnectionIdleTime(properties.getMaxConnectionIdleTime()); builder.maxConnectionLifeTime(properties.getMaxConnectionLifeTime()); builder.connectTimeout(properties.getConnectTimeout()); builder.socketTimeout(properties.getSocketTimeout()); // builder.socketKeepAlive(properties.getSocketKeepAlive()); builder.sslEnabled(properties.getSslEnabled()); builder.sslInvalidHostNameAllowed(properties.getSslInvalidHostNameAllowed()); builder.alwaysUseMBeans(properties.getAlwaysUseMBeans()); builder.heartbeatFrequency(properties.getHeartbeatFrequency()); builder.minHeartbeatFrequency(properties.getMinHeartbeatFrequency()); builder.heartbeatConnectTimeout(properties.getHeartbeatConnectTimeout()); builder.heartbeatSocketTimeout(properties.getHeartbeatSocketTimeout()); builder.localThreshold(properties.getLocalThreshold()); MongoClientOptions mongoClientOptions = builder.build(); // MongoDB地址列表 ListserverAddresses = 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); } //System.out.println("serverAddresses:" + serverAddresses.toString()); // 连接认证 MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(properties.getUsername(), properties.getAuthenticationDatabase() != null ? properties.getAuthenticationDatabase() : properties.getDatabase(), properties.getPassword().toCharArray()); //创建客户端和Factory MongoClient mongoClient = new MongoClient(serverAddresses, mongoCredential, mongoClientOptions); MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, properties.getDatabase()); return mongoDbFactory; } }
5. 使用MongoTemplate
配置完MongoDbFactory后就可以使用MongoTemplate了。
public class MongoService { @Autowired private MongoTemplate mongoTemplate; //.. }
或者继承MongoRepository
@Repository public interface DemoRepository extends MongoRepository{ }