Git Backend

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

The default implementation of EnvironmentRepository uses a Git backend, which is very convenient for managing upgrades and physical environments and for auditing changes. To change the location of the repository, you can set the spring.cloud.config.server.git.uri configuration property in the Config Server (for example in application.yml). If you set it with a file: prefix, it should work from a local repository so that you can get started quickly and easily without a server. However, in that case, the server operates directly on the local repository without cloning it (it does not matter if it is not bare because the Config Server never makes changes to the "remote" repository). To scale the Config Server up and make it highly available, you need to have all instances of the server pointing to the same repository, so only a shared file system would work. Even in that case, it is better to use the ssh: protocol for a shared filesystem repository, so that the server can clone it and use a local working copy as a cache.

此存储库实现将 HTTP 资源的 {label} 参数映射到 git 标签(提交 ID、分支名称或标记)。如果 git 分支或标记名称包含斜杠 (/),则 HTTP URL 中的标签应改用特殊字符串 ({special-string}) 指定(以避免与其他 URL 路径混淆)。例如,如果标签是 foo/bar,替换斜杠将产生以下标签:foo({special-string})bar。特殊字符串 ({special-string}) 的包含也可以应用于 {application} 参数。如果你使用诸如 curl 之类的命令行客户端,请小心 URL 中的括号——你应该用单引号 (``) 从 shell 中转义它们。

This repository implementation maps the {label} parameter of the HTTP resource to a git label (commit id, branch name, or tag). If the git branch or tag name contains a slash (/), then the label in the HTTP URL should instead be specified with the special string ({special-string}) (to avoid ambiguity with other URL paths). For example, if the label is foo/bar, replacing the slash would result in the following label: foo({special-string})bar. The inclusion of the special string ({special-string}) can also be applied to the {application} parameter. If you use a command-line client such as curl, be careful with the brackets in the URL — you should escape them from the shell with single quotes ('').

Skipping SSL Certificate Validation

可以通过将 git.skipSslValidation 属性设置为 true(默认值为 false)来禁用配置服务器对 Git 服务器的 SSL 证书的验证。

The configuration server’s validation of the Git server’s SSL certificate can be disabled by setting the git.skipSslValidation property to true (default is false).

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

Setting HTTP Connection Timeout

你可以配置配置服务器获取 HTTP 连接所需的时间(以秒为单位)。使用 git.timeout 属性(默认值为 5)。

You can configure the time, in seconds, that the configuration server will wait to acquire an HTTP connection. Use the git.timeout property (default is 5).

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

Placeholders in Git URI

Spring Cloud Config Server 支持带有 {application}{profile} 占位符的 git 存储库 URL(如果你需要,还可以带有 {label},但请记住,标签无论如何都会应用为 git 标签)。因此,你可以通过使用类似以下的结构来支持“每个应用程序一个存储库”策略:

Spring Cloud Config Server supports a git repository URL with placeholders for the {application} and {profile} (and {label} if you need it, but remember that the label is applied as a git label anyway). So you can support a “one repository per application” policy by using a structure similar to the following:

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

你还可以通过使用类似的模式,但使用 {profile} 来支持“每个配置文件一个存储库”策略。

You can also support a “one repository per profile” policy by using a similar pattern but with {profile}.

此外,在你的 {application} 参数中使用特殊字符串“({special-string})”可以支持多个组织,如下例所示:

Additionally, using the special string "({special-string})" within your {application} parameters can enable support for multiple organizations, as shown in the following example:

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

其中,在请求时间以以下格式提供 {application}organization({special-string})application

where {application} is provided at request time in the following format: organization({special-string})application.

Pattern Matching and Multiple Repositories

Spring Cloud Config 还包括支持针对 application 和 profile 名称进行模式匹配的更复杂的需求。模式格式是一个以逗号分隔的 {application}/{profile} 名称与通配符列表(请注意,以通配符开头的模式可能需要引号),如下例所示:

Spring Cloud Config also includes support for more complex requirements with pattern matching on the application and profile name. The pattern format is a comma-separated list of {application}/{profile} names with wildcards (note that a pattern beginning with a wildcard may need to be quoted), as shown in the following example:

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/(它仅匹配所有配置文件中的一个名为 “simple” 的应用程序)。“local” 存储库匹配所有以 “local” 开头的应用程序名称,无论使用哪种配置文件(没有 profile 匹配器的任何模式都会自动添加后缀 /)。

If {application}/{profile} does not match any of the patterns, it uses the default URI defined under spring.cloud.config.server.git.uri. In the above example, for the “simple” repository, the pattern is simple/ (it only matches one application named simple in all profiles). The “local” repository matches all application names beginning with local in all profiles (the / suffix is added automatically to any pattern that does not have a profile matcher).

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

The “one-liner” short cut used in the “simple” example can be used only if the only property to be set is the URI. If you need to set anything else (credentials, pattern, and so on) you need to use the full form.

repo 中的 pattern`属性实际上是一个数组,因此可以使用 YAML 数组(或在属性文件中使用 `[0]、`[1]`等后缀)来绑定到多个模式。如果您要使用多个配置文件运行应用程序,则可能需要这样做,如下例所示:

The pattern property in the repo is actually an array, so you can use a YAML array (or [0], [1], etc. suffixes in properties files) to bind to multiple patterns. You may need to do so if you are going to run apps with multiple profiles, as shown in the following example:

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 猜测包含一个不会以 implies that you actually want to match a list of profiles starting with this pattern (so /staging 结尾的配置文件的模式是 ["/staging", "/staging,*"] 的一个快捷方式,依此类推)。这通常在需要在你本地的 “development” 配置文件中运行应用程序但也在远程运行 “cloud” 配置文件的情况下出现。

Spring Cloud guesses that a pattern containing a profile that does not end in implies that you actually want to match a list of profiles starting with this pattern (so /staging is a shortcut for ["/staging", "/staging,*"], and so on). This is common where, for instance, you need to run applications in the “development” profile locally but also the “cloud” profile remotely.

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

Every repository can also optionally store config files in sub-directories, and patterns to search for those directories can be specified as search-paths. The following example shows a config file at the top level:

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

在前面的示例中,服务器在顶层和 foo/ 子目录中搜索配置文件,以及名称以 bar 开头的任何子目录。

In the preceding example, the server searches for config files in the top level and in the foo/ sub-directory and also any sub-directory whose name begins with bar.

默认情况下,当首次请求配置时,服务器会克隆远程存储库。可以将服务器配置为在启动时克隆存储库,如下面的顶级示例所示:

By default, the server clones remote repositories when configuration is first requested. The server can be configured to clone the repositories at startup, as shown in the following top-level example:

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

在前面的示例中,服务器在 itaccepts 接受任何请求之前,在启动时克隆 team-a 的 config-repo。在请求存储库的配置之前,不会克隆所有其他存储库。

In the preceding example, the server clones team-a’s config-repo on startup, before it accepts any requests. All other repositories are not cloned until configuration from the repository is requested.

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

Setting a repository to be cloned when the Config Server starts up can help to identify a misconfigured configuration source (such as 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.

Authentication

要在远程存储库上使用 HTTP 基本认证,请分别添加 usernamepassword 属性(不在 URL 中),如下例所示:

To use HTTP basic authentication on the remote repository, add the username and password properties separately (not in the URL), as shown in the following example:

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

如果您不使用 HTTPS 和用户凭证,那么将密钥存储在默认目录(~/.ssh)中并且 URI 指向 SSH 位置(例如 ` git@github.com:configuration/cloud-configuration`)时,SSH 也应该开箱即用。Git 服务器的条目出现在 ~/.ssh/known_hosts`文件中并采用 `ssh-rsa`格式这一点很重要。不支持其他格式(例如 `ecdsa-sha2-nistp256)。为了避免造成意外,您应确保 known_hosts`文件中 Git 服务器仅存在一个条目,并且它与您提供给 Config Server 的 URL 匹配。如果您在 URL 中使用主机名,您希望 `known_hosts`文件中存在精确的主机名(而不是 IP)。存储库使用 JGit 进行访问,因此您应该可以应用在此方面找到的任何文档。可以在 `~/.git/config`或(以与任何其他 JVM 进程相同的方式)使用系统属性 (-Dhttps.proxyHost`和 -Dhttps.proxyPort)中设置 HTTPS 代理设置。

If you do not use HTTPS and user credentials, SSH should also work out of the box when you store keys in the default directories (~/.ssh) and the URI points to an SSH location, such as git@github.com:configuration/cloud-configuration. It is important that an entry for the Git server be present in the ~/.ssh/known_hosts file and that it is in ssh-rsa format. Other formats (such as ecdsa-sha2-nistp256) are not supported. To avoid surprises, you should ensure that only one entry is present in the known_hosts file for the Git server and that it matches the URL you provided to the config server. If you use a hostname in the URL, you want to have exactly that (not the IP) in the known_hosts file. The repository is accessed by using JGit, so any documentation you find on that should be applicable. HTTPS proxy settings can be set in ~/.git/config or (in the same way as for any other JVM process) with system properties (-Dhttps.proxyHost and -Dhttps.proxyPort).

如果您不知道您的 ~/.git 目录在哪里,请使用 git config --global 来操作设置(例如,git config --global http.sslVerify false)。

If you do not know where your ~/.git directory is, use git config --global to manipulate the settings (for example, git config --global http.sslVerify false).

JGit 需要 PEM 格式的 RSA 密钥。下面是一个 ssh-keygen(来自 openssh)命令示例,它将生成正确格式的密钥:

JGit requires RSA keys in PEM format. Below is an example ssh-keygen (from openssh) command that will generate a key in the corect format:

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

警告:使用 SSH 密钥时,预期的 ssh 私有密钥必须以 “-----BEGIN RSA PRIVATE KEY-----” 开头。如果密钥以 “-----BEGIN OPENSSH PRIVATE KEY-----” 开头,则在 spring-cloud-config 服务器启动时 RSA 密钥将无法加载。错误如下所示:

Warning: When working with SSH keys, the expected ssh private-key must begin with -----BEGIN RSA PRIVATE KEY-----. If the key starts with -----BEGIN OPENSSH PRIVATE KEY----- then the RSA key will not load when spring-cloud-config server is started. The error looks like:

- 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 的示例,用于生成采用合适格式的新密钥。

To correct the above error the RSA key must be converted to PEM format. An example using openssh is provided above for generating a new key in the appropriate format.

Authentication with AWS CodeCommit

Spring Cloud Config Server 还支持 AWS CodeCommit身份验证。从命令行使用 Git 时,AWS CodeCommit 使用身份验证帮助器。JGit 库不会使用此帮助器,因此如果 Git URI 匹配 AWS CodeCommit 模式,它会创建一个 JGit 凭证提供者用于 AWS CodeCommit。AWS CodeCommit URI 遵循此模式:

Spring Cloud Config Server also supports AWS CodeCommit authentication. AWS CodeCommit uses an authentication helper when using Git from the command line. This helper is not used with the JGit library, so a JGit CredentialProvider for AWS CodeCommit is created if the Git URI matches the AWS CodeCommit pattern. AWS CodeCommit URIs follow this pattern:

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

如果您提供一个用户名和密码与 AWS CodeCommit URI 搭配使用,它们必须是 AWS accessKeyId and secretAccessKey才能访问存储库。如果您不指定用户名和密码,accessKeyId 和 secretAccessKey 将使用 Default Credential Provider Chain检索。

If you provide a username and password with an AWS CodeCommit URI, they must be the AWS accessKeyId and secretAccessKey that provide access to the repository. If you do not specify a username and password, the accessKeyId and secretAccessKey are retrieved by using the Default Credential Provider Chain.

如果您的 Git URI 匹配 CodeCommit URI 模式(如前文所示),则必须在用户名和密码或默认凭证提供程序链支持的位置之一中提供有效的 AWS 凭证。AWS EC2 实例可能使用 IAM Roles for EC2 Instances

If your Git URI matches the CodeCommit URI pattern (shown earlier), you must provide valid AWS credentials in the username and password or in one of the locations supported by the default credential provider chain. AWS EC2 instances may use IAM Roles for EC2 Instances.

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

The software.amazon.awssdk:auth jar is an optional dependency. If the software.amazon.awssdk:auth jar is not on your classpath, the AWS Code Commit credential provider is not created, regardless of the git server URI.

Authentication with Google Cloud Source

Spring Cloud Config Server 还支持针对 Google Cloud Source存储库进行身份验证。

Spring Cloud Config Server also supports authenticating against Google Cloud Source repositories.

如果您的 Git URI 使用 httphttps 协议,且域名是 source.developers.google.com,则将使用 Google Cloud Source 凭据提供程序。Google Cloud Source 存储库 URI 采用格式 https://source.developers.google.com/p/${GCP_PROJECT}/r/${REPO}。要获取存储库的 URI,请在 Google Cloud Source UI 中单击 “克隆”,然后选择 “手动生成凭据”。不要生成任何凭据,只需复制显示的 URI 即可。

If your Git URI uses the http or https protocol and the domain name is source.developers.google.com, the Google Cloud Source credentials provider will be used. A Google Cloud Source repository URI has the format https://source.developers.google.com/p/${GCP_PROJECT}/r/${REPO}. To obtain the URI for your repository, click on "Clone" in the Google Cloud Source UI, and select "Manually generated credentials". Do not generate any credentials, simply copy the displayed URI.

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

The Google Cloud Source credentials provider will use Google Cloud Platform application default credentials. See Google Cloud SDK documentation on how to create application default credentials for a system. This approach will work for user accounts in dev environments and for service accounts in production environments.

com.google.auth:google-auth-library-oauth2-http 是一个可选的依赖项。如果 google-auth-library-oauth2-http jar 不在您的类路径中,则无论 git 服务器 URI 如何,都不会创建 Google Cloud Source 凭据提供程序。

com.google.auth:google-auth-library-oauth2-http is an optional dependency. If the google-auth-library-oauth2-http jar is not on your classpath, the Google Cloud Source credential provider is not created, regardless of the git server URI.

Git SSH configuration using properties

默认情况下,Spring Cloud Config Server 使用的 JGit 库在使用 SSH URI 连接到 Git 存储库时,使用 SSH 配置文件,例如 ~/.ssh/known_hosts/etc/ssh/ssh_config。在云环境(如 Cloud Foundry)中,本地文件系统可能是临时的或难以访问的。对于这些情况,可以通过使用 Java 属性来设置 SSH 配置。为了激活基于属性的 SSH 配置,spring.cloud.config.server.git.ignoreLocalSshSettings 属性必须设置为 true,如下面的示例中所示:

By default, the JGit library used by Spring Cloud Config Server uses SSH configuration files such as ~/.ssh/known_hosts and /etc/ssh/ssh_config when connecting to Git repositories by using an SSH URI. In cloud environments such as Cloud Foundry, the local filesystem may be ephemeral or not easily accessible. For those cases, SSH configuration can be set by using Java properties. In order to activate property-based SSH configuration, the spring.cloud.config.server.git.ignoreLocalSshSettings property must be set to true, as shown in the following example:

  spring:
    cloud:
      config:
        server:
          git:
            uri: git@gitserver.com: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 配置属性。

The following table describes the SSH configuration properties.

Table 1. SSH Configuration Properties
Property Name Remarks

ignoreLocalSshSettings

If true, use property-based instead of file-based SSH config. Must be set at as spring.cloud.config.server.git.ignoreLocalSshSettings, not inside a repository definition.

privateKey

Valid SSH private key. Must be set if ignoreLocalSshSettings is true and Git URI is SSH format.

hostKey

Valid SSH host key. Must be set if hostKeyAlgorithm is also set.

hostKeyAlgorithm

One of ssh-dss, ssh-rsa, ssh-ed25519, ecdsa-sha2-nistp256, ecdsa-sha2-nistp384, or ecdsa-sha2-nistp521. Must be set if hostKey is also set.

strictHostKeyChecking

true or false. If false, ignore errors with host key.

knownHostsFile

Location of custom .known_hosts file.

preferredAuthentications

Override server authentication method order. This should allow for evading login prompts if server has keyboard-interactive authentication before the publickey method.

Placeholders in Git Search Paths

Spring Cloud Config Server 还支持一个带有 {application}{profile}(如果你需要的话,还有 {label})占位符的搜索路径,如下例所示:

Spring Cloud Config Server also supports a search path with placeholders for the {application} and {profile} (and {label} if you need it), as shown in the following example:

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

前面的清单在存储库中搜索与目录同名的文件(以及顶级)。通配符在带有占位符的搜索路径中也是有效的(搜索中包括任何匹配的目录)。

The preceding listing causes a search of the repository for files in the same name as the directory (as well as the top level). Wildcards are also valid in a search path with placeholders (any matching directory is included in the search).

Force pull in Git Repositories

如前所述,如果本地副本变脏(例如,文件夹内容被操作系统进程更改)以至于 Spring Cloud Config Server 无法从远程存储库更新本地副本,则 Spring Cloud Config Server 会克隆远程 Git 存储库。

As mentioned earlier, Spring Cloud Config Server makes a clone of the remote git repository in case the local copy gets dirty (for example, folder content changes by an OS process) such that Spring Cloud Config Server cannot update the local copy from remote repository.

为了解决这个问题,有一个 force-pull 属性,如果本地副本变脏,则 Spring Cloud Config Server 会强制从远程存储库提取,如下例所示:

To solve this issue, there is a force-pull property that makes Spring Cloud Config Server force pull from the remote repository if the local copy is dirty, as shown in the following example:

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

如果你有多个存储库配置,你可以为每个存储库配置 force-pull 属性,如下例所示:

If you have a multiple-repositories configuration, you can configure the force-pull property per repository, as shown in the following example:

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-pull 属性的默认值为 false

The default value for force-pull property is false.

Deleting untracked branches in Git Repositories

由于 Spring Cloud Config Server 在将分支结账到本地仓库(例如通过标签获取属性)后克隆了远程 Git 存储库,它将永久保留此分支,或保留到下次服务器重新启动(创建新的本地仓库)。因此,会出现远程分支被删除但本地副本仍可用于获取的情况。如果 Spring Cloud Config Server 客户端服务以 --spring.cloud.config.label=deletedRemoteBranch,master 启动,它将从 deletedRemoteBranch 本地分支获取属性,而不是从 master

As Spring Cloud Config Server has a clone of the remote git repository after check-outing branch to local repo (e.g fetching properties by label) it will keep this branch forever or till the next server restart (which creates new local repo). So there could be a case when remote branch is deleted but local copy of it is still available for fetching. And if Spring Cloud Config Server client service starts with --spring.cloud.config.label=deletedRemoteBranch,master it will fetch properties from deletedRemoteBranch local branch, but not from master.

为了保持本地存储库分支干净并与远程分支保持一致,可以设置 deleteUntrackedBranches 属性。它将使 Spring Cloud Config Server 强制 从本地存储库中删除未跟踪的分支。示例:

In order to keep local repository branches clean and up to remote - deleteUntrackedBranches property could be set. It will make Spring Cloud Config Server force delete untracked branches from local repository. Example:

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

deleteUntrackedBranches 属性的默认值为 false

The default value for deleteUntrackedBranches property is false.

Git Refresh Rate

你可以使用 spring.cloud.config.server.git.refreshRate 控制配置服务器从 Git 后端获取更新的配置数据的时间间隔。此属性的值以秒为单位指定。默认值为 0,这意味着配置服务器将在每次请求时从 Git 仓库获取更新的配置。如果值为负数,则不会发生刷新。

You can control how often the config server will fetch updated configuration data from your Git backend by using spring.cloud.config.server.git.refreshRate. The value of this property is specified in seconds. By default the value is 0, meaning the config server will fetch updated configuration from the Git repo every time it is requested. If the value is a negative number the refresh will not occur.

Default Label

Git 使用的默认标签是“main”。如果你没有设置 spring.cloud.config.server.git.defaultLabel 并且名为 main 的分支不存在,则配置服务器默认还会尝试结账名为 master 的分支。如果你想禁用回退分支行为,你可以将 spring.cloud.config.server.git.tryMasterBranch 设为 false

The default label used for Git is main. If you do not set spring.cloud.config.server.git.defaultLabel and a branch named main does not exist, the config server will by default also try to checkout a branch named master. If you would like to disable to the fallback branch behavior you can set spring.cloud.config.server.git.tryMasterBranch to false.