Trong ví dụ này chạy server trên port 8888 và lưu cấu hình ở github với uri như trên.
Mặc định thì các file cấu hình lưu ở root folder, tuy nhiên nếu muốn load các file cấu hình ở folder con ta có thể dùng thêm search-paths
Trường hợp git repository của bạn là private thì bạn có thể dùng thêm thuộc tính username, password
Nếu bạn không muốn dùng github mà dùng một folder trên local để chứa các file cấu hình thì bạn thay uri bằng địa chỉ folder đó là được. (lưu ý, folder đó cũng phải là 1 repository, và chỉ những data đã được commit mới được sử dụng)
Đây là git repository của mình:
Gồm các file chứa cấu hình cho ứng dụng spring có name là app với các profiles khác nhau:
Tương tự mình có một sub folder để chứa cấu hình cho ứng dụng spring có name là demo với các profiles tương ứng:
Start ứng dụng spring boot này:
._________//___'_____(_)______(()___ | '_ |'_| | '_/_` | / ___)| |_)| | | | | || (_| | )))) ' |____| .__|_| |_|_| |___, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.3.RELEASE)2020-08-2009:54:57.120 INFO 7956 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:88882020-08-2009:54:58.878 INFO 7956 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=app, profiles=[dev], label=null, version=34f63fea1576b0c5e5bb67d9a32420d10cb48f3f, state=null2020-08-2009:54:58.879 INFO 7956 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-configClient'}, BootstrapPropertySource {name='bootstrapProperties-https://github.com/stackjava/spring-cloud-config-server-repo.git/app-dev.properties'}, BootstrapPropertySource {name='bootstrapProperties-https://github.com/stackjava/spring-cloud-config-server-repo.git/app.properties'}]2020-08-2009:54:58.884 INFO 7956 --- [ main] s.c.s.SpringCloudConfigClientApplication : The following profiles are active: dev2020-08-2009:54:59.256 INFO 7956 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=ab77c954-7bc9-3c62-b748-55ed0878c2e02020-08-2009:54:59.407 INFO 7956 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 7777(http)2020-08-2009:54:59.413 INFO 7956 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]2020-08-2009:54:59.413 INFO 7956 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37]2020-08-2009:54:59.415 INFO 7956 --- [ main] o.a.catalina.core.AprLifecycleListener : An older version [1.2.21] of the Apache Tomcat Native library is installed, while Tomcat recommends a minimum version of [1.2.23]2020-08-2009:54:59.415 INFO 7956 --- [ main] o.a.catalina.core.AprLifecycleListener : Loaded Apache Tomcat Native library [1.2.21] using APR version [1.6.5].2020-08-2009:54:59.415 INFO 7956 --- [ main] o.a.catalina.core.AprLifecycleListener : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].2020-08-2009:54:59.415 INFO 7956 --- [ main] o.a.catalina.core.AprLifecycleListener : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]2020-08-2009:54:59.418 INFO 7956 --- [ main] o.a.catalina.core.AprLifecycleListener : OpenSSL successfully initialized [OpenSSL 1.1.1a20 Nov 2018]2020-08-2009:54:59.499 INFO 7956 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2020-08-2009:54:59.499 INFO 7956 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 603 ms2020-08-2009:54:59.643 INFO 7956 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'2020-08-2009:55:00.429 INFO 7956 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 7777(http) with context path ''2020-08-2009:55:01.028 INFO 7956 --- [ main] s.c.s.SpringCloudConfigClientApplication : Started SpringCloudConfigClientApplication in 6.098seconds(JVM running for7.174)2020-08-2009:55:37.804 INFO 7956 --- [nio-7777-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'2020-08-2009:55:37.804 INFO 7956 --- [nio-7777-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'2020-08-2009:55:37.809 INFO 7956 --- [nio-7777-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms