> For the complete documentation index, see [llms.txt](https://potoyang.gitbook.io/spring-in-action-v5/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://potoyang.gitbook.io/spring-in-action-v5/di-14-zhang-pei-zhi-guan-li/14.2-yun-hang-pei-zhi-fu-wu-qi/14.2.2-tian-xie-pei-zhi-ku.md).

# 14.2.2 填写配置库

Config Server 所提供服务的属性资源，有几种方法可以进行设置。最基本、简单的选择是，提交 `application.properties` 或 `application.yml`到 Git 存储库的根路径。

假设推送了一个名为 `application.yml` 的文件到 Git 存储库中。这个配置文件与您在上一节中的配置不同，它是将由 Config Server 来提供配置服务的。假设在 `application.yml` 中您配置了以下属性：

```yaml
server:
  port: 0
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:8761/eureka/
```

虽然 `application.yml` 中的属性不多，但它所做的配置是相当重要的。它告诉应用程序中的每个服务两个重要信息：一个是 Eureka 的注册地址，另一个是随机选择可用端口。这意味着，当调整在第 14.3 节中开发的服务以适配使用 Config Server 时，您可以在服务中删除 Eureka 的配置了。

作为 Config Server 的客户机，可以在命令行中使用 curl 查看 Config Server 提供的新配置数据：

```bash
$ curl localhost:8888/someapp/someconfig
{
  "name": "someapp",
  "profiles": [
    "someconfig"
  ],
  "label": null,
  "version": "95df0cbc3bca106199bd804b27a1de7c3ef5c35e",
  "state": null,
  "propertySources": [
    {
      "name": "http://localhost:10080/habuma/tacocloudconfig/application.yml",
      "source": {
        "server.port": 0,
        "eureka.client.service-url.defaultZone":"http://eureka1:8761/eureka/"
      }
    }
  ]
}
```

与您之前对 Config Server 请求时的响应数据不同，此响应在属性 `propertySources` 数组中包含一些内容。具体来说，它包含一个 `name` 属性，指明引用的 Git 存储库。而 `source` 属性中，包含您推入 Git 存储库的那些具体属性。

## 在 GIT 子路径存储配置

依据您组织的实际情况，您可以选择将配置存储在 Git 存储库子路径而不是根路径中。例如，假设您要将配置存储在名为“config”的子目录中，您需对 `spring.cloud.config.server.git.search-paths` 属性进行设置，以告诉 Config Server 从 `/config` 而不是从根目录获取配置：

```yaml
spring:
  cloud:
    config:
      server:
        git:
          uri: http://localhost:10080/tacocloud/tacocloud-config
          search-paths: config
```

请注意 `spring.cloud.config.server.git.search-paths` 可设置多个值。您可以设置用逗号分隔的多个路径，以便让 Config Server 从多个路径中获取属性配置：

```yaml
spring:
  cloud:
    config:
      server:
        git:
          uri: http://localhost:10080/tacocloud/tacocloud-config
          search-paths: config,moreConfig
```

这将设置 Config Server 从 `/config` 和 `/moreConfig` 中获取配置。

指定搜索路径时，也可以使用通配符：

```yaml
spring:
  cloud:
    config:
      server:
        git:
          uri: http://localhost:10080/tacocloud/tacocloud-config
          search-paths: config,more*
```

在这里，Config Server 将从 `/config` 以及任何名称以 `more` 开头的子目录中获取配置。

## 从分支或标签获取配置

默认情况下，Config Server 从 Git 的 `master` 分支获取配置。从客户机上发送请求时，可以指定特定分支或标签，如图 14.2 那样。但您可能会发现，修改 Config Server 的默认行为，改为从某个特定 Git 标签或分支获取配置，可能会很有用。这可以在 `spring.cloud.config.server.git.default-label` 属性中指定具体分支或标签，以覆盖默认值。

例如，以下设置将 Config Server 设为从“sidework”的分支（或标签）中获取配置：

```yaml
spring:
  cloud:
    config:
      server:
        git:
          uri: http://localhost:10080/tacocloud/tacocloud-config
          default-label: sidework
```

按照上面的设置，配置将从“sidework”分支获取，除非客户端在请求中指定了其他分支。

## GIT 存储库的授权校验

Config Server 使用的 Git 存储库，很可能需要使用用户名和密码进行权限验证。那样的话，您就需要为 Config Server 指定 Git 存储库的身份凭据信息。

属性 `spring.cloud.config.server.username` 以及 `spring.cloud.config.server.password` 可以设置 Git 存储库的用户名和密码。如下配置展示了如何设置这些属性：

```yaml
spring:
  cloud:
    config:
      server:
        git:
          uri: http://localhost:10080/tacocloud/tacocloud-config
          username: tacocloud
          password: s3cr3tP455w0rd
```

这将用户名和密码分别设置为 `tacocloud` 和 `s3cr3tP455w0rd` 。

使用 curl 发送相关请求以伪装成客户端，可以帮助您了解 Config Server 的详细信息。 Config Server 还可以做很多事情。但是微服务不会使用 curl 来获取配置数据。在我们进一步了解 Config Server 更多的提供配置的方式之前，让我们来看一下，微服务是如何使用 Config Server 的服务的。
