spring cloud config學習二:配置倉庫

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.basedirspring.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除了可以通過applicationprofile模式來匹配配置倉庫之外,還支持通過帶有通配符的表達式來匹配,以實現更為復雜的配置要求。并且當我們有多個匹配規則的時候,還可以通過逗號來分割多個{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可以是任意值。而testprod倉庫均使用git倉庫的存儲,并且test倉庫未配置匹配規則,所以它只匹配application名為test的應用;prod倉庫則需要匹配applicationprod并且profilepp開頭,或者applicationonline并且profileoo開頭的應用和環境。

當配置多個倉庫的時候,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的方式進行認證,那么我們需要增加usernamepassword屬性來配置賬戶,比如快速入門的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作為倉庫來存儲配置文件了,對于客戶端來說,這個過程是透明的,所以不需要做任何變動。

本地倉庫

在使用了gitsvn倉庫之后,文件都會在config server的本地文件系統中存儲一份,這些文件默認會被存儲以config-repo為前綴的臨時目錄中,比如/tmp/config-repo-<隨機數>的目錄。由于其隨機性以及臨時目錄的特性,可能會有一些不可預知的后果,為了避免將來可能會出現的問題,最好的方法就是指定一個固定的位置來存儲這些重要信息。我們只需要通過spring.cloud.config.server.git.basedirspring.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服務配置倉庫

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,763評論 6 539
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,238評論 3 428
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 177,823評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,604評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,339評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,713評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,712評論 3 445
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,893評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,448評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,201評論 3 357
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,397評論 1 372
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,944評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,631評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,033評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,321評論 1 293
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,128評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,347評論 2 377

推薦閱讀更多精彩內容