简体   繁体   English

使用 Mongodb 版本 4 和副本配置 flapdoodle 嵌入式 mongo

[英]Configuring flapdoodle embedded mongo with Mongodb version 4 and replica

I am currently working on a spring boot application 2.0.3.RELEASE.我目前正在开发 Spring Boot 应用程序 2.0.3.RELEASE。 I want to configure Flapdoodle MongoDb with MongoDb version 4.0 and I also want to set a single mongo instance and create replicas for it.我想用 MongoDb 4.0 版配置 Flapdoodle MongoDb,我还想设置一个 mongo 实例并为其创建副本。

So far i haven't figured out the process of creating cluster and replicas using flapdoodle.到目前为止,我还没有弄清楚使用fladdoodle创建集群和副本的过程。

I am using我在用

         MongodConfigBuilder().version(Version.Main.DEVELOPMENT)
        .replication(new Storage(null, null, 0))
        .build();

i have read many questions here related to this configuration but none of them is related to my problem.我在这里阅读了许多与此配置相关的问题,但没有一个与我的问题有关。 eg How to configure two instance mongodb use spring boot and spring data eg 如何配置两个实例mongodb 使用spring boot 和spring data

The flapdoodle configuration has an implemetation for this but i am not sure how to access it. fladdoodle 配置对此有一个实现,但我不确定如何访问它。

https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/blob/master/src/main/java/de/flapdoodle/embed/mongo/tests/MongosSystemForTestFactory.java https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/blob/master/src/main/java/de/flapdoodle/embed/mongo/tests/MongosSystemForTestFactory.java

Is there any way to configure it in my test class before application starts.在应用程序启动之前,有什么方法可以在我的测试类中配置它。 thanks谢谢

I had to start the Embedded mongo with replicaset in web Integration tests, used below code我必须在 Web 集成测试中使用副本集启动嵌入式 mongo,在下面的代码中使用

@Configuration
public class MongoConfig {

    public static int mongodPort;
    public static String defaultHost = "localhost";
    static {
        try {
            mongodPort = Network.getFreeServerPort();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Bean
    public IMongodConfig prepareMongodConfig() throws IOException {
        IMongoCmdOptions cmdOptions = new MongoCmdOptionsBuilder()
                .useNoPrealloc(false)
                .useSmallFiles(false)
                .master(false)
                .verbose(false)
                .useNoJournal(false)
                .syncDelay(0)
                .build();

        IMongodConfig mongoConfigConfig = new MongodConfigBuilder()
                .version(Version.Main.PRODUCTION)
                .net(new Net(mongodPort, Network.localhostIsIPv6()))
                .replication(new Storage(null, "testRepSet", 5000))
                .configServer(false)
                .cmdOptions(cmdOptions)
                .build();
        return mongoConfigConfig;
    }

}

and before calling my controller I enabled the DB with replica set using below code在调用我的控制器之前,我使用以下代码启用了带有副本集的数据库

 Public class ITtest {
    public  void  setSystemProperty() {
            System.setProperty("spring.data.mongodb.port", String.valueOf(MongoConfig.mongodPort));
            System.setProperty("spring.data.mongodb.host", MongoConfig.defaultHost);
        }

        public static boolean isReplicaSetRun = false;

        public static void setupMongoReplica() {
            if (! isReplicaSetRun) {
                System.out.println("Starting db on port: " +MongoConfig.mongodPort);
                MongoClient client = new MongoClient(MongoConfig.defaultHost, MongoConfig.mongodPort);
                client.getDatabase("admin").runCommand(new Document("replSetInitiate", new Document()));
                client.close();
                isReplicaSetRun = true;
            }
        }

        @Test
        @Order(1)
        public void testParallel() {
            setSystemProperty();
            setupMongoReplica();
            // call web controller
      }
   }

If want to run application, then enabling of replicaset can be done in implementation of ApplicationListener如果要运行应用程序,则可以在 ApplicationListener 的实现中启用副本集

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM