Spring Boot application integrates multiple MongoDB
quickly.
- Import dependencies
<dependency>
<groupId>com.yookue.springstarter</groupId>
<artifactId>multiple-mongo-spring-boot-starter</artifactId>
<version>LATEST</version>
</dependency>
By default, this starter will auto take effect, you can turn it off by
spring.multiple-mongo.enabled = false
- Configure Spring Boot
application.yml
with prefixspring.multiple-mongo
spring:
multiple-mongo:
primary:
host: '192.168.0.1'
database: 'foo1'
secondary:
host: '192.168.0.2'
database: 'foo2'
tertiary:
host: '192.168.0.3'
database: 'foo3'
This starter supports 3
MongoClientFactory
at most. (Three strikes and you're out)
-
Optional feature: If you want to use mongodb repositories, locate your repositories under the following packages (take
primary
as an example)- Repositories:
**.repository.primary.mongo
- Repositories:
-
Configure your beans with the following beans by
@Autowired
/@Resource
annotation, combined with@Qualifier
annotation (takeprimary
as an example)
Bean Type | Qualifier |
---|---|
MongoClientFactory | PrimaryMongoAutoConfiguration.CLIENT_FACTORY |
MongoClient | PrimaryMongoAutoConfiguration.MONGO_CLIENT |
MongoTransactionManager | PrimaryMongoAutoConfiguration.TRANSACTION_MANAGER |
MongoTemplate | PrimaryMongoAutoConfiguration.MONGO_TEMPLATE |
GridFsTemplate | PrimaryMongoAutoConfiguration.GRID_FS_TEMPLATE |
ReactiveMongoTemplate | PrimaryMongoReactiveConfiguration.MONGO_TEMPLATE |
ReactiveGridFsTemplate | PrimaryMongoReactiveConfiguration.GRID_FS_TEMPLATE |
- jdk 17+
This project is under the Apache License 2.0
See the NOTICE.txt
file for required notices and attributions.
You like this package? Then donate to Yookue to support the development.
- Yookue: https://yookue.com