如何通过 Spring Boot 测试在嵌入式 MongoDB 中启用日志

Ste*_*ton 6 java spring mongodb spring-boot

我正在尝试编写一个使用嵌入式 MondoDB 4.0.2 的 Spring Boot 测试;要测试的代码需要 Mongo ChangeStreams,这需要 MongoDB 作为副本集启动。MongoDB 作为 MongoDB v4 的副本集需要启用日志功能。我无法找到一种方法来开始启用日记功能,因此将其发布在这里寻找答案。我随后找到了如何做到这一点 - 下面。

我有 spring-boot 2.1.3.RELEASE。Spring-data-mongodb 2.1.5.RELEASE

这就是我一直在尝试的:

@RunWith(SpringRunner.class)
@DataMongoTest(properties= {
    "spring.mongodb.embedded.version= 4.0.2",
    "spring.mongodb.embedded.storage.repl-set-name = r_0",
    "spring.mongodb.embedded.storage.journal.enabled=true"
    })
public class MyStreamWatcherTest {

@SpringBootApplication
@ComponentScan(basePackages = {"my.package.with.dao.classes"})
@EnableMongoRepositories( { "my.package.with.dao.repository" })
static public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@Before
public void startup() {
    MongoDatabase adminDb = mongoClient.getDatabase("admin");
    Document config = new Document("_id", "rs0");
    BasicDBList members = new BasicDBList();
    members.add(new Document("_id", 0).append("host", 
           mongoClient.getConnectPoint()));
    config.put("members", members);
    adminDb.runCommand(new Document("replSetInitiate", config));
Run Code Online (Sandbox Code Playgroud)

但是,当测试开始时,用于启动 mongo 的选项不包括启用日志。

修复方法是添加此类:

@Configuration
public class MyEmbeddedMongoConfiguration {

  private int localPort = 0;

  public int getLocalPort() {
    return localPort;
  }

  @Bean
  public IMongodConfig mongodConfig(EmbeddedMongoProperties embeddedProperties) throws IOException {
    MongodConfigBuilder builder = new MongodConfigBuilder()
             .version(Version.V4_0_2)
             .cmdOptions(new MongoCmdOptionsBuilder().useNoJournal(false).build());
    // Save the local port so the replica set initializer can come get it.
    this.localPort = Network.getFreeServerPort();
    builder.net(new Net("127.0.0.1", this.getLocalPort(), Network.localhostIsIPv6()));
    EmbeddedMongoProperties.Storage storage = embeddedProperties.getStorage();
    if (storage != null) {
        String databaseDir = storage.getDatabaseDir();
        String replSetName = "rs0";   // Should be able to:  storage.getReplSetName();
        int oplogSize = (storage.getOplogSize() != null)
                ? (int) storage.getOplogSize().toMegabytes() : 0;
        builder.replication(new Storage(databaseDir, replSetName, oplogSize));
    }
    return builder.build();
}
Run Code Online (Sandbox Code Playgroud)

这启用了日志并且 mongod 启动并启用了副本集。然后我添加了另一个类来初始化副本集:

@Configuration
public class EmbeddedMongoReplicaSetInitializer {

@Autowired
MyEmbeddedMongoConfiguration myEmbeddedMongoConfiguration;

MongoClient mongoClient;

// We don't use this MongoClient as it will try to wait for the replica set to stabilize
// before address-fetching methods will return. It is specified here to order this class's
// creation after MongoClient, so we can be sure mongod is running.
EmbeddedMongoReplicaSetInitializer(MongoClient mongoClient) {
    this.mongoClient = mongoClient;
}

@PostConstruct
public void initReplicaSet() {

    //List<ServerAddress> serverAddresses = mongoClient.getServerAddressList();
    MongoClient mongo = new MongoClient(new ServerAddress("127.0.0.1", myEmbeddedMongoConfiguration.getLocalPort()));

    MongoDatabase adminDb = mongo.getDatabase("admin");
    Document config = new Document("_id", "rs0");
    BasicDBList members = new BasicDBList();
    members.add(new Document("_id", 0).append("host", String.format("127.0.0.1:%d", myEmbeddedMongoConfiguration.getLocalPort())));
    config.put("members", members);
    adminDb.runCommand(new Document("replSetInitiate", config));
    mongo.close();
}

}
Run Code Online (Sandbox Code Playgroud)

这样就完成了工作。如果有人有让这变得更容易的提示,请在此处发布。