Spring Mongodb: 配置文件使用特定的数据库?

Spring Mongodb: configure documents to use specific dbs?

我有一个基于休息的 spring-boot 微服务,我想将其配置为将身份验证和会话数据存储在一个 mongo 数据库中,并将应用程序数据存储在另一个数据库中。

除了 spring 在两个数据库中创建我的所有文档外,我当前的设置大部分都有效。我查看了@Document()注解,但它没有供数据库使用的参数。

在mongo shell:

> show dbs
admin                 0.000GB
authentication        0.000GB
prospect              0.000GB

> use authentication
switched to db authentication

> show collections
credentials
credentials_authorities
prospects
roles
sessions

> use prospect
switched to db prospect

> show collections
prospects

application.properties:

com.mycompany.prospect.domainName=mycompany.com

spring.data.mongodb.host=mongodb-service
spring.data.mongodb.port=27017

com.mycompany.prospect.server.appDatabase=prospect
com.mycompany.prospect.server.sessionDatabase=authentication

HttpSessionConfig.java

@Configuration
@EnableMongoHttpSession()
public class HttpSessionConfig {

  private final Logger logger = LoggerFactory.getLogger(this.getClass());

  @Bean
  public CookieSerializer cookieSerializer(
      @Value("${com.mycompany.prospect.domainName:localhost}")
          final String domainName) {

    logger.info("Configuring DefaultCookieSerializer with DomainName '{}'.", domainName);

    final DefaultCookieSerializer serializer = new DefaultCookieSerializer();
    serializer.setDomainName(domainName);
    serializer.setUseHttpOnlyCookie(true);
    return serializer;
  }

  @Bean
  public JdkMongoSessionConverter jdkMongoSessionConverter() {
    return new JdkMongoSessionConverter();
  }
}

MongoConfig.java

@Configuration
@EnableMongoRepositories
public class MongoConfig {

  private final Environment env;

  public MongoConfig(Environment env) {
    this.env = env;
  }

  @Bean("applicationTemplate")
  @Qualifier("applicationTemplate")
  public MongoTemplate mongoTemplate(
      final MappingMongoConverter mappingMongoConverter, final MongoClient mongoClient) {
    final String databaseName =
        env.getRequiredProperty("com.mycompany.prospect.server.appDatabase");

    final MongoDbFactory dbFactory = new SimpleMongoDbFactory(mongoClient, databaseName);
    return new MongoTemplate(dbFactory, mappingMongoConverter);
  }

  @Bean("mongoOperations")
  @Qualifier("mongoOperations")
  @Primary
  public MongoTemplate mongoOperations(
      final MappingMongoConverter mappingMongoConverter, final MongoClient mongoClient) {
    final String databaseName =
        env.getRequiredProperty("com.mycompany.prospect.server.sessionDatabase");
    final MongoDbFactory dbFactory = new SimpleMongoDbFactory(mongoClient, databaseName);
    final MongoTemplate mongoTemplate = new MongoTemplate(dbFactory, mappingMongoConverter);
    return mongoTemplate;
  }    
}

Prospect.java

@Document(collection = "prospects")
public class Prospect {
  @Id private String prospectId;

  @TextIndexed() private String businessName;

  public String getProspectId() {
    return prospectId;
  }

  public void setProspectId(String prospectId) {
    this.prospectId = prospectId;
  }

  public String getBusinessName() {
    return businessName;
  }

  public void setBusinessName(final String businessName) {
    this.businessName = businessName;
  }
}    

我希望前景集合仅存在于前景数据库中。

我试过删除数据库并重新启动服务,但它们会在重新启动时恢复。关于如何正确执行此操作的任何想法?

如果您的应用程序指向单个 mongodb 实例并且您想根据每个网络请求切换数据库。你可以看看这个示例, https://github.com/Loki-Afro/multi-tenant-spring-mongodb/blob/master/src/main/java/com/github/zarathustra/mongo/MultiTenantMongoDbFactory.java

PS:- 我在研究实现多租户时发现了这一点