spring cloud config學習二:配置倉庫

git配置倉庫

spring cloud config的服務(wù)端,對于配置倉庫的默認實現(xiàn)采用了git。git非常適用于存儲配置內(nèi)容,它可以非常方便的使用各種第三方工具來對內(nèi)容進行管理更新和版本化,同時git倉庫的hook功能還可以幫助我們實時的監(jiān)控配置內(nèi)容的修改。其中,git自身的版本控制功能正是其他一些配置中心所欠缺的,通過git進行存儲意味著,一個應(yīng)用的不同部署實例可以從spring cloud config的服務(wù)端獲取不同的版本配置,從而支持一些特殊的應(yīng)用場景。

由于spring cloud config中默認使用git,所以我們只需要在config server中的application.properties中設(shè)置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://前綴來設(shè)置為一個文件地址(在window系統(tǒng)中,要使用file:///來定位文件內(nèi)容),那么它將以本地倉庫的方式運行,這樣我們就可以脫離git服務(wù)端來快速進行調(diào)試與開發(fā),比如:因為在快速入門的時候我們知道配置git倉庫的時候讀取配置會從git遠程倉庫中g(shù)it 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://配置的本地文件系統(tǒng)方式雖然對于本地開發(fā)調(diào)試時使用非常方便,但是該方式也僅用于開發(fā)與測試,在生產(chǎn)環(huán)境中務(wù)必搭建自己的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)文件被檢出或克隆到本地文件系統(tǒng)。 默認情況下,它們放在系統(tǒng)臨時目錄中,前綴為config-repo-。 在linux上,例如可以是/tmp/config-repo- <randomid>。 一些操作系統(tǒng)會定期清除臨時目錄。 這可能會導致意外的行為,例如缺少屬性。 為避免此問題,請通過將spring.cloud.config.server.git.basedirspring.cloud.config.server.svn.basedir設(shè)置為不駐留在系統(tǒng)臨時結(jié)構(gòu)中的目錄來更改Config Server所使用的目錄。

占位符配置url

{application},{profile},{label}這些占位符除了用于標識配置文件的規(guī)則之外,還可以用于config Server中對git倉庫地址的url配置。比如我們可以通過{application}占位符實現(xiàn)一個應(yīng)用對應(yīng)一個git倉庫目錄的配置效果,具體配置實現(xiàn):

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}代表了應(yīng)用名,所以當客戶端應(yīng)用向config server發(fā)起獲取配置的請求時,Config server會根據(jù)客戶端的spring.application.name信息來填充{application}占位符以定位配置資源的存儲位置,從而實現(xiàn)根據(jù)微服務(wù)應(yīng)用的屬性動態(tài)獲取不同的配置。另外,在這些占位符中,{label}參數(shù)較為特別,如果git的分支和標簽名包含"/",那么{label}參數(shù)在http的url中應(yīng)該使用"(_)"來代替,以避免改變了url含義,指向到其他的url資源。

當我們使用git作為配置中心來存儲各個微服務(wù)應(yīng)用配置文件的時候,該功能會變得非常有用,通過在url中使用占位符可以幫助我們規(guī)劃和實現(xiàn)通用的倉庫配置,比如,下面的規(guī)劃:

  • 代碼庫:使用服務(wù)名作為git倉庫名稱,比如用戶服務(wù)的代碼庫http://git.oschina.net/zhihaomiao/user-service
  • 配置庫:使用服務(wù)名加上-config后綴作為git倉庫名稱,比如用戶服務(wù)的配置庫地址位置是http://git.oschina.net/zhihaomiao/user-service-config

這時,我們就可以使用spring.cloud.config.server.git.uri=http://git.oschina.net/zhihaomiao/{application}-config配置,來同時匹配多個不同服務(wù)的配置倉庫。

demo
首先在git上建立一個倉庫,倉庫名為 user-service-config

具體的配置文件如下:


為不同的環(huán)境配置不同的配置信息等等:

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服務(wù),配置文件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

說明
這種配置方式我認為是最好的,每個服務(wù)(user-service)對應(yīng)一個git上的倉庫(命名為user-service-config),對應(yīng)的微服務(wù)項目代碼可以在git上建立(user-service)倉庫,將代碼和配置很好的分離開來,當然還可以在config server中這樣配置(http://git.oschina.net/zhihaomiao/{application}-config-{profile}),一個環(huán)境對應(yīng)git上的一個倉庫,不過太細粒度了,不建議這么使用。

我們發(fā)現(xiàn)在快速入門中的配置是在通過一個git倉庫中定義不同的{application}-{profile}.yml,這樣也是一種多項目配置文件的管理方式,關(guān)于哪種配置更加適合,當然還是具體問題具體分析,沒有最好的方式,只有最合適的方式。

參考資料
Git Backend-Placeholders in Git URI

配置多個倉庫

config server除了可以通過applicationprofile模式來匹配配置倉庫之外,還支持通過帶有通配符的表達式來匹配,以實現(xiàn)更為復(fù)雜的配置要求。并且當我們有多個匹配規(guī)則的時候,還可以通過逗號來分割多個{application}/{profile}配置規(guī)則,比如:

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

上述配置內(nèi)容通過配置內(nèi)容spring.cloud.config.server.git.uri屬性,指定了一個默認的倉庫位置,當使用{application}/{profile}模式未能匹配到合適的倉庫時,就將在該默認倉庫位置下獲取配置信息。除此之外,還配置了三個倉庫,分別是dev,test,prod。其中,dev倉庫匹配dev/*的模式,所以無論profile是什么,它都能匹配application名稱為dev的應(yīng)用。并且我們可以注意到,它存儲的配置文件位置還采用了config server的本地文件系統(tǒng)中的內(nèi)容。對于此位置,我們可以通過訪問http://localhost:9090/dev/profile的請求來驗證到該倉庫的配置內(nèi)容,其中profile可以是任意值。而testprod倉庫均使用git倉庫的存儲,并且test倉庫未配置匹配規(guī)則,所以它只匹配application名為test的應(yīng)用;prod倉庫則需要匹配applicationprod并且profilepp開頭,或者applicationonline并且profileoo開頭的應(yīng)用和環(huán)境。

當配置多個倉庫的時候,config server在啟動的時會直接克隆第一個倉庫的配置庫,其他的配置只有在請求時才會克隆到本地,所以對于倉庫的排列可以根據(jù)配置內(nèi)容的重要程度有所區(qū)分。另外,如果表達式是以通配符開始的,那么需要使用引號將配置內(nèi)容引起來。

注意

  • 這種方式的配置肯定是不好的,造成管理的混亂,spring.cloud.config.server.git.uri配置是默認的回退配置,如果其他條件都不符合或者是配置的屬性在其匹配的模式中并沒有得到,那么就會去spring.cloud.config.server.git.uri去匹配。
  • 本列中的repos后面的dev,test,prod都是對應(yīng)著application應(yīng)用名稱。
  • 上面的pattern配置如果在yml中配置的話,那么還有另外一種寫法,可以看官網(wǎng),使用-這邊就不展示了。

參考資料
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:

默認情況下,首次請求配置時,服務(wù)器克隆遠程存儲庫。 服務(wù)器可以配置為在啟動時克隆存儲庫。 例如在頂層:

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.

在此示例中,服務(wù)器在啟動之前克隆了team-a的config-repo,然后它接受任何請求。 所有其他存儲庫將不被克隆,直到請求時才發(fā)起克隆的操作。

cloneOnStart也可以配置在repos下,這個時候就會將下面的所有應(yīng)用的配置文件在服務(wù)啟動的時候克隆到本地。

好處

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.

在配置服務(wù)器啟動時設(shè)置要克隆的存儲庫可以幫助在配置服務(wù)器啟動時快速識別配置錯誤的配置源(例如,無效的存儲庫URI)。 使用cloneOnStart未啟用配置源時,配置服務(wù)器可能啟動成功配置錯誤或無效的配置源,并且不會檢測到錯誤,直到應(yīng)用程序從該配置源請求配置時才發(fā)現(xiàn)配置錯誤。

參考資料
cloneOnStart
Placeholders in Git Search Paths

子目錄存儲

除了支持占位符配置,多倉庫配置之外,config server還可以將配置文件定位到git倉庫的子目錄中。我們在快速入門中的我們除了配置spring.cloud.config.server.git.uri之外海配置了另外一個參數(shù):spring.cloud.config.server.git.search-paths,通過這個參數(shù)可以實現(xiàn)在http://git.oschina.net/zhihaomiao/config-repo-demo倉庫的config-repo子目錄下實現(xiàn)配置的存儲。

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

通過上面的配置,我們可以實現(xiàn)http://git.oschina.net/zhihaomiao/config-repo-demo倉庫下,一個應(yīng)用一個目錄的效果。

對于spring.cloud.config.server.git.search-paths參數(shù)的配置也支持使用{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

這種方式也可以一個服務(wù)一個目錄,這樣就可以在一個倉庫中管理多個服務(wù)的配置,這種方式也比較好。

參考資料
Pattern Matching and Multiple Repositories
Placeholders in Git Search Paths

訪問權(quán)限

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倉庫中進行配置匹配以實現(xiàn)訪問。

svn配置倉庫

config server除了支持git倉庫之外,也能使用svn倉庫,只需要如下配置。

  • 在pom.xml中引入svn的依賴配置,讓config server擁有讀取svn內(nèi)容的能力:
  • application.properties中使用svn的配置屬性來指定svn服務(wù)器的位置,以及訪問的賬戶名與密碼:
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的本地文件系統(tǒng)中存儲一份,這些文件默認會被存儲以config-repo為前綴的臨時目錄中,比如/tmp/config-repo-<隨機數(shù)>的目錄。由于其隨機性以及臨時目錄的特性,可能會有一些不可預(yù)知的后果,為了避免將來可能會出現(xiàn)的問題,最好的方法就是指定一個固定的位置來存儲這些重要信息。我們只需要通過spring.cloud.config.server.git.basedirspring.cloud.config.server.svn.basedir來配置一個我們準備好的目錄即可。

本地文件系統(tǒng)

spring cloud config也提供了一種不適用git倉庫或svn倉庫的存儲方式,而是使用本地文件系統(tǒng)的存儲方式來保存配置信息。實現(xiàn)方式也簡單,只需要配置屬性spring.profile.active=native,config server會默認從應(yīng)用的src/main/resource目錄下搜索配置文件。如果需要指定搜索配置文件的路徑,我們可以通過spring.cloud.config.server.native.searchLocations屬性來指定具體的配置文件位置。

雖然spring cloud config提供了這樣的功能,但是為了支持更好的內(nèi)容管理和版本控制等強大功能,還是推薦使用git倉庫的方式

本博客代碼
代碼地址
配置倉庫
user服務(wù)配置倉庫
order服務(wù)配置倉庫

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容