我正在尝试学习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
不应该充满属性文件条目吗?我是新来的。所以主要是从教程和指南中学习。我的理解正确吗?
如有任何帮助,我们将不胜感激。谢谢。
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
如果您使用记事本创建文件,它将放置文件的扩展名,如
limits-service.properties.txt
,而不是您需要文件名 limits-service.properties
。我尝试从 CMD 重命名该文件,它成功了。
将
limits-service.properties.txt
重命名为 limits-service.properties
按照以下步骤,我遇到了同样的错误。
应用程序属性如下:
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.properties
、limits-service-prod.properties
并通过 URL http://localhost:8888/limits-service/dev
调用它们,然后检查它们是否有效..
我猜问题主要是路径或文件名。
尝试添加属性 spring.cloud.config.server.git.searchPaths。这将告诉 spring 你的属性文件在哪个路径。
spring.profiles.active=git
spring.cloud.config.server.git.uri=https://gitlab.com/<project-name>/<app-name>.git
spring.cloud.config.server.git.clone-on-start=true
spring.cloud.config.server.git.default-label=main
spring.cloud.config.server.git.username=git-username
spring.cloud.config.server.git.password=your-access-token-from-git
spring.cloud.config.server.git.searchPaths=path*
如果您使用记事本,请删除 .txt 并将文件另存为 .properties 文件