MongoDB 多数据源连接池配置
1.Config配置类
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
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.MongoDatabaseFactory;
import org.springframework.data.mongodb.SpringDataMongoDB;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import java.util.concurrent.TimeUnit;
/**
* @author: curyu
* @date: 2022/9/5 13:51
* @description: mongodb连接池配置
*/
@Configuration
public class MongoConfiguration {
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.data.mongodb.primary")
public MongoProperties masterMongoProperties() {
return new MongoProperties();
}
@Bean
@ConfigurationProperties(prefix = "spring.data.mongodb.second")
public MongoProperties secondMongoProperties() {
return new MongoProperties();
}
/**
* 连接池自定义配置处理
*
* @return
*/
@Bean
@Primary
public MongoDatabaseFactory mongoDatabaseFactory(@Qualifier("masterMongoProperties") MongoProperties properties) {
MongoClientSettings.Builder builder = MongoClientSettings.builder();
builder.applyConnectionString(new ConnectionString(properties.getUri()));
builder.applyToConnectionPoolSettings(b -> {
// 允许的最大连接数。这些连接在空闲时将保留在池中。一旦池耗尽,任何需要连接的操作都将阻塞等待可用连接 默认: 100
b.maxSize(100);
// 最小连接数。这些连接在空闲时将保留在池中,并且池将确保它至少包含这个最小数量 默认: 0
b.minSize(5);
// 池连接可以存活的最长时间。零值表示寿命没有限制。超过其生命周期的池连接将被关闭并在必要时由新连接替换
b.maxConnectionLifeTime(0, TimeUnit.SECONDS);
// 池连接的最大空闲时间。零值表示对空闲时间没有限制。超过其空闲时间的池连接将被关闭并在必要时由新连接替换
b.maxConnectionIdleTime(5, TimeUnit.MINUTES);
// 默认最大连接时间120s;
b.maxWaitTime(60000, TimeUnit.MILLISECONDS);
});
MongoClient mongoClient = MongoClients.create(builder.build(), SpringDataMongoDB.driverInformation());
return new SimpleMongoClientDatabaseFactory(mongoClient, properties.getDatabase());
}
/**
* 连接池自定义配置处理
*
* @return
*/
@Bean
public MongoDatabaseFactory secondMongoDatabaseFactory(@Qualifier("secondMongoProperties") MongoProperties properties) {
MongoClientSettings.Builder builder = MongoClientSettings.builder();
builder.applyConnectionString(new ConnectionString(properties.getUri()));
builder.applyToConnectionPoolSettings(b -> {
// 允许的最大连接数。这些连接在空闲时将保留在池中。一旦池耗尽,任何需要连接的操作都将阻塞等待可用连接 默认: 100
b.maxSize(100);
// 最小连接数。这些连接在空闲时将保留在池中,并且池将确保它至少包含这个最小数量 默认: 0
b.minSize(5);
// 池连接可以存活的最长时间。零值表示寿命没有限制。超过其生命周期的池连接将被关闭并在必要时由新连接替换
b.maxConnectionLifeTime(0, TimeUnit.SECONDS);
// 池连接的最大空闲时间。零值表示对空闲时间没有限制。超过其空闲时间的池连接将被关闭并在必要时由新连接替换
b.maxConnectionIdleTime(5, TimeUnit.MINUTES);
// 默认最大连接时间120s;
b.maxWaitTime(60000, TimeUnit.MILLISECONDS);
});
MongoClient mongoClient = MongoClients.create(builder.build(), SpringDataMongoDB.driverInformation());
return new SimpleMongoClientDatabaseFactory(mongoClient, properties.getDatabase());
}
@Bean
@Primary
public MongoTemplate mongoTemplate(@Qualifier("mongoDatabaseFactory") MongoDatabaseFactory mongoDatabaseFactory) {
MongoTemplate mongoTemplate = new MongoTemplate(mongoDatabaseFactory);
//去除保存实体时,spring data mongodb 自动添加的_class字段
MappingMongoConverter mongoMapping = (MappingMongoConverter) mongoTemplate.getConverter();
mongoMapping.setTypeMapper(new DefaultMongoTypeMapper(null));
mongoMapping.afterPropertiesSet();
return mongoTemplate;
}
@Bean
public MongoTemplate secondMongoTemplate(@Qualifier("secondMongoDatabaseFactory") MongoDatabaseFactory mongoDatabaseFactory) {
MongoTemplate mongoTemplate = new MongoTemplate(mongoDatabaseFactory);
//去除保存实体时,spring data mongodb 自动添加的_class字段
MappingMongoConverter mongoMapping = (MappingMongoConverter) mongoTemplate.getConverter();
mongoMapping.setTypeMapper(new DefaultMongoTypeMapper(null));
mongoMapping.afterPropertiesSet();
return mongoTemplate;
}
}
2. 配置文件
spring:
data:
mongodb:
# mongodb 默认数据源
primary:
uri: mongodb://root:root@10.15.1.155:27017/${spring.data.mongodb.primary.database}?authSource=admin&authMechanism=SCRAM-SHA-1
database: test-db
# mongodb 第二数据源
second:
uri: mongodb://admin:admin123@10.15.1.156:27017/${spring.data.mongodb.second.database}?authSource=admin&authMechanism=SCRAM-SHA-1
database: test2-db
3. 数据源测试
import com.mongodb.client.FindIterable;
import lombok.Data;
import org.bson.Document;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author: curyu
* @date: 2021/9/4 16:56
* @description:
*/
@Data
@RunWith(SpringRunner.class)
@SpringBootTest
class MongoUtilTest {
@Autowired
private MongoTemplate mongoTemplate;
@Qualifier("secondMongoTemplate")
@Autowired
private MongoTemplate secondMongoTemplate;
@Test
void getById() {
System.out.println(" mongo DataSource 1 ...");
// 数据源1
FindIterable<Document> cc = mongoTemplate.getCollection("order").find().limit(10);
for (Document document : cc) {
System.out.println(document);
}
System.out.println(" mongo DataSource 2 ...");
// 数据源2
FindIterable<Document> cc2 = secondMongoTemplate.getCollection("order2").find().limit(10);
for (Document document : cc2) {
System.out.println(document);
}
}
}