Spring Data MongoDB + Spring Boot两次启动?

时间:2019-05-19 10:09:22

标签: spring spring-boot spring-data spring-data-mongodb

我正在尝试使Spring Data MongoDB(可响应)与Spring Boot 2.1.5(和WebFlux)一起使用。

从启动日志中,我怀疑出现了问题,好像它被初始化了两次(注意两次存储库扫描,它们甚至返回不同的结果):

2019.05.19 11:59:53 | INFO | org.springframework.data.repository.config.RepositoryConfigurationDelegate - Bootstrapping Spring Data repositories in DEFAULT mode.
2019.05.19 11:59:55 | INFO | org.springframework.data.repository.config.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 1235ms. Found 8 repository interfaces.
2019.05.19 11:59:55 | INFO | org.springframework.data.repository.config.RepositoryConfigurationDelegate - Bootstrapping Spring Data repositories in DEFAULT mode.
2019.05.19 11:59:55 | INFO | org.springframework.data.repository.config.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 64ms. Found 0 repository interfaces.
2019.05.19 11:59:59 | INFO | org.mongodb.driver.cluster - Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2019.05.19 11:59:59 | INFO | org.mongodb.driver.cluster - Cluster created with settings {hosts=[localhost:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2019.05.19 11:59:59 | INFO | org.mongodb.driver.cluster - Adding discovered server localhost:27017 to client view of cluster
2019.05.19 12:00:00 | INFO | org.mongodb.driver.connection - Opened connection [connectionId{localValue:2, serverValue:13}] to localhost:27017
2019.05.19 12:00:00 | INFO | org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 9]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=16058831}
2019.05.19 12:00:00 | INFO | org.mongodb.driver.cluster - Discovered cluster type of STANDALONE
2019.05.19 12:00:00 | INFO | org.mongodb.driver.connection - Opened connection [connectionId{localValue:1, serverValue:14}] to localhost:27017
2019.05.19 12:00:00 | INFO | org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 9]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=43838433}

我的构建文件中没有什么特别的:

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("org.springframework.boot:spring-boot-starter-data-mongodb-reactive")
    implementation("org.springframework.boot:spring-boot-starter-validation")
    implementation("org.springframework.boot:spring-boot-starter-security")

    ...

    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("de.flapdoodle.embed:de.flapdoodle.embed.mongo")
    testImplementation("io.projectreactor:reactor-test")
    testImplementation("org.springframework.security:spring-security-test")
}

关于Mongo,绝对没有Java配置,只有application.properties设置:

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.database=mydb
spring.data.mongodb.username=myuser
spring.data.mongodb.password=mypass

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

我遇到了相同的症状。看来我有一个实现AbstractReactiveMongoConfiguration的Configuration类,这导致两次创建MongoClient。将客户端创建工作留给框架并通过属性进行配置可以解决我的问题。

这已通过Spring Boot 2.1.6-RELEASE进行了测试,并具有与发布者相同的依赖性。