如何配置两个实例mongodb使用spring引导和spring数据 [英] How to configure two instance mongodb use spring boot and spring data

查看:73
本文介绍了如何配置两个实例mongodb使用spring引导和spring数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个实例是嵌入式MongoDb,第二个实例是实时MongoDb. 如何配置使用spring数据和spring boot.如何通过属性文件轻松切换这些实例?

The First instance is the embedded MongoDb, the second instance is the live MongoDb. How do it configure use spring data and spring boot. How to switch easily these instances by properties file??

更新

  • 默认情况下,应用程序应启动内置数据库并存储数据 进入APPDIR/db目录

  • By default application should start build-in database and store data into APPDIR/db directory

应该有可能让应用程序知道外部数据库 将通过配置mongo.url属性使用.在这种情况下,不需要 启动内部数据库.而不是外部连接 应该使用

It should be possible to let application know that external database will be used by configuring mongo.url property. In this case no need to start internal database. Instead of that external connection should be used

请粘贴一些配置.

更新 我有:

<!--Embedded MongoDB-->
<dependency>
    <groupId>de.flapdoodle.embed</groupId>
    <artifactId>de.flapdoodle.embed.mongo</artifactId>
    <version>1.50.5</version>
</dependency>

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.uri=mongodb://localhost/test
spring.data.mongodb.database=test

# EMBEDDED MONGODB (EmbeddedMongoProperties)
#spring.mongodb.embedded.storage.databaseDir=c:/db
#spring.mongodb.embedded.version=3.2.2

如果我要指定外部mongodb,那么我希望嵌入式mongodb不应该启动.

if I'll specify external mongodb, then i want embedded mongodb shouldn't to startup.

java.io.IOException: Could not start process: <EOF>
    at de.flapdoodle.embed.mongo.AbstractMongoProcess.onAfterProcessStart(AbstractMongoProcess.java:79) ~[de.flapdoodle.embed.mongo-1.50.5.jar!/:?]
    at de.flapdoodle.embed.process.runtime.AbstractProcess.<init>(AbstractProcess.java:114) [de.flapdoodle.embed.process-1.50.2.jar!/:?]
    at de.flapdoodle.embed.mongo.AbstractMongoProcess.<init>(AbstractMongoProcess.java:53) [de.flapdoodle.embed.mongo-1.50.5.jar!/:?]
    at de.flapdoodle.embed.mongo.MongodProcess.<init>(MongodProcess.java:50) [de.flapdoodle.embed.mongo-1.50.5.jar!/:?]
    at de.flapdoodle.embed.mongo.MongodExecutable.start(MongodExecutable.java:44) [de.flapdoodle.embed.mongo-1.50.5.jar!/:?]
    at de.flapdoodle.embed.mongo.MongodExecutable.start(MongodExecutable.java:34) [de.flapdoodle.embed.mongo-1.50.5.jar!/:?]
    at de.flapdoodle.embed.process.runtime.Executable.start(Executable.java:101) [de.flapdoodle.embed.process-1.50.2.jar!/:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_05]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_05]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_05]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[?:1.8.0_05]

推荐答案

我认为您可以使用Spring配置文件.

I think you can use Spring profiles.

这是文档.

Spring Profiles提供了一种隔离应用程序各部分的方法 配置,并使其仅在某些环境中可用.

Spring Profiles provide a way to segregate parts of your application configuration and make it only available in certain environments.

更新

注意:我上面提到的文档中都指出了我下面将要讨论的所有内容.您应该真正看一下本文档.这个文档很棒(不要开玩笑).

UPDATE

Note : Everything that I will talk about below is indicated in the documentation I mentioned above... You should really take a look to this documentation. This documentation is great (no joke).

来自附录A.常见应用程序属性(Spring Boot文档)

From Appendix A. Common application properties (Spring boot documentation)

以下是在application.properties中配置远程MongoDB实例的方法:

Here's how to configuration remote MongoDB instance in application.properties :

# MONGODB (MongoProperties)
spring.data.mongodb.authentication-database= # Authentication database name.
spring.data.mongodb.database=test # Database name.
spring.data.mongodb.field-naming-strategy= # Fully qualified name of the FieldNamingStrategy to use.
spring.data.mongodb.grid-fs-database= # GridFS database name.
spring.data.mongodb.host=localhost # Mongo server host.
spring.data.mongodb.password= # Login password of the mongo server.
spring.data.mongodb.port=27017 # Mongo server port.
spring.data.mongodb.repositories.enabled=true # Enable Mongo repositories.
spring.data.mongodb.uri=mongodb://localhost/test # Mongo database URI. When set, host and port are ignored.
spring.data.mongodb.username= # Login user of the mongo server.

这是在application.properties中配置嵌入式MongoDB实例的方法:

And here's how to configure embedded MongoDB instance in application.properties:

# EMBEDDED MONGODB (EmbeddedMongoProperties)
spring.mongodb.embedded.features=SYNC_DELAY # Comma-separated list of features to enable.
spring.mongodb.embedded.storage.databaseDir= # Directory used for data storage.
spring.mongodb.embedded.storage.oplogSize= # Maximum size of the oplog in megabytes.
spring.mongodb.embedded.storage.replSetName= # Name of the replica set.
spring.mongodb.embedded.version=2.6.10 # Version of Mongo to use.

来自

From Change configuration depending on the environment (Spring boot documentation)

要对属性文件执行相同的操作,可以使用 application-${profile}.properties以指定特定于配置文件的值.

To do the same thing with properties files you can use application-${profile}.properties to specify profile-specific values.

您可以在application-dev.properties中定义MongoDB嵌入式配置,并在application-prod.properties

You can define the MongoDB embedded configuration into application-dev.properties and the MongoDB remote configuration into application-prod.properties

我假设您在类(从

您可以为此类分配spring配置文件,例如(来自文档):

You can assign a spring profile to this class like (from documentation) :

@Configuration
@Profile("dev")
public class ProductionConfiguration {

    // ...

}

这样,你的嵌入式MongoDB是,只有当你选择个人资料.

This way, your embedded MongoDB is started only when you choose dev profile.

这篇关于如何配置两个实例mongodb使用spring引导和spring数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆