Spring Boot Reactive Mongo 在启动时挂起
Posted
技术标签:
【中文标题】Spring Boot Reactive Mongo 在启动时挂起【英文标题】:Spring Boot Reactive Mongo Hangs on startup 【发布时间】:2018-10-30 11:01:55 【问题描述】:我有一个 Spring Boot application 使用响应式 Mongo DB
驱动程序,该驱动程序在启动时挂起。
日志:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.1.RELEASE)
2018-05-20 13:45:47.923 DEBUG 26675 --- [ main] s.b.w.r.c.StandardReactiveWebEnvironment : Adding PropertySource 'systemProperties' with lowest search precedence
2018-05-20 13:45:47.924 DEBUG 26675 --- [ main] s.b.w.r.c.StandardReactiveWebEnvironment : Adding PropertySource 'systemEnvironment' with lowest search precedence
2018-05-20 13:45:47.924 DEBUG 26675 --- [ main] s.b.w.r.c.StandardReactiveWebEnvironment : Initialized StandardReactiveWebEnvironment with PropertySources [MapPropertySource name='systemProperties', SystemEnvironmentPropertySource name='systemEnvironment']
2018-05-20 13:45:48.018 INFO 26675 --- [ main] c.a.m.mobsters.MobstersApplication : Starting MobstersApplication on Adrians-iMac.local with PID 26675 (/Users/adrian/IdeaProjects/MobstersREST/backend/target/classes started by adrian in /Users/adrian/IdeaProjects/MobstersREST)
2018-05-20 13:45:48.018 INFO 26675 --- [ main] c.a.m.mobsters.MobstersApplication : The following profiles are active: prod
2018-05-20 13:45:48.018 DEBUG 26675 --- [ main] o.s.boot.SpringApplication : Loading source class com.adrian.mobstersrest.mobsters.MobstersApplication
2018-05-20 13:45:48.092 DEBUG 26675 --- [ main] o.s.b.c.c.ConfigFileApplicationListener : Activated profiles prod
2018-05-20 13:45:48.093 DEBUG 26675 --- [ main] o.s.b.c.c.ConfigFileApplicationListener : Loaded config file 'file:/Users/adrian/IdeaProjects/MobstersREST/backend/target/classes/application.yml' (classpath:/application.yml) for profile prod
2018-05-20 13:45:48.093 DEBUG 26675 --- [ main] o.s.b.c.c.ConfigFileApplicationListener : Loaded config file 'file:/Users/adrian/IdeaProjects/MobstersREST/backend/target/classes/application.yml' (classpath:/application.yml)
2018-05-20 13:45:48.097 INFO 26675 --- [ main] onfigReactiveWebServerApplicationContext : Refreshing org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@2f9f7dcf: startup date [Sun May 20 13:45:48 EDT 2018]; root of context hierarchy
2018-05-20 13:45:48.098 DEBUG 26675 --- [ main] onfigReactiveWebServerApplicationContext : Bean factory for org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@2f9f7dcf: org.springframework.beans.factory.support.DefaultListableBeanFactory@2525ff7e: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,mobstersApplication]; root of factory hierarchy
2018-05-20 13:45:49.075 DEBUG 26675 --- [ main] o.s.b.a.AutoConfigurationPackages : @EnableAutoConfiguration was declared on a class in the package 'com.adrian.mobstersrest.mobsters'. Automatic @Repository and @Entity scanning is enabled.
2018-05-20 13:45:49.662 DEBUG 26675 --- [ main] onfigReactiveWebServerApplicationContext : Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@619bd14c]
2018-05-20 13:45:49.662 DEBUG 26675 --- [ main] onfigReactiveWebServerApplicationContext : Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@323e8306]
2018-05-20 13:45:49.969 INFO 26675 --- [ main] s.w.r.r.m.a.RequestMappingHandlerMapping : Mapped "[/],methods=[GET]" onto public java.lang.String com.adrian.mobstersrest.mobsters.api.v1.controller.IndexController.index()
2018-05-20 13:45:49.982 INFO 26675 --- [ main] s.w.r.r.m.a.RequestMappingHandlerMapping : Mapped "[/api/v1/mobsters/username/queue],methods=[POST]" onto public org.reactivestreams.Publisher<java.lang.Void> com.adrian.mobstersrest.mobsters.api.v1.controller.MobsterController.queue(java.lang.String)
2018-05-20 13:45:49.983 INFO 26675 --- [ main] s.w.r.r.m.a.RequestMappingHandlerMapping : Mapped "[/api/v1/mobsters],methods=[GET]" onto public reactor.core.publisher.Flux<com.adrian.mobstersrest.mobsters.domain.Mobster> com.adrian.mobstersrest.mobsters.api.v1.controller.MobsterController.getMobsters()
2018-05-20 13:45:49.983 INFO 26675 --- [ main] s.w.r.r.m.a.RequestMappingHandlerMapping : Mapped "[/api/v1/mobsters],methods=[POST]" onto public org.reactivestreams.Publisher<java.lang.Void> com.adrian.mobstersrest.mobsters.api.v1.controller.MobsterController.addMobster(org.reactivestreams.Publisher<com.adrian.mobstersrest.mobsters.domain.Mobster>)
2018-05-20 13:45:50.054 INFO 26675 --- [ main] o.s.w.r.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.reactive.resource.ResourceWebHandler]
2018-05-20 13:45:50.055 INFO 26675 --- [ main] o.s.w.r.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.reactive.resource.ResourceWebHandler]
2018-05-20 13:45:50.503 INFO 26675 --- [ main] org.mongodb.driver.cluster : Cluster created with settings hosts=[ds157599.mlab.com:57599], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500
2018-05-20 13:45:50.830 INFO 26675 --- [ main] org.mongodb.driver.cluster : Cluster created with settings hosts=[ds157599.mlab.com:57599], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500
2018-05-20 13:45:51.129 INFO 26675 --- [.mlab.com:57599] org.mongodb.driver.connection : Opened connection [connectionIdlocalValue:1, serverValue:133602] to ds157599.mlab.com:57599
2018-05-20 13:45:51.161 INFO 26675 --- [.mlab.com:57599] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescriptionaddress=ds157599.mlab.com:57599, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersionversionList=[3, 4, 14], minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=null, roundTripTimeNanos=30750252, setName='rs-ds157599', canonicalAddress=ds157599-a.mlab.com:57599, hosts=[ds157599-a.mlab.com:57599], passives=[], arbiters=[], primary='ds157599-a.mlab.com:57599', tagSet=TagSet[], electionId=7fffffff0000000000000001, setVersion=1, lastWriteDate=Sun May 20 13:45:45 EDT 2018, lastUpdateTimeNanos=67395842952667
2018-05-20 13:45:51.198 INFO 26675 --- [.mlab.com:57599] org.mongodb.driver.connection : Opened connection [connectionIdlocalValue:2, serverValue:133603] to ds157599.mlab.com:57599
2018-05-20 13:45:51.239 INFO 26675 --- [.mlab.com:57599] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescriptionaddress=ds157599.mlab.com:57599, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersionversionList=[3, 4, 14], minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=null, roundTripTimeNanos=41193458, setName='rs-ds157599', canonicalAddress=ds157599-a.mlab.com:57599, hosts=[ds157599-a.mlab.com:57599], passives=[], arbiters=[], primary='ds157599-a.mlab.com:57599', tagSet=TagSet[], electionId=7fffffff0000000000000001, setVersion=1, lastWriteDate=Sun May 20 13:45:45 EDT 2018, lastUpdateTimeNanos=67395921620843
2018-05-20 13:45:51.456 INFO 26675 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2018-05-20 13:45:51.467 INFO 26675 --- [ main] .b.a.e.w.r.WebFluxEndpointHandlerMapping : Mapped "[/actuator/health],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]" onto public org.reactivestreams.Publisher<org.springframework.http.ResponseEntity<java.lang.Object>> org.springframework.boot.actuate.endpoint.web.reactive.AbstractWebFluxEndpointHandlerMapping$ReadOperationHandler.handle(org.springframework.web.server.ServerWebExchange)
2018-05-20 13:45:51.468 INFO 26675 --- [ main] .b.a.e.w.r.WebFluxEndpointHandlerMapping : Mapped "[/actuator/info],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]" onto public org.reactivestreams.Publisher<org.springframework.http.ResponseEntity<java.lang.Object>> org.springframework.boot.actuate.endpoint.web.reactive.AbstractWebFluxEndpointHandlerMapping$ReadOperationHandler.handle(org.springframework.web.server.ServerWebExchange)
2018-05-20 13:45:51.469 INFO 26675 --- [ main] .b.a.e.w.r.WebFluxEndpointHandlerMapping : Mapped "[/actuator],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]" onto protected java.util.Map<java.lang.String, java.util.Map<java.lang.String, org.springframework.boot.actuate.endpoint.web.Link>> org.springframework.boot.actuate.endpoint.web.reactive.WebFluxEndpointHandlerMapping.links(org.springframework.web.server.ServerWebExchange)
2018-05-20 13:45:51.562 INFO 26675 --- [ main] o.s.w.r.r.m.a.ControllerMethodResolver : Looking for @ControllerAdvice: org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@2f9f7dcf: startup date [Sun May 20 13:45:48 EDT 2018]; root of context hierarchy
2018-05-20 13:45:52.538 INFO 26675 --- [ntLoopGroup-2-2] org.mongodb.driver.connection : Opened connection [connectionIdlocalValue:3, serverValue:133604] to ds157599.mlab.com:57599
我的反应式 Mongo 配置:
package com.adrian.mobstersrest.mobsters.config;
import com.mongodb.ConnectionString;
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration;
import org.springframework.data.mongodb.core.mapping.event.LoggingEventListener;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
@SpringBootApplication(exclude = MongoAutoConfiguration.class, MongoDataAutoConfiguration.class)
@EnableReactiveMongoRepositories
@AutoConfigureAfter(EmbeddedMongoAutoConfiguration.class)
public class MongoConfig extends AbstractReactiveMongoConfiguration
@Value("$spring.data.mongodb.uri")
private String uri;
@Override
@Bean
@DependsOn("embeddedMongoServer")
public MongoClient reactiveMongoClient()
return MongoClients.create(new ConnectionString(uri));
@Override
protected String getDatabaseName()
return "mobsters";
@Bean
public LoggingEventListener mongoEventListener()
return new LoggingEventListener();
根据日志,mongo 似乎挂起,它只是位于 Opened 连接,但从未说我的应用程序已初始化。
当连接到在 Docker
容器中运行的本地 Mongo 实例时也会发生这种情况。
【问题讨论】:
【参考方案1】:看来你不应该在这个类中使用@SpringBootApplication。
你应该有一个“主”类,它的主方法已经有@SpringBootApplication,你可以在那里添加排除项,你的项目中不应该有多个@SpringBootApplication。之后,只需删除该注释 MongoConfig 类。
@EnableReactiveMongoRepositories
@AutoConfigureAfter(EmbeddedMongoAutoConfiguration.class)
public class MongoConfig extends AbstractReactiveMongoConfiguration
编辑:我刚刚注意到这是 2 多年前发布的......
【讨论】:
以上是关于Spring Boot Reactive Mongo 在启动时挂起的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot 2. 异步 API。 CompletableFuture vs. Reactive
Spring Boot Couchbase Reactive 不支持分页
Spring Boot Reactive Mongo 在启动时挂起
使用 Spring Security OAuth 时 Spring Boot Reactive WebClient “serverWebExchange must be null”
使用 Spring Boot WebFlux、Spring Data MongoDB Reactive 和 ReactiveMongoRepository 更新 1 个或多个特定字段 MongoDB
找不到 Spring Boot Mongo Reactive 类 - 找不到类 [org.springframework.data.mongodb.MongoDatabaseFactory]