在应用程序关闭时保留数据

在应用程序关闭时保留数据

本文介绍了如何使嵌入式 mongodb 在应用程序关闭时保留数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想安装完整的 mongodb,所以我使用以下 pom 创建了一个简单的 spring-boot 应用程序:

I don't want to install the full mongodb, so I created a simple spring-boot application with the following pom:

    <!-- This dependency is to have an embedded mongodb -->
    <dependency>
        <groupId>de.flapdoodle.embed</groupId>
        <artifactId>de.flapdoodle.embed.mongo</artifactId>
        <version>1.50.5</version>
    </dependency>

    <!-- while this provides a spring factory bean for the embedded mongodb -->
    <dependency>
        <groupId>cz.jirutka.spring</groupId>
        <artifactId>embedmongo-spring</artifactId>
        <version>RELEASE</version>
    </dependency>

    <!-- finally this one is the spring-boot starter for mongodb -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
        <version>${spring.boot.version}</version>
     </dependency>

它工作正常,但在应用程序关闭时我丢失了所有存储的数据.

It works fine, but on the application shutdown I lose all the data stored.

我注意到数据库是在一个临时文件夹中创建的C:\Users\myUser\AppData\Local\Temp\embedmongo-db-78beadc3-fa16-4101-afb1-ea7496f6f90c 并且,每次重新启动应用程序时,都会在相同的位置.

I noticed that the db is created inside of a temporary folder underC:\Users\myUser\AppData\Local\Temp\embedmongo-db-78beadc3-fa16-4101-afb1-ea7496f6f90c and, every time the application is restarted, another folder with different ID is created at the same location.

那么,是否可以指定应该在何处创建数据库,以便保留现有数据库而不丢失所有数据?就像你会用 h2 或 sqllite 做的那样?

So, would it be possible to specify where the DB should be created so to keep the existing one and not lose all the data? Like you would do with h2 or sqllite?

推荐答案

现在有可能.

接下来的代码只是展示了我实现它的解决方案.

Next code just shows my solution to implement it.

public class MongoInMemory {
 private int port;
 private String host;
 private MongodProcess process = null;

 public MongoInMemory(int port, String host){
    this.port = port;
    this.host = host;
 }

 @PostConstruct
 public void init() throws IOException {
    Storage storage = new Storage(
            System.getProperty("user.home") + "/.ttraining-storage", null, 0);

    IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
            .defaults(Command.MongoD)
            .artifactStore(new ExtractedArtifactStoreBuilder()
                    .defaults(Command.MongoD)
                    .download(new DownloadConfigBuilder()
                            .defaultsForCommand(Command.MongoD).build())
                    .executableNaming(new UserTempNaming()))
            .build();

    IMongodConfig mongodConfig = new MongodConfigBuilder()
            .version(Version.Main.PRODUCTION)
            .net(new Net(host, port, false))
            .replication(storage)
            .build();

    MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
    process = runtime.prepare(mongodConfig).start();
 }

 @PreDestroy
 public void stop(){
    process.stop();
 }
}

在配置类中将其定义为bean

@Bean
public MongoInMemory mongoInMemory(
    @Value("${spring.data.mongodb.port}") int port,
    @Value("${spring.data.mongodb.host}")  String host) {
        return new MongoInMemory(port, host)
}

最后,删除入口点中嵌入的 mongodb 自动配置

@SpringBootApplication(exclude = EmbeddedMongoAutoConfiguration.class)

PS:从依赖中移除cz.jirutka.spring:embedmongo-spring

PSS:在 Spring-boot 2.0.0.RELEASE 上测试

PSS: Tested on Spring-boot 2.0.0.RELEASE

PSSS:你也可以在 application.properties 中定义 path 作为一个属性,也可以在构造函数中定义它

PSSS: Also you can define path as a property in application.properties and also it in constructor

这篇关于如何使嵌入式 mongodb 在应用程序关闭时保留数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 01:46