Pattern Matching and Multiple Repositories

可以在 application and profile name中使用匹配模式来支持更加复杂的需求。格式为用通配符以逗号分隔的{application}\/{profile} names 集合,例子如下

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          repos:
            simple: https://github.com/simple/config-repo
            special:
              pattern: special*/dev*,*special*/dev*
              uri: https://github.com/special/config-repo
            local:
              pattern: local*
              uri: file:/home/configsvc/config-repo

如果{application}\/{profile} 没有匹配到任何模式,就会使用定义在"spring.cloud.config.server.git.uri"下的默认uri。在上面的例子中,对于"simple" 仓库,匹配模式为“simple\/*”(即:它仅匹配在所有profiles中名称为simple"的应用)。本地仓库"local"匹配在所有profiles中名称开头为"local"的应用(后缀为\/*被自动添加到所有模式中)

注意

在上面例子中的"simple",它的属性只能是URI,如果需要设置其它属性,需要使用全模式。

在repp中的属性本质上是一个数组,所以可以使用YAML数组(或者[0], [1])来绑定多模式。如果打算使用多属性来运行应用那么需要使用这种模式,例子如下。

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          repos:
            development:
              pattern:
                - */development
                - */staging
              uri: https://github.com/development/config-repo
            staging:
              pattern:
                - */qa
                - */production
              uri: https://github.com/staging/config-repo

注意

每个仓库有可能在子目录下存储配置文件,可以通过指定searchPaths来查找这些子目录。例:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          searchPaths: foo,bar*

在上面的例子中,服务会根目录,"foo\/" 子目录,以及以"bar"开头的子目录中的配置文件。

默认情况下当第一次请求配置的时候服务会克隆远程属性。当然也可以配置成在启动时克隆。例子(cloneOnStart属性)

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

在这个例子中,服务会在启动时而不是启动时克隆team-a 的配置仓库。其它的仓库直到请求时才会被克隆。

为了在远程仓库中使用基于HTTP的认证,可以分别添加"username" 和 "password"属性(不是在URL中),如:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          username: trolley
          password: strongpassword

如果不使用HTTPS和用户认证,当把keys(秘钥对)存储在默认目录下(~\/.ssh),并且uri指向了SSH地址,那么SSH是开箱即用的("[email protected]:configuration\/cloud-configuration")。使用JGit连接仓库,所以你能检索到的文档都是可用的。可以在 ~\/.git\/config 或者通过系统参数(-Dhttps.proxyHost and -Dhttps.proxyPort)配置HTTPS代理

如果不知道git目录在哪,可以使用git config --global 操作配置项(如:git config --global http.sslVerify false)

results matching ""

    No results matching ""