雖然在開發過程,在本地建立git倉庫操做起來很是方便,可是在實際項目應用中,多個項目組須要經過一箇中心服務器來共享配置,因此Spring Cloud配置中心支持遠程git倉庫,以使分散的項目組更方便的進行協做。git
Gitee碼雲web
首先我在gitee上建立了一個遠程倉庫https://gitee.com/zxuqian/spring-cloud-config-remote
專門用來存放配置文件,而後咱們會經過配置文件來訪問此倉庫。而後咱們把之前本地的配置文件遷移到此庫中。爲了測試效果,咱們把web-client.yml
的message
的值修改成:此條消息來自於遠程配置倉庫
spring
如今在咱們以前的configserver中做一些配置上的改動。首先爲了保留以前的本地倉庫的配置,咱們把application.yml
重命名爲application-local.yml
。bootstrap
這個-local
是一個profile
,它的值是-
後面的,即local
,咱們能夠在bootstrap.yml
中指定使用哪一個profile
。好比實際項目中開發階段和生產階段的配置有所不一樣,因此會有application-development.yml
和application-production.yml
等兩種或以上的配置。服務器
而後新建一個application-remote.yml
文件,添加以下配置內容:app
server: port: 8888 spring: cloud: config: server: git: uri: https://gitee.com/zxuqian/spring-cloud-config-remote username: 您的gitee用戶名 password: 您的gitee密碼
由於是自用帳號的倉庫,因此就不提供帳號密碼了,改爲本身對應的。這裏uri
配置了遠程git倉庫的地址。spring-boot
最後在bootstrap.yml
中啓用咱們的remote
profile:測試
spring: application: name: config-server profiles: active: remote
spring.profiles.active
即指定了咱們的remote
Profile,使用application-remote.yml
配置文件。code
使用spring-boot:run
啓動咱們的config server,而後訪問http://localhost:8888/web-client/default
,看到以下結果:server
{"name":"web-client","profiles":["default"],"label":null,"version":"cbef7d379ef01d68810c3fdc2105b2226ea6c611","state":null,"propertySources":[{"name":"https://gitee.com/zxuqian/spring-cloud-config-remote/web-client.yml","source":{"message":"此條消息來自於遠程配置倉庫","management.endpoints.web.exposure.include":"*"}}]}
message
的值取自於遠程倉庫。這裏的web-client/default
是配置文件名/profile
,由於咱們的web客戶端項目沒有其餘Profile,則默認值爲default
,只有這樣寫全,才能夠訪問到web-client.yml
的配置。