本項目中config既是spring相關(guān)配置的管理服務(wù),又是整個產(chǎn)品配置參數(shù)的管理服務(wù),所以需要在啟動時就把git或者本地的配置文件提前加載起來,這樣才能正常啟動。如何在系統(tǒng)啟動前就加載配置文件,這個是通過spring.cloud.server.bootstrap參數(shù)控制的,只需要設(shè)置為true即可。
如果我要在項目部署的時候使用本地差異化的配置該如何做呢?
spring cloud config提供了從本地磁盤讀取配置的方法。由于此處使用了bootstrap模式,所以不能使用native模式啟動 否則會報錯。官方提供的方法是使用composite模式,整個配置文件如下:
spring:
application:
name: config-server
profiles:
active: composite,default # 如果要使用本地配置文件,此處需增加composite。多profile時,誰在前面誰的配置優(yōu)先級就高
cloud:
config:
server:
bootstrap: true # 提前加載配置文件,保證后續(xù)數(shù)據(jù)庫連接正常啟動
default-profile: default
default-label: master
composite: # 此配置為使用本地文件,與git脫離關(guān)系
- type: native
search-locations: file:C:/workspace/javaProjects/ai-cloud-conf/combat-center/
git:
uri: http://git.aixsj.com/aitensor/ai-cloud-conf.git
search-paths: combat-center/
username: config
password: config2018
fail-fast: true