Spring Boot YML配置文件未根据活动配置文件正确解析

时间:2018-05-20 16:13:08

标签: mongodb spring-boot yaml

我有以下YML文件:

spring:
  profiles: prod
spring:
  data:
    mongodb:
      uri: ${MONGODB_URI}
---
spring:
  profiles: dev
spring:
  data:
    mongodb:
      uri: mongodb://admin:password@localhost:27017/mobsters

我正在尝试使用prod个人资料。

根据日志,Spring Boot激活了正确的个人资料,但使用了错误的mongo uri? (uri代表dev

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.1.RELEASE)

2018-05-20 12:02:48.183 DEBUG 24897 --- [           main] s.b.w.r.c.StandardReactiveWebEnvironment : Adding PropertySource 'systemProperties' with lowest search precedence
2018-05-20 12:02:48.183 DEBUG 24897 --- [           main] s.b.w.r.c.StandardReactiveWebEnvironment : Adding PropertySource 'systemEnvironment' with lowest search precedence
2018-05-20 12:02:48.183 DEBUG 24897 --- [           main] s.b.w.r.c.StandardReactiveWebEnvironment : Initialized StandardReactiveWebEnvironment with PropertySources [MapPropertySource {name='systemProperties'}, SystemEnvironmentPropertySource {name='systemEnvironment'}]
2018-05-20 12:02:48.267  INFO 24897 --- [           main] c.a.m.mobsters.MobstersApplication       : Starting MobstersApplication on Adrians-iMac.local with PID 24897 (/Users/adrian/IdeaProjects/MobstersREST/backend/target/classes started by adrian in /Users/adrian/IdeaProjects/MobstersREST)
2018-05-20 12:02:48.267  INFO 24897 --- [           main] c.a.m.mobsters.MobstersApplication       : The following profiles are active: prod
2018-05-20 12:02:48.267 DEBUG 24897 --- [           main] o.s.boot.SpringApplication               : Loading source class com.adrian.mobstersrest.mobsters.MobstersApplication
2018-05-20 12:02:48.316 DEBUG 24897 --- [           main] o.s.b.c.c.ConfigFileApplicationListener  : Activated profiles prod
2018-05-20 12:02:48.316 DEBUG 24897 --- [           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 12:02:48.320  INFO 24897 --- [           main] onfigReactiveWebServerApplicationContext : Refreshing org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@3349e9bb: startup date [Sun May 20 12:02:48 EDT 2018]; root of context hierarchy
2018-05-20 12:02:48.321 DEBUG 24897 --- [           main] onfigReactiveWebServerApplicationContext : Bean factory for org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@3349e9bb: org.springframework.beans.factory.support.DefaultListableBeanFactory@40cb8df7: 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 12:02:49.273 DEBUG 24897 --- [           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 12:02:49.696 DEBUG 24897 --- [           main] onfigReactiveWebServerApplicationContext : Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@1bdaa23d]
2018-05-20 12:02:49.696 DEBUG 24897 --- [           main] onfigReactiveWebServerApplicationContext : Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@79f227a9]
2018-05-20 12:02:49.986  INFO 24897 --- [           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 12:02:49.997  INFO 24897 --- [           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 12:02:49.997  INFO 24897 --- [           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 12:02:49.998  INFO 24897 --- [           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 12:02:50.066  INFO 24897 --- [           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 12:02:50.066  INFO 24897 --- [           main] o.s.w.r.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.reactive.resource.ResourceWebHandler]
2018-05-20 12:02:50.318  INFO 24897 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2018-05-20 12:02:50.537  INFO 24897 --- [localhost:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server localhost:27017

com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='admin', source='mobsters', password=<hidden>, mechanismProperties={}}
    at com.mongodb.connection.SaslAuthenticator.wrapException(SaslAuthenticator.java:162) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.SaslAuthenticator.access$200(SaslAuthenticator.java:39) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:68) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:46) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.SaslAuthenticator.doAsSubject(SaslAuthenticator.java:168) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:46) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:122) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:52) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:127) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) ~[mongodb-driver-core-3.6.3.jar:na]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_171]
Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }
    at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:164) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:295) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.CommandHelper.sendAndReceive(CommandHelper.java:84) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:34) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:119) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.SaslAuthenticator.access$000(SaslAuthenticator.java:39) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:52) ~[mongodb-driver-core-3.6.3.jar:na]
    ... 9 common frames omitted

2018-05-20 12:02:50.567  INFO 24897 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2018-05-20 12:02:50.581  INFO 24897 --- [localhost:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server localhost:27017

com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='admin', source='mobsters', password=<hidden>, mechanismProperties={}}
    at com.mongodb.connection.SaslAuthenticator.wrapException(SaslAuthenticator.java:162) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.SaslAuthenticator.access$200(SaslAuthenticator.java:39) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:68) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:46) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.SaslAuthenticator.doAsSubject(SaslAuthenticator.java:168) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:46) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:122) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:52) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:127) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) ~[mongodb-driver-core-3.6.3.jar:na]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_171]
Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }
    at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:164) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:295) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.CommandHelper.sendAndReceive(CommandHelper.java:84) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:34) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:119) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.SaslAuthenticator.access$000(SaslAuthenticator.java:39) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:52) ~[mongodb-driver-core-3.6.3.jar:na]
    ... 9 common frames omitted

2018-05-20 12:02:51.115  INFO 24897 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2018-05-20 12:02:51.125  INFO 24897 --- [           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 12:02:51.126  INFO 24897 --- [           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 12:02:51.127  INFO 24897 --- [           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 12:02:51.223  INFO 24897 --- [           main] o.s.w.r.r.m.a.ControllerMethodResolver   : Looking for @ControllerAdvice: org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@3349e9bb: startup date [Sun May 20 12:02:48 EDT 2018]; root of context hierarchy
2018-05-20 12:02:51.903  INFO 24897 --- [           main] org.mongodb.driver.cluster               : No server chosen by com.mongodb.async.client.ClientSessionHelper$1@5d3b58ca from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='admin', source='mobsters', password=<hidden>, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }}}]}. Waiting for 30000 ms before timing out
2018-05-20 12:03:21.910  INFO 24897 --- [1e97d61411ef4e7] org.mongodb.driver.cluster               : No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='admin', source='mobsters', password=<hidden>, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }}}]}. Waiting for 30000 ms before timing out
2018-05-20 12:03:51.917  WARN 24897 --- [           main] onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'queuedActionService' defined in file [/Users/adrian/IdeaProjects/MobstersREST/backend/target/classes/com/adrian/mobstersrest/mobsters/actions/QueuedActionService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dailyActions' defined in class path resource [com/adrian/mobstersrest/mobsters/bean/DailyActionsConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.Queue]: Factory method 'dailyActions' threw exception; nested exception is org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='admin', source='mobsters', password=<hidden>, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='admin', source='mobsters', password=<hidden>, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }}}]
2018-05-20 12:03:51.925 DEBUG 24897 --- [           main] .c.l.ClasspathLoggingApplicationListener : Application failed to start 

正如您在日志中看到的那样:

c.a.m.mobsters.MobstersApplication : The following profiles are active: prod

1 个答案:

答案 0 :(得分:1)

对于“spring”根密钥和每个配置文件,yaml参数树应该是唯一的。

这应该可以解决问题:

{player.primaryPositionNumber}

顺便说一下,也可以写下这样的东西:

spring:
  profiles: prod
  data:
    mongodb:
      uri: ${MONGODB_URI}
---
spring:
  profiles: dev
  data:
    mongodb:
      uri: mongodb://admin:password@localhost:27017/mobsters

这样如果设置了env变量uri: ${MONGODB_URI:mongodb://admin:password@localhost:27017/mobsters} ,它将是使用的值,否则将使用默认值MONGODB_URI