问题描述
第一个实例是嵌入式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 datainto APPDIR/db directory
应该有可能让应用程序知道外部数据库将通过配置mongo.url属性使用.在这种情况下,不需要启动内部数据库.而不是外部连接应该使用
It should be possible to let application know that external databasewill be used by configuring mongo.url property. In this case no needto start internal database. Instead of that external connectionshould 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.
这是文档.
更新
注意:我上面提到的文档中都指出了我下面将要讨论的所有内容.您应该真正看一下本文档.这个文档很棒(不要开玩笑).
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.
来自根据环境更改配置(Spring启动文档)
From Change configuration depending on the environment (Spring boot documentation)
您可以在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
我假设您在类(从文档):
import de.flapdoodle.embed.mongo.config.ArtifactStoreBuilder;
...
MongodStarter starter = MongodStarter.getDefaultInstance();
String bindIp = "localhost";
int port = 12345;
IMongodConfig mongodConfig = new MongodConfigBuilder()
.version(Version.Main.PRODUCTION)
.net(new Net(bindIp, port, Network.localhostIsIPv6()))
.build();
MongodExecutable mongodExecutable = null;
您可以为此类分配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数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!