使用 Mongodb 版本 4 和副本配置 flapdoodle 嵌入式 mongo [英] Configuring flapdoodle embedded mongo with Mongodb version 4 and replica

查看:71
本文介绍了使用 Mongodb 版本 4 和副本配置 flapdoodle 嵌入式 mongo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发 Spring Boot 应用程序 2.0.3.RELEASE.我想用 MongoDb 4.0 版配置 Flapdoodle MongoDb,我还想设置一个 mongo 实例并为其创建副本.

I am currently working on a spring boot application 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.

到目前为止我还没有弄清楚使用fladdoodle创建集群和副本的过程.

So far i haven't figured out the process of creating cluster and replicas using flapdoodle.

我正在使用

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

我在这里阅读了许多与此配置相关的问题,但没有一个与我的问题有关.例如如何配置两个实例mongodb使用spring启动和弹簧数据

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

fladdoodle 配置对此有一个实现,但我不确定如何访问它.

The flapdoodle configuration has an implemetation for this but i am not sure how to access it.

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

在应用程序启动之前,有没有办法在我的测试类中配置它.谢谢

Is there any way to configure it in my test class before application starts. thanks

推荐答案

我不得不在 web 集成测试中使用 replicaset 启动 Embedded mongo,在下面的代码中使用

I had to start the Embedded mongo with replicaset in web Integration tests, used below code

@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
      }
   }

如果要运行应用程序,则可以在ApplicationListener的实现中启用replicaset

If want to run application, then enabling of replicaset can be done in implementation of ApplicationListener

这篇关于使用 Mongodb 版本 4 和副本配置 flapdoodle 嵌入式 mongo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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