【问题标题】:propertySources Shows Empty (Spring Cloud Config)propertySources 显示为空(Spring Cloud Config)
【发布时间】:2021-09-19 20:17:08
【问题描述】:

我正在尝试学习 Spring Cloud。所以我使用 Spring Starter(一个非常基础的)创建了一个新项目

我像这样配置了我的application.properties

server.port=9090
spring.cloud.config.server.native.searchLocations=file://H:/opt/app/app.static.properties
spring.profiles.active=native

但是当我启动我的 Spring Boot 应用程序时,它可以正常启动(据我所知),但是当我点击 URL http://localhost:9090/config/default/master

我在浏览器上得到输出:

{"name":"config","profiles":["default"],"label":"master","version":null,"state":null,"propertySources":[]}

我的控制台输出是:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
[32m :: Spring Boot :: [39m              [2m (v2.5.2)[0;39m

[2m2021-07-09 20:28:59.573[0;39m [32m INFO[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mcom.example.demo.DemoApplication        [0;39m [2m:[0;39m Starting DemoApplication using Java 1.8.0_281 on HVDIVD17CA50359 with PID 30936 (H:\git\develop\demo\target\classes started by sm187t in H:\git\develop\demo)
[2m2021-07-09 20:28:59.583[0;39m [32m INFO[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mcom.example.demo.DemoApplication        [0;39m [2m:[0;39m The following profiles are active: native
[2m2021-07-09 20:28:59.909[0;39m [32m INFO[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36m.e.DevToolsPropertyDefaultsPostProcessor[0;39m [2m:[0;39m Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
[2m2021-07-09 20:29:06.725[0;39m [32m INFO[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.s.cloud.context.scope.GenericScope    [0;39m [2m:[0;39m BeanFactory id=374f6c33-f58f-370c-bb56-bdf932b163cb
[2m2021-07-09 20:29:09.167[0;39m [32m INFO[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port(s): 9090 (http)
[2m2021-07-09 20:29:09.204[0;39m [32m INFO[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.apache.catalina.core.StandardService  [0;39m [2m:[0;39m Starting service [Tomcat]
[2m2021-07-09 20:29:09.204[0;39m [32m INFO[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36morg.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/9.0.48]
[2m2021-07-09 20:29:09.677[0;39m [32m INFO[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2021-07-09 20:29:09.678[0;39m [32m INFO[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 9764 ms
[2m2021-07-09 20:29:10.197[0;39m [32mDEBUG[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.s.b.w.s.ServletContextInitializerBeans[0;39m [2m:[0;39m Mapping filters: filterRegistrationBean urls=[/*] order=-2147483647, springSecurityFilterChain urls=[/*] order=-100, characterEncodingFilter urls=[/*] order=-2147483648, formContentFilter urls=[/*] order=-9900, requestContextFilter urls=[/*] order=-105
[2m2021-07-09 20:29:10.197[0;39m [32mDEBUG[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.s.b.w.s.ServletContextInitializerBeans[0;39m [2m:[0;39m Mapping servlets: dispatcherServlet urls=[/]
[2m2021-07-09 20:29:11.354[0;39m [32mDEBUG[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerAdapter[0;39m [2m:[0;39m ControllerAdvice beans: 0 @ModelAttribute, 0 @InitBinder, 1 RequestBodyAdvice, 1 ResponseBodyAdvice
[2m2021-07-09 20:29:12.124[0;39m [32mDEBUG[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m 23 mappings in 'requestMappingHandlerMapping'
[2m2021-07-09 20:29:12.275[0;39m [32mDEBUG[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.s.w.s.handler.SimpleUrlHandlerMapping [0;39m [2m:[0;39m Patterns [/webjars/**, /**] in 'resourceHandlerMapping'
[2m2021-07-09 20:29:12.309[0;39m [32mDEBUG[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36m.m.m.a.ExceptionHandlerExceptionResolver[0;39m [2m:[0;39m ControllerAdvice beans: 0 @ExceptionHandler, 1 ResponseBodyAdvice
[2m2021-07-09 20:29:13.756[0;39m [32m INFO[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.s.b.a.e.web.EndpointLinksResolver     [0;39m [2m:[0;39m Exposing 1 endpoint(s) beneath base path '/actuator'
[2m2021-07-09 20:29:13.787[0;39m [32m INFO[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.s.s.web.DefaultSecurityFilterChain    [0;39m [2m:[0;39m Will secure any request with [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@33b379d5, org.springframework.security.web.context.SecurityContextPersistenceFilter@701d0f6c, org.springframework.security.web.header.HeaderWriterFilter@255f44df, org.springframework.web.filter.CorsFilter@32abf174, org.springframework.security.web.csrf.CsrfFilter@5019771a, org.springframework.security.web.authentication.logout.LogoutFilter@74f8b524, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@2364f95a, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@53fb9109, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@64e99fc9, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@54704c84, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@334cbd88, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@7e74ddeb, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@6033bddc, org.springframework.security.web.session.SessionManagementFilter@1b3eed50, org.springframework.security.web.access.ExceptionTranslationFilter@4d7a1b8f, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@4590bbf6]
[2m2021-07-09 20:29:14.137[0;39m [32m INFO[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.s.b.d.a.OptionalLiveReloadServer      [0;39m [2m:[0;39m LiveReload server is running on port 35729
[2m2021-07-09 20:29:15.000[0;39m [32m INFO[0;39m [35m30936[0;39m [2m---[0;39m [2m[on(3)-127.0.0.1][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring DispatcherServlet 'dispatcherServlet'
[2m2021-07-09 20:29:15.001[0;39m [32m INFO[0;39m [35m30936[0;39m [2m---[0;39m [2m[on(3)-127.0.0.1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Initializing Servlet 'dispatcherServlet'
[2m2021-07-09 20:29:15.002[0;39m [32mDEBUG[0;39m [35m30936[0;39m [2m---[0;39m [2m[on(3)-127.0.0.1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Detected StandardServletMultipartResolver
[2m2021-07-09 20:29:15.002[0;39m [32mDEBUG[0;39m [35m30936[0;39m [2m---[0;39m [2m[on(3)-127.0.0.1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Detected AcceptHeaderLocaleResolver
[2m2021-07-09 20:29:15.002[0;39m [32mDEBUG[0;39m [35m30936[0;39m [2m---[0;39m [2m[on(3)-127.0.0.1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Detected FixedThemeResolver
[2m2021-07-09 20:29:15.007[0;39m [32mDEBUG[0;39m [35m30936[0;39m [2m---[0;39m [2m[on(3)-127.0.0.1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Detected org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@611461
[2m2021-07-09 20:29:15.008[0;39m [32mDEBUG[0;39m [35m30936[0;39m [2m---[0;39m [2m[on(3)-127.0.0.1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Detected org.springframework.web.servlet.support.SessionFlashMapManager@7077db3
[2m2021-07-09 20:29:15.008[0;39m [32mDEBUG[0;39m [35m30936[0;39m [2m---[0;39m [2m[on(3)-127.0.0.1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
[2m2021-07-09 20:29:15.009[0;39m [32m INFO[0;39m [35m30936[0;39m [2m---[0;39m [2m[on(3)-127.0.0.1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Completed initialization in 8 ms
[2m2021-07-09 20:29:15.888[0;39m [32m INFO[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port(s): 9090 (http) with context path ''
[2m2021-07-09 20:29:16.252[0;39m [32m INFO[0;39m [35m30936[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mcom.example.demo.DemoApplication        [0;39m [2m:[0;39m Started DemoApplication in 19.628 seconds (JVM running for 24.074)
[2m2021-07-09 20:29:30.465[0;39m [32mDEBUG[0;39m [35m30936[0;39m [2m---[0;39m [2m[nio-9090-exec-1][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped to org.springframework.cloud.config.server.environment.EnvironmentController#labelled(String, String, String)
[2m2021-07-09 20:29:30.522[0;39m [32mDEBUG[0;39m [35m30936[0;39m [2m---[0;39m [2m[nio-9090-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m GET "/config/default/master", parameters={}
[2m2021-07-09 20:29:30.526[0;39m [32mDEBUG[0;39m [35m30936[0;39m [2m---[0;39m [2m[nio-9090-exec-1][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped to org.springframework.cloud.config.server.environment.EnvironmentController#labelled(String, String, String)
[2m2021-07-09 20:29:30.994[0;39m [32mDEBUG[0;39m [35m30936[0;39m [2m---[0;39m [2m[nio-9090-exec-1][0;39m [36mm.m.a.RequestResponseBodyMethodProcessor[0;39m [2m:[0;39m Using 'application/json;q=0.8', given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8] and supported [application/json]
[2m2021-07-09 20:29:31.004[0;39m [32mDEBUG[0;39m [35m30936[0;39m [2m---[0;39m [2m[nio-9090-exec-1][0;39m [36mm.m.a.RequestResponseBodyMethodProcessor[0;39m [2m:[0;39m Writing [Environment [name=config, profiles=[default], label=master, propertySources=[], version=null, state= (truncated)...]
[2m2021-07-09 20:29:31.063[0;39m [32mDEBUG[0;39m [35m30936[0;39m [2m---[0;39m [2m[nio-9090-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Completed 200 OK

propertySources 不应该充满属性文件条目吗?我是新来的。所以主要是从教程和指南中学习。我的理解正确吗?

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 尝试在记录器中启用DEBUG选项并检查日志。
  • 你在windows机器上吗?如果是,那么您在配置文件的绝对路径中缺少一个额外的“/”
  • @Nemanja 我在 Windows 机器上。后来我意识到了这一点,并添加了它。但结果是一样的。我成功了spring.cloud.config.server.native.search-locations=file:///H:/opt/app/app.static.properties
  • @GnanaJeyam 我添加了属性logging.level.web=DEBUG,但我没有看到任何有价值的东西。但这可能是因为我不知道我在寻找什么。我已经用完整的控制台输出更新了主要问题。
  • 而不是原生配置文件。您可以尝试使用dev 配置文件,例如spring.profiles.active=dev,并确保您到达正确的端点。

标签: spring spring-boot spring-cloud spring-cloud-config


【解决方案1】:

searchLocations 属性接受文件和/或子目录的位置。 尝试spring.cloud.config.server.native.searchLocations=file://H:/opt/app/ 而不是spring.cloud.config.server.native.searchLocations=file://H:/opt/app/app.static.properties 并点击http://localhost:9090/config/default/master/app.static.properties

【讨论】:

  • 我必须使用spring.cloud.config.server.native.searchLocations=file:///H:/opt/app/ 和您提供的 URL 才能使其正常工作。谢谢!
  • @hell_storm2004 是的,对于 Windows 机器,您必须添加一个额外的 /
【解决方案2】:

按照以下步骤,我得到了同样的错误。

应用属性如下:

spring.application.name=spring-cloud-config-server
server.port=8888
logging.level.web=DEBUG
spring.cloud.config.server.git.uri=file:///C:/Users/DELL/git-localconfig-repo

添加依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency> 

在限制服务pom.xml 文件中

如果还是不行,请尝试删除limits-service.properties并创建一个新的,或者尝试创建limits-service-dev.propertieslimits-service-prod.properties并通过URL调用它们http://localhost:8888/limits-service/dev并检查它们是否有效..

问题主要是我猜路径或文件名。

【讨论】:

    猜你喜欢
    • 2018-10-31
    • 2016-02-18
    • 1970-01-01
    • 2019-08-09
    • 2016-09-29
    • 2017-06-23
    • 2020-08-07
    • 2018-08-01
    • 2021-03-06
    相关资源
    最近更新 更多