我有一个基于休息的spring-boot微服务,我想配置它来将认证和会话数据存储在一个mongo数据库中,将应用程序数据存储在另一个数据库中。
我当前的设置主要有效,除了spring正在两个数据库中创建我的所有文档。我查看了freq_dist.get('a')
1
# get() method returns None if character is not in dict
freq_dist.get('4')
None
注释,但它没有供数据库使用的参数。
在mongo shell中:
@Document()
application.properties:
> 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
HttpSessionConfig.java
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
MongoConfig.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();
}
}
Prospect.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;
}
}
我希望前景集合只存在于潜在客户数据库中。
我已经尝试删除数据库并重新启动服务,但是他们重新启动时会重新启动。关于如何正确地做到这一点的任何想法?
答案 0 :(得分:0)
如果您的应用程序指向单个mongodb实例,并且您希望根据每个Web请求切换数据库。你可以看看这个示例, https://github.com/Loki-Afro/multi-tenant-spring-mongodb/blob/master/src/main/java/com/github/zarathustra/mongo/MultiTenantMongoDbFactory.java
PS: - 我发现了这个,当我为实现多租户做研究时