Configuring flapdoodle embedded mongo with Mongodb version 4 and replica
Asked Answered
B

1

7

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.

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

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

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.java

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

Belden answered 24/7, 2018 at 13:10 Comment(1)
baeldung.com/spring-boot-embedded-mongodbInvidious
D
1

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

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

Danais answered 24/4, 2020 at 8:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.