此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Cloud Config 4.1.4spring-doc.cadn.net.cn

Git 后端

的默认实现EnvironmentRepository使用 Git 后端,这对于管理升级和物理环境以及审计更改非常方便。 要更改存储库的位置,您可以设置spring.cloud.config.server.git.uriconfiguration 属性(例如在application.yml). 如果使用file:前缀,它应该从本地存储库工作,以便您可以在没有服务器的情况下快速轻松地开始使用。但是,在这种情况下,服务器将直接在本地存储库上运行,而无需克隆它(如果它不是裸露的并不重要,因为 Config Server 永远不会更改“远程”存储库)。 要扩展 Config Server 并使其具有高可用性,您需要将服务器的所有实例指向同一个存储库,因此只有共享文件系统才能工作。 即使在这种情况下,最好使用ssh:协议,以便服务器可以克隆它并使用本地工作副本作为缓存。spring-doc.cadn.net.cn

此存储库实现映射了{label}参数添加到 git 标签(提交 ID、分支名称或标签)。 如果 git 分支或标签名称包含斜杠 (),则应使用特殊字符串指定 HTTP URL 中的标签/({special-string})(以避免与其他 URL 路径产生歧义)。 例如,如果标签为foo/bar,替换斜杠将导致以下标签:foo({special-string})bar. 包含特殊字符串({special-string})也可以应用于{application}参数。 如果您使用命令行客户端(如 curl),请小心 URL 中的括号 — 您应该使用单引号 ('') 从 shell 中转义它们。spring-doc.cadn.net.cn

跳过 SSL 证书验证

可以通过设置git.skipSslValidationproperty 设置为true(默认值为false).spring-doc.cadn.net.cn

spring:
  cloud:
    config:
      server:
        git:
          uri: https://example.com/my/repo
          skipSslValidation: true

设置连接超时

您可以配置配置服务器等待获取 HTTP 或 SSH 连接的时间(以秒为单位)。使用git.timeout属性(默认值为5).spring-doc.cadn.net.cn

spring:
  cloud:
    config:
      server:
        git:
          uri: https://example.com/my/repo
          timeout: 4

Git URI 中的占位符

Spring Cloud Config Server 支持带有占位符的 git 存储库 URL{application}{profile}(以及{label}如果你需要它,但请记住,标签无论如何都是作为 git 标签应用的)。 因此,您可以使用类似于以下的结构来支持“每个应用程序一个存储库”策略:spring-doc.cadn.net.cn

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/myorg/\{application}

您还可以使用类似的模式来支持 “每个配置文件一个存储库” 策略,但使用{profile}.spring-doc.cadn.net.cn

此外,在{application}参数可以启用对多个 organizations,如以下示例所示:spring-doc.cadn.net.cn

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/\{application}

哪里{application}在请求时按以下格式提供:organization({special-string})application.spring-doc.cadn.net.cn

模式匹配和多个存储库

Spring Cloud Config 还包括对模式的更复杂需求的支持 在 Application 和 Profile Name 上匹配。 模式格式是逗号分隔的{application}/{profile}带有通配符的名称(请注意,可能需要用引号开头的模式),如以下示例所示:spring-doc.cadn.net.cn

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. 在上面的示例中,对于 “simple” 存储库,模式为simple/*(它只匹配一个名为simple在所有配置文件中)。“local” 存储库匹配所有以local在所有配置文件中(后缀会自动添加到没有配置文件匹配器的任何模式)。/*spring-doc.cadn.net.cn

仅当要设置的唯一属性是 URI 时,才能使用“simple”示例中使用的“one-liner”快捷方式。 如果需要设置任何其他内容 (凭据、模式等),则需要使用完整表单。

patternproperty 实际上是一个数组,因此你可以使用 YAML 数组(或者[0],[1]等后缀)绑定到多个模式。 如果要运行具有多个配置文件的应用程序,则可能需要这样做,如以下示例所示:spring-doc.cadn.net.cn

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
Spring Cloud 猜测,包含不以 结尾的配置文件的模式意味着您实际上希望匹配以此模式开头的配置文件列表(因此**/staging["*/staging", "*/staging,*"]等)。 这很常见,例如,您需要在本地运行 “development” 配置文件中的应用程序,但也需要远程运行 “cloud” 配置文件中的应用程序。

每个仓库还可以选择将配置文件存储在子目录中,并且搜索这些目录的模式可以指定为search-paths. 以下示例显示了顶层的配置文件:spring-doc.cadn.net.cn

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

在前面的示例中,服务器在顶层和foo/子目录以及名称以bar.spring-doc.cadn.net.cn

默认情况下,服务器在配置 首先请求。 可以将服务器配置为在启动时克隆存储库,如以下顶级示例所示:spring-doc.cadn.net.cn

spring:
  cloud:
    config:
      server:
        git:
          uri: https://git/common/config-repo.git
          repos:
            team-a:
                pattern: team-a-*
                cloneOnStart: true
                uri: https://git/team-a/config-repo.git
            team-b:
                pattern: team-b-*
                cloneOnStart: false
                uri: https://git/team-b/config-repo.git
            team-c:
                pattern: team-c-*
                uri: https://git/team-a/config-repo.git

在前面的示例中,服务器在启动时克隆了 team-a 的 config-repo,然后再 接受任何请求。 在请求存储库中的配置之前,不会克隆所有其他存储库。spring-doc.cadn.net.cn

将存储库设置为在 Config Server 启动时克隆有助于在 Config Server 启动时快速识别配置错误的配置源(例如无效的存储库 URI)。 跟cloneOnStart未为配置源启用,则 Config Server 可能会使用配置错误或无效的配置源成功启动,并且在应用程序从该配置源请求配置之前不会检测到错误。

认证

要在远程存储库上使用 HTTP 基本身份验证,请在usernamepassword属性(不在 URL 中),如以下示例所示:spring-doc.cadn.net.cn

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

如果您不使用 HTTPS 和用户凭证,则当您将密钥存储在默认目录 (~/.ssh) 和 URI 指向 SSH 位置,例如[email protected]:configuration/cloud-configuration. Git 服务器的条目必须出现在~/.ssh/known_hosts文件,并且它位于ssh-rsa格式。 其他格式(如ecdsa-sha2-nistp256) 不受支持。 为避免意外,您应该确保known_hosts文件,并且它与你提供给配置服务器的 URL 匹配。 如果您在 URL 中使用主机名,则希望在known_hosts文件。 存储库是使用 JGit 访问的,因此您找到的任何文档都应该适用。 HTTPS 代理设置可以在~/.git/config或(与任何其他 JVM 进程相同)使用 系统属性 (-Dhttps.proxyHost-Dhttps.proxyPort).spring-doc.cadn.net.cn

如果您不知道您的位置~/.gitdirectory 是,请使用git config --global要作设置(例如,git config --global http.sslVerify false).

JGit 需要 PEM 格式的 RSA 密钥。下面是一个示例 ssh-keygen(来自 openssh)命令,它将生成 corect 格式的密钥:spring-doc.cadn.net.cn

ssh-keygen -m PEM -t rsa -b 4096 -f ~/config_server_deploy_key.rsa

警告:使用 SSH 密钥时,预期的 ssh 私钥必须以-----BEGIN RSA PRIVATE KEY-----.如果键以-----BEGIN OPENSSH PRIVATE KEY-----那么 RSA 密钥在 spring-cloud-config 服务器启动时不会加载。错误如下所示:spring-doc.cadn.net.cn

- Error in object 'spring.cloud.config.server.git': codes [PrivateKeyIsValid.spring.cloud.config.server.git,PrivateKeyIsValid]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spring.cloud.config.server.git.,]; arguments []; default message []]; default message [Property 'spring.cloud.config.server.git.privateKey' is not a valid private key]

要更正上述错误,必须将 RSA 密钥转换为 PEM 格式。上面提供了一个使用 openssh 的示例,用于以适当的格式生成新密钥。spring-doc.cadn.net.cn

使用 AWS CodeCommit 进行身份验证

Spring Cloud Config Server 还支持 AWS CodeCommit 身份验证。 AWS CodeCommit 在从命令行使用 Git 时使用身份验证帮助程序。 此帮助程序不与 JGit 库一起使用,因此,如果 Git URI 与 AWS CodeCommit 模式匹配,则会为 AWS CodeCommit 创建 JGit CredentialProvider。 AWS CodeCommit URI 遵循以下模式:spring-doc.cadn.net.cn

https://git-codecommit.${AWS_REGION}.amazonaws.com/v1/repos/${repo}

如果您提供带有 AWS CodeCommit URI 的用户名和密码,则它们必须是提供存储库访问权限的 AWS accessKeyId 和 secretAccessKey。 如果不指定用户名和密码,则使用 Default Credential Provider Chain 获取 accessKeyId 和 secretAccessKey。spring-doc.cadn.net.cn

如果您的 Git URI 与 CodeCommit URI 模式(如前所示)匹配,则必须在用户名和密码中提供有效的AWS凭证,或者在默认凭证提供程序链支持的位置之一提供。 AWS EC2 实例可以使用 EC2 实例的 IAM 角色spring-doc.cadn.net.cn

software.amazon.awssdk:authjar 是一个可选的依赖项。 如果software.amazon.awssdk:authjar 不在您的类路径上,则不会创建 AWS Code Commit 凭证提供程序,无论 git 服务器 URI 如何。

使用 Google Cloud Source 进行身份验证

Spring Cloud Config Server 还支持对 Google Cloud Source 存储库进行身份验证。spring-doc.cadn.net.cn

如果您的 Git URI 使用httphttps协议,域名为source.developers.google.com,则将使用 Google Cloud Source 凭据提供程序。Google Cloud Source 存储库 URI 的格式为source.developers.google.com/p/${GCP_PROJECT}/r/${REPO}.要获取存储库的 URI,请单击 Google Cloud Source UI 中的“Clone”,然后选择“Manually generated credentials”。不生成任何凭证,只需复制显示的 URI。spring-doc.cadn.net.cn

Google Cloud Source 凭据提供程序将使用 Google Cloud Platform 应用程序默认凭据。请参阅 Google Cloud SDK 文档,了解如何为系统创建应用程序默认凭证。此方法适用于开发环境中的用户帐户和生产环境中的服务帐户。spring-doc.cadn.net.cn

com.google.auth:google-auth-library-oauth2-http是可选的依赖项。 如果google-auth-library-oauth2-httpjar 不在您的 Classpath 上,则不会创建 Google Cloud Source 凭证提供程序,无论 git 服务器 URI 如何。

使用属性的 Git SSH 配置

默认情况下,Spring Cloud Config Server 使用的 JGit 库使用 SSH 配置文件,例如~/.ssh/known_hosts/etc/ssh/ssh_config使用 SSH URI 连接到 Git 存储库时。 在 Cloud Foundry 等云环境中,本地文件系统可能是短暂的或不容易访问的。 对于这些情况,可以使用 Java 属性设置 SSH 配置。 为了激活基于属性的 SSH 配置,spring.cloud.config.server.git.ignoreLocalSshSettingsproperty 必须设置为true,如以下示例所示:spring-doc.cadn.net.cn

  spring:
    cloud:
      config:
        server:
          git:
            uri: [email protected]:team/repo1.git
            ignoreLocalSshSettings: true
            hostKey: someHostKey
            hostKeyAlgorithm: ssh-rsa
            privateKey: |
                         -----BEGIN RSA PRIVATE KEY-----
                         MIIEpgIBAAKCAQEAx4UbaDzY5xjW6hc9jwN0mX33XpTDVW9WqHp5AKaRbtAC3DqX
                         IXFMPgw3K45jxRb93f8tv9vL3rD9CUG1Gv4FM+o7ds7FRES5RTjv2RT/JVNJCoqF
                         ol8+ngLqRZCyBtQN7zYByWMRirPGoDUqdPYrj2yq+ObBBNhg5N+hOwKjjpzdj2Ud
                         1l7R+wxIqmJo1IYyy16xS8WsjyQuyC0lL456qkd5BDZ0Ag8j2X9H9D5220Ln7s9i
                         oezTipXipS7p7Jekf3Ywx6abJwOmB0rX79dV4qiNcGgzATnG1PkXxqt76VhcGa0W
                         DDVHEEYGbSQ6hIGSh0I7BQun0aLRZojfE3gqHQIDAQABAoIBAQCZmGrk8BK6tXCd
                         fY6yTiKxFzwb38IQP0ojIUWNrq0+9Xt+NsypviLHkXfXXCKKU4zUHeIGVRq5MN9b
                         BO56/RrcQHHOoJdUWuOV2qMqJvPUtC0CpGkD+valhfD75MxoXU7s3FK7yjxy3rsG
                         EmfA6tHV8/4a5umo5TqSd2YTm5B19AhRqiuUVI1wTB41DjULUGiMYrnYrhzQlVvj
                         5MjnKTlYu3V8PoYDfv1GmxPPh6vlpafXEeEYN8VB97e5x3DGHjZ5UrurAmTLTdO8
                         +AahyoKsIY612TkkQthJlt7FJAwnCGMgY6podzzvzICLFmmTXYiZ/28I4BX/mOSe
                         pZVnfRixAoGBAO6Uiwt40/PKs53mCEWngslSCsh9oGAaLTf/XdvMns5VmuyyAyKG
                         ti8Ol5wqBMi4GIUzjbgUvSUt+IowIrG3f5tN85wpjQ1UGVcpTnl5Qo9xaS1PFScQ
                         xrtWZ9eNj2TsIAMp/svJsyGG3OibxfnuAIpSXNQiJPwRlW3irzpGgVx/AoGBANYW
                         dnhshUcEHMJi3aXwR12OTDnaLoanVGLwLnkqLSYUZA7ZegpKq90UAuBdcEfgdpyi
                         PhKpeaeIiAaNnFo8m9aoTKr+7I6/uMTlwrVnfrsVTZv3orxjwQV20YIBCVRKD1uX
                         VhE0ozPZxwwKSPAFocpyWpGHGreGF1AIYBE9UBtjAoGBAI8bfPgJpyFyMiGBjO6z
                         FwlJc/xlFqDusrcHL7abW5qq0L4v3R+FrJw3ZYufzLTVcKfdj6GelwJJO+8wBm+R
                         gTKYJItEhT48duLIfTDyIpHGVm9+I1MGhh5zKuCqIhxIYr9jHloBB7kRm0rPvYY4
                         VAykcNgyDvtAVODP+4m6JvhjAoGBALbtTqErKN47V0+JJpapLnF0KxGrqeGIjIRV
                         cYA6V4WYGr7NeIfesecfOC356PyhgPfpcVyEztwlvwTKb3RzIT1TZN8fH4YBr6Ee
                         KTbTjefRFhVUjQqnucAvfGi29f+9oE3Ei9f7wA+H35ocF6JvTYUsHNMIO/3gZ38N
                         CPjyCMa9AoGBAMhsITNe3QcbsXAbdUR00dDsIFVROzyFJ2m40i4KCRM35bC/BIBs
                         q0TY3we+ERB40U8Z2BvU61QuwaunJ2+uGadHo58VSVdggqAo0BSkH58innKKt96J
                         69pcVH/4rmLbXdcmNYGm6iu+MlPQk4BUZknHSmVHIFdJ0EPupVaQ8RHT
                         -----END RSA PRIVATE KEY-----

下表描述了 SSH 配置属性。spring-doc.cadn.net.cn

表 1.SSH 配置属性
属性名称 言论

ignoreLocalSshSettingsspring-doc.cadn.net.cn

如果true,请使用基于属性的 SSH 配置,而不是基于文件的 SSH 配置。必须设置为spring.cloud.config.server.git.ignoreLocalSshSettings,而不是在存储库定义中。spring-doc.cadn.net.cn

私有密钥spring-doc.cadn.net.cn

有效的 SSH 私钥。如果ignoreLocalSshSettings为 true,Git URI 为 SSH 格式。spring-doc.cadn.net.cn

hostKeyspring-doc.cadn.net.cn

有效的 SSH 主机密钥。如果hostKeyAlgorithmspring-doc.cadn.net.cn

hostKey算法spring-doc.cadn.net.cn

其中之一ssh-dss, ssh-rsa, ssh-ed25519, ecdsa-sha2-nistp256, ecdsa-sha2-nistp384, or ecdsa-sha2-nistp521.如果hostKeyspring-doc.cadn.net.cn

strictHostKeyCheckingspring-doc.cadn.net.cn

truefalse.如果为 false,则忽略 host key 的错误。spring-doc.cadn.net.cn

knownHostsFile 文件spring-doc.cadn.net.cn

自定义位置.known_hosts文件。spring-doc.cadn.net.cn

preferredAuthenticationsspring-doc.cadn.net.cn

覆盖服务器身份验证方法顺序。如果服务器在publickey方法。spring-doc.cadn.net.cn

Git 搜索路径中的占位符

Spring Cloud Config Server 还支持带有{application}{profile}(以及{label}如果 您需要它),如以下示例所示:spring-doc.cadn.net.cn

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          search-paths: '\{application}'

前面的清单会导致在存储库中搜索与目录同名(以及顶级)的文件。 通配符在带有占位符的搜索路径中也有效(搜索中包括任何匹配的目录)。spring-doc.cadn.net.cn

强制拉取 Git 存储库

如前所述, Spring Cloud Config Server 会克隆远程 git 存储库,以防本地副本变脏(例如, 文件夹内容更改),使得 Spring Cloud Config Server 无法从远程存储库更新本地副本。spring-doc.cadn.net.cn

为了解决这个问题,有一个force-pull如果本地副本是脏的,则 Spring Cloud Config Server 会强制从远程存储库中提取属性,如以下示例所示:spring-doc.cadn.net.cn

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          force-pull: true

如果您有多存储库配置,则可以配置force-pull属性,如以下示例所示:spring-doc.cadn.net.cn

spring:
  cloud:
    config:
      server:
        git:
          uri: https://git/common/config-repo.git
          force-pull: true
          repos:
            team-a:
                pattern: team-a-*
                uri: https://git/team-a/config-repo.git
                force-pull: true
            team-b:
                pattern: team-b-*
                uri: https://git/team-b/config-repo.git
                force-pull: true
            team-c:
                pattern: team-c-*
                uri: https://git/team-a/config-repo.git
的默认值force-pullproperty 为false.

删除 Git 存储库中未跟踪的分支

由于 Spring Cloud Config Server 具有远程 git 存储库的克隆 将分支签出到本地仓库后(例如通过标签获取属性),它将保留此分支 forever 或直到下一次服务器重启(这将创建新的本地存储库)。 因此,可能会出现远程分支被删除但其本地副本仍可用于获取的情况。 如果 Spring Cloud Config Server 客户端服务以--spring.cloud.config.label=deletedRemoteBranch,master它将从deletedRemoteBranchlocal 分支,而不是从master.spring-doc.cadn.net.cn

为了保持本地仓库分支的整洁和远程 -deleteUntrackedBranches属性。 它将使 Spring Cloud Config Server 强制从本地存储库中删除未跟踪的分支。 例:spring-doc.cadn.net.cn

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          deleteUntrackedBranches: true
的默认值deleteUntrackedBranchesproperty 为false.

Git 刷新率

您可以控制配置服务器获取更新的配置数据的频率 使用 Git 后端spring.cloud.config.server.git.refreshRate.这 此属性的值以秒为单位指定。默认情况下,该值为 0,这意味着 配置服务器每次都会从 Git 仓库中获取更新的配置 请求。如果该值为负数,则不会进行刷新。spring-doc.cadn.net.cn

默认标签

Git 使用的默认标签是main.如果未设置spring.cloud.config.server.git.defaultLabel以及一个名为main不存在,则配置服务器默认情况下还会尝试检出名为master.如果 您希望禁用您可以设置的 fallback 分支行为spring.cloud.config.server.git.tryMasterBranchfalse.spring-doc.cadn.net.cn

在容器中使用 Git 运行 Config Server

如果您要获取java.io.IOException在类似于以下内容的容器中运行 Config Server 时:spring-doc.cadn.net.cn

2022-01-03 20:04:02,892 [tributeWriter-2] ERROR org.eclipse.jgit.util.FS$FileStoreAttributes.saveToConfig - Cannot save config file 'FileBasedConfig[/.config/jgit/config]'
java.io.IOException: Creating directories for /.config/jgit failed
  1. 在容器内为用户提供可写主目录。spring-doc.cadn.net.cn

  2. 设置环境变量XDG_CONFIG_HOME以指向 Java 进程具有写入权限的目录。spring-doc.cadn.net.cn