git配置倉庫
在spring cloud config
的服務端,對于配置倉庫的默認實現采用了git。git非常適用于存儲配置內容,它可以非常方便的使用各種第三方工具來對內容進行管理更新和版本化,同時git倉庫的hook功能還可以幫助我們實時的監控配置內容的修改。其中,git自身的版本控制功能正是其他一些配置中心所欠缺的,通過git進行存儲意味著,一個應用的不同部署實例可以從spring cloud config
的服務端獲取不同的版本配置,從而支持一些特殊的應用場景。
由于spring cloud config
中默認使用git,所以我們只需要在config server
中的application.properties
中設置spring.cloud.config.server.git.uri
屬性,比如下面的配置:
spring:
application:
name: config-server-git
cloud:
config:
server:
git:
uri: http://git.oschina.net/zhihaomiao/config-repo-demo
username:
password:
search-paths: config-repo
如果我們將該值通過file://前綴來設置為一個文件地址(在window系統中,要使用file:///來定位文件內容),那么它將以本地倉庫的方式運行,這樣我們就可以脫離git服務端來快速進行調試與開發,比如:因為在快速入門的時候我們知道配置git倉庫的時候讀取配置會從git遠程倉庫中git clone到本地,我的控制臺日志告訴我下載到本地的/var/folders/p0/kw_s_8xj2gqc929nys7cj2yh0000gn/T/config-repo-2847833657021753497/
文件夾下(當然自己也可以git clone
遠程的配置信息到本地),所以如下配置(spring.cloud.config.server.git.uri=file://${user.home}/config-repo
)
spring:
application:
name: config-server-git
cloud:
config:
server:
git:
uri: /var/folders/p0/kw_s_8xj2gqc929nys7cj2yh0000gn/T/config-repo-2847833657021753497/config-repo
其中,${user.home}
代表當前用戶的所屬目錄,file://配置的本地文件系統方式雖然對于本地開發調試時使用非常方便,但是該方式也僅用于開發與測試,在生產環境中務必搭建自己的git倉庫來存儲配置資源。
With VCS based backends (git, svn) files are checked out or cloned to the local filesystem. By default they are put in the system temporary directory with a prefix of config-repo-. On linux, for example it could be /tmp/config-repo-<randomid>. Some operating systems routinely clean out temporary directories. This can lead to unexpected behaviour such as missing properties. To avoid this problem, change the directory Config Server uses, by setting
spring.cloud.config.server.git.basedir or spring.cloud.config.server.svn.basedir to a directory that does not reside in the system temp structure.
使用基于VCS的后端(git,svn)文件被檢出或克隆到本地文件系統。 默認情況下,它們放在系統臨時目錄中,前綴為config-repo-
。 在linux上,例如可以是/tmp/config-repo- <randomid>
。 一些操作系統會定期清除臨時目錄。 這可能會導致意外的行為,例如缺少屬性。 為避免此問題,請通過將spring.cloud.config.server.git.basedir
或spring.cloud.config.server.svn.basedir
設置為不駐留在系統臨時結構中的目錄來更改Config Server
所使用的目錄。
占位符配置url
{application}
,{profile}
,{label}
這些占位符除了用于標識配置文件的規則之外,還可以用于config Server
中對git
倉庫地址的url配置。比如我們可以通過{application}
占位符實現一個應用對應一個git倉庫目錄的配置效果,具體配置實現:
spring.cloud.config.server.git.uri=http://git.oschina.net/zhihaomiao/{application}
spring.cloud.config.server.git.username=username
spring.cloud.config.server.git.password=password
其中,{application}
代表了應用名,所以當客戶端應用向config server
發起獲取配置的請求時,Config server
會根據客戶端的spring.application.name
信息來填充{application}
占位符以定位配置資源的存儲位置,從而實現根據微服務應用的屬性動態獲取不同的配置。另外,在這些占位符中,{label}
參數較為特別,如果git的分支和標簽名包含"/",那么{label}參數在http的url中應該使用"(_)"來代替,以避免改變了url含義,指向到其他的url資源。
當我們使用git作為配置中心來存儲各個微服務應用配置文件的時候,該功能會變得非常有用,通過在url中使用占位符可以幫助我們規劃和實現通用的倉庫配置,比如,下面的規劃:
- 代碼庫:使用服務名作為git倉庫名稱,比如用戶服務的代碼庫
http://git.oschina.net/zhihaomiao/user-service
- 配置庫:使用服務名加上
-config
后綴作為git倉庫名稱,比如用戶服務的配置庫地址位置是http://git.oschina.net/zhihaomiao/user-service-config
這時,我們就可以使用spring.cloud.config.server.git.uri=http://git.oschina.net/zhihaomiao/{application}-config
配置,來同時匹配多個不同服務的配置倉庫。
demo
首先在git上建立一個倉庫,倉庫名為 user-service-config
具體的配置文件如下:
為不同的環境配置不同的配置信息等等:
spring:
datasource:
username: user-dev
check:
uri: default-1.0
定義config server
中的配置文件為:
spring:
application:
name: config-server-git
cloud:
config:
server:
git:
uri: http://git.oschina.net/zhihaomiao/{application}-config
username: zhihao.miao
password: 13579qwertyu
server:
port: 9090
然后定義一個user-service
服務,配置文件bootstrap.yml
文件如下:
spring:
application:
name: user-service
cloud:
config:
uri: http://localhost:9090
profile: pro
label: master
server:
port: 7070
user-service
中定義UserController
:
@RestController
@RequestMapping("/user")
public class UserController {
private Logger log = LoggerFactory.getLogger(getClass());
@Value("${spring.datasource.username}")
private String username;
@Value("${check.uri}")
private String checkurl;
@GetMapping("/index")
public String index(){
log.info("username="+username+",check.uri=="+username);
return "username="+username+",check.uri==="+checkurl;
}
}
訪問:
http://localhost:7070/user/index
說明
這種配置方式我認為是最好的,每個服務(user-service
)對應一個git上的倉庫(命名為user-service-config
),對應的微服務項目代碼可以在git上建立(user-service
)倉庫,將代碼和配置很好的分離開來,當然還可以在config server中這樣配置(http://git.oschina.net/zhihaomiao/{application}-config-{profile}
),一個環境對應git上的一個倉庫,不過太細粒度了,不建議這么使用。
我們發現在快速入門中的配置是在通過一個git倉庫中定義不同的{application}-{profile}.yml
,這樣也是一種多項目配置文件的管理方式,關于哪種配置更加適合,當然還是具體問題具體分析,沒有最好的方式,只有最合適的方式。
參考資料
Git Backend-Placeholders in Git URI
配置多個倉庫
config server
除了可以通過application
和profile
模式來匹配配置倉庫之外,還支持通過帶有通配符的表達式來匹配,以實現更為復雜的配置要求。并且當我們有多個匹配規則的時候,還可以通過逗號來分割多個{application}/{profile}配置規則,比如:
spring.cloud.config.server.git.uri=http://git.oschina.net/zhihaomiao/config-repo
spring.cloud.config.server.git.repos.dev.pattern=dev/*
spring.cloud.config.server.git.repos.dev.uri=file://home/git/config-repo
spring.cloud.config.server.git.repos.test=http://git.oschina.net/test/config-repo
spring.cloud.config.server.git.repos.prod.pattern=prod/pp*,online/oo*
spring.cloud.config.server.git.repos.prod.uri=http://git.oschina.net/prod/config-repo
上述配置內容通過配置內容spring.cloud.config.server.git.uri
屬性,指定了一個默認的倉庫位置,當使用{application}/{profile}
模式未能匹配到合適的倉庫時,就將在該默認倉庫位置下獲取配置信息。除此之外,還配置了三個倉庫,分別是dev
,test
,prod
。其中,dev
倉庫匹配dev/*
的模式,所以無論profile
是什么,它都能匹配application
名稱為dev
的應用。并且我們可以注意到,它存儲的配置文件位置還采用了config server
的本地文件系統中的內容。對于此位置,我們可以通過訪問http://localhost:9090/dev/profile
的請求來驗證到該倉庫的配置內容,其中profile
可以是任意值。而test
和prod
倉庫均使用git倉庫的存儲,并且test倉庫未配置匹配規則,所以它只匹配application
名為test
的應用;prod
倉庫則需要匹配application
為prod
并且profile
為pp
開頭,或者application
為online
并且profile
為oo
開頭的應用和環境。
當配置多個倉庫的時候,config server
在啟動的時會直接克隆第一個倉庫的配置庫,其他的配置只有在請求時才會克隆到本地,所以對于倉庫的排列可以根據配置內容的重要程度有所區分。另外,如果表達式是以通配符開始的,那么需要使用引號將配置內容引起來。
注意
- 這種方式的配置肯定是不好的,造成管理的混亂,
spring.cloud.config.server.git.uri
配置是默認的回退配置,如果其他條件都不符合或者是配置的屬性在其匹配的模式中并沒有得到,那么就會去spring.cloud.config.server.git.uri
去匹配。 - 本列中的repos后面的dev,test,prod都是對應著application應用名稱。
- 上面的pattern配置如果在yml中配置的話,那么還有另外一種寫法,可以看官網,使用-這邊就不展示了。
參考資料
Pattern Matching and Multiple Repositories
cloneOnStart的用法
By default the server clones remote repositories when configuration is first requested. The server can be configured to clone the repositories at startup. For example at the top level:
默認情況下,首次請求配置時,服務器克隆遠程存儲庫。 服務器可以配置為在啟動時克隆存儲庫。 例如在頂層:
spring:
cloud:
config:
server:
git:
uri: https://git/common/config-repo.git
repos:
team-a:
pattern: team-a-*
cloneOnStart: true
uri: http://git/team-a/config-repo.git
team-b:
pattern: team-b-*
cloneOnStart: false
uri: http://git/team-b/config-repo.git
team-c:
pattern: team-c-*
uri: http://git/team-a/config-repo.git
In this example the server clones team-a’s config-repo on startup before it accepts any requests. All other repositories will not be cloned until configuration from the repository is requested.
在此示例中,服務器在啟動之前克隆了team-a的config-repo,然后它接受任何請求。 所有其他存儲庫將不被克隆,直到請求時才發起克隆的操作。
cloneOnStart也可以配置在repos下,這個時候就會將下面的所有應用的配置文件在服務啟動的時候克隆到本地。
好處
Setting a repository to be cloned when the Config Server starts up can help to identify a misconfigured configuration source (e.g., an invalid repository URI) quickly, while the Config Server is starting up. With cloneOnStart not enabled for a configuration source, the Config Server may start successfully with a misconfigured or invalid configuration source and not detect an error until an application requests configuration from that configuration source.
在配置服務器啟動時設置要克隆的存儲庫可以幫助在配置服務器啟動時快速識別配置錯誤的配置源(例如,無效的存儲庫URI)。 使用cloneOnStart未啟用配置源時,配置服務器可能啟動成功配置錯誤或無效的配置源,并且不會檢測到錯誤,直到應用程序從該配置源請求配置時才發現配置錯誤。
參考資料
cloneOnStart
Placeholders in Git Search Paths
子目錄存儲
除了支持占位符配置,多倉庫配置之外,config server
還可以將配置文件定位到git倉庫的子目錄中。我們在快速入門中的我們除了配置spring.cloud.config.server.git.uri
之外海配置了另外一個參數:spring.cloud.config.server.git.search-paths
,通過這個參數可以實現在http://git.oschina.net/zhihaomiao/config-repo-demo
倉庫的config-repo
子目錄下實現配置的存儲。
spring:
application:
name: config-server-git
cloud:
config:
server:
git:
uri: http://git.oschina.net/zhihaomiao/config-repo-demo
username:
password:
search-paths: config-repo
server:
port: 9090
通過上面的配置,我們可以實現http://git.oschina.net/zhihaomiao/config-repo-demo
倉庫下,一個應用一個目錄的效果。
對于spring.cloud.config.server.git.search-paths
參數的配置也支持使用{application}
,{profile}
和{label}
占位符,比如:
spring:
application:
name: config-server-git
cloud:
config:
server:
git:
uri: http://git.oschina.net/zhihaomiao/config-repo-demo
username:
password:
search-paths: {application}
server:
port: 9090
這種方式也可以一個服務一個目錄,這樣就可以在一個倉庫中管理多個服務的配置,這種方式也比較好。
參考資料
Pattern Matching and Multiple Repositories
Placeholders in Git Search Paths
訪問權限
config server
在訪問git倉庫的時候,若采用http的方式進行認證,那么我們需要增加username
和password
屬性來配置賬戶,比如快速入門的demo
spring:
application:
name: config-server-git
cloud:
config:
server:
git:
uri: http://git.oschina.net/zhihaomiao/config-repo-demo
username:
password:
search-paths: config-repo
server:
port: 9090
若不采用http的認證方式,我們也可以采用ssh的方式,通過生成key并在git倉庫中進行配置匹配以實現訪問。
svn配置倉庫
config server
除了支持git
倉庫之外,也能使用svn
倉庫,只需要如下配置。
- 在pom.xml中引入
svn
的依賴配置,讓config server
擁有讀取svn
內容的能力: - 在
application.properties
中使用svn
的配置屬性來指定svn
服務器的位置,以及訪問的賬戶名與密碼:
spring.cloud.config.server.svn.uri=svn://localhost:443/didispace/config-repo
spring.cloud.config.server.svn.username=username
spring.cloud.config.server.svn.password=password
通過上面的配置修改,config server
就可以使用svn
作為倉庫來存儲配置文件了,對于客戶端來說,這個過程是透明的,所以不需要做任何變動。
本地倉庫
在使用了git
或svn
倉庫之后,文件都會在config server
的本地文件系統中存儲一份,這些文件默認會被存儲以config-repo
為前綴的臨時目錄中,比如/tmp/config-repo-<隨機數>
的目錄。由于其隨機性以及臨時目錄的特性,可能會有一些不可預知的后果,為了避免將來可能會出現的問題,最好的方法就是指定一個固定的位置來存儲這些重要信息。我們只需要通過spring.cloud.config.server.git.basedir
或spring.cloud.config.server.svn.basedir
來配置一個我們準備好的目錄即可。
本地文件系統
spring cloud config
也提供了一種不適用git
倉庫或svn
倉庫的存儲方式,而是使用本地文件系統的存儲方式來保存配置信息。實現方式也簡單,只需要配置屬性spring.profile.active=native
,config server
會默認從應用的src/main/resource
目錄下搜索配置文件。如果需要指定搜索配置文件的路徑,我們可以通過spring.cloud.config.server.native.searchLocations
屬性來指定具體的配置文件位置。
雖然spring cloud config
提供了這樣的功能,但是為了支持更好的內容管理和版本控制等強大功能,還是推薦使用git倉庫的方式
本博客代碼
代碼地址
配置倉庫
user服務配置倉庫
order服務配置倉庫