返回文章列表

文章

spring-cloud-config-server中是如何找到对应的配置文件

spring-cloud-config-server的使用和配置

部分内容超过 Notion API 单页读取上限,已尽力加载可访问内容。

目录
  1. Config Server 如何找到 Git 远端仓库里的具体配置文件?
  2. file://** 配置的含义:**
  3. ~/config-repo/order-service.yml
  4. 实例演示

spring-cloud-config-serverapplication.yml 如下,它展示了 Config Server 如何定位和加载配置文件的关键点。

server:
  port: 8888

spring:
  application:
    name: config-service
  cloud:
    config:
      server:
        git:
          uri: file://${user.home}/config-repo/
          default-label: master  # 指定默认分支
          search-paths:
            - '{application}' # 或者更通用的 '{application}/**' 如果你的配置文件可能在更深的子目录

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
    register-with-eureka: true
    fetch-registry: true

Config Server 如何找到 Git 远端仓库里的具体配置文件?#

Config Server 查找 Git 远端仓库里配置文件的过程可以概括为以下几步:

  1. 根据 spring.cloud.config.server.git.uri 定位 Git 仓库:
    • 在您的配置中,uri: file://${user.home}/config-repo/ 指定了一个 本地文件系统上的 Git 仓库
      • file:// 前缀表示这是一个本地文件路径。
      • ${user.home} 是一个 Spring Boot 占位符,它会解析为当前用户的主目录(例如在 Windows 上是 C:\Users\YourUsername,在 Linux/macOS 上是 /home/yourusername)。
      • 所以,Config Server 会去查找 C:\Users\YourUsername\config-repo/ (Windows) 或 /home/yourusername/config-repo/ (Linux/macOS) 这个目录,并将其视为一个 Git 仓库。
    • 如果是真正的远程 Git 仓库(如 GitHub、GitLab、Gitee 等),uri 会是这样的形式:
      • https://github.com/your-username/your-config-repo.git
      • git@github.com:your-username/your-config-repo.git (需要配置 SSH Key)
      • http://your-gitlab.com/your-config-repo.git
  2. 根据 default-label 确定分支:
    • default-label: master 告诉 Config Server,当没有指定特定分支时,它应该从 master 分支(或更现代的 main 分支,这取决于你的 Git 仓库设置)拉取配置。
    • 客户端请求配置时,可以通过 /{application}/{profile}[/{label}] 的形式指定 label(即分支)。如果未指定,则使用 default-label
  3. 克隆或拉取 Git 仓库:
    • 当 Config Server 启动时,或者当客户端请求配置时,它会尝试克隆(如果是第一次)或拉取(如果已经克隆过)uri 指定的 Git 仓库到 Config Server 内部的一个临时目录。
    • 它会切换到 default-label 指定的分支(或客户端请求的分支)。
  4. 根据客户端请求的 applicationprofile 组合查找文件:
    • Config Server 客户端(例如 producer-serviceconsumer-service)会在它们的 bootstrap.yml (或 bootstrap.properties) 中配置 Config Server 的地址,并指定自己的 spring.application.namespring.profiles.active
    • 例如,一个名为 producer-service 的应用,激活的 profile 为 dev,它会向 Config Server 发送请求,类似于 http://localhost:8888/producer-service/dev
    • Config Server 收到这个请求后,会在其本地克隆的 Git 仓库中按照以下命名约定查找配置文件(优先级从高到低):
      • {application}-{profile}.yml (或 .properties):例如 producer-service-dev.yml
      • {application}.yml (或 .properties):例如 producer-service.yml
      • application-{profile}.yml (或 .properties):例如 application-dev.yml
      • application.yml (或 .properties):例如 application.yml
  5. 可选的 search-paths
    • 在您的示例中没有配置 search-paths,但这是一个很常用的配置。
    • 如果您配置了 search-paths: config-folder/{application},那么 Config Server 不会直接在仓库根目录查找文件,而是在 config-folder/producer-service/ 这样的路径下查找。这对于组织大型配置仓库非常有用。
    • 例如,如果您的配置仓库结构是:

      您可以将 search-paths 设置为 / 或者 '{application}' (Spring Cloud Config 会自动填充)。

config-repo ├── config-service │ ├── config-service-dev.yml │ ├── config-service-test.yml │ └── config-service.yml ├── order-service.yml └── user-service └── user-service-dev.yml ```

file://** 配置的含义:**#

uri: file://${user.home}/config-repo/ 意味着您需要在您的用户主目录下手动创建一个名为 config-repo 的目录,并且这个目录需要被初始化为一个 Git 仓库。 操作步骤(以 Linux/macOS 为例,Windows 类似):

  1. 打开终端。
  2. 进入你的用户主目录:cd ~
  3. 创建配置仓库目录:mkdir config-repo
  4. 进入目录:cd config-repo
  5. 初始化 Git 仓库:git init
  6. 创建一个示例配置文件,例如 order-service.yml

~/config-repo/order-service.yml#

server: port: 8082

spring: datasource: url: jdbc:mysql://localhost:3306/order_db username: root password: root 7. 添加并提交到 Git:<br>`git init` 默认会创建 `master` 分支,与你的 `default-label: master` 配置相符。 bash git add . git commit -m "Initial config for order-service" ``` 这样,当你的 config-service 启动时,它就会从这个本地的 config-repo Git 仓库中获取配置。你的 order-service 客户端(在 application.yml 中配置 spring.config.import=optional:configserver:)就会去 config-server 拉取这个 producer-service.yml 配置。 总结来说: spring-cloud-config-server 通过 Git URI 定位 Git 仓库,然后根据分支和命名约定在仓库中查找匹配 applicationprofile 的配置文件。

实例演示#

这里有一个user-service的微服务应用,application.yml 的配置如下:

spring:
  application:
    name: user-service # 你的服务名称
  profiles:
    active: dev        # 激活的 profile

  config:
    import: optional:configserver:http://localhost:8888  # (推荐)

# 如果 user-service 还需要注册到 Eureka (推荐)
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka # Eureka Server 的地址
    register-with-eureka: true # 将 user-service 注册到 Eureka
    fetch-registry: true     # 从 Eureka 获取服务列表          # Git 分支(如果你用 Git 存配置),可以是 main 或 master 等

这个服务在启动阶段会去config-service中读取应用配置,本地的的配置文件路径为 C:\Users\username\config-repo\user-service\user-service-dev.yml,内容如下:

server:
  port: 8081

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/user_db
    username: root
    password: root

本地的目录结构如下:

config-repo
├── config-service
│   ├── config-service-dev.yml
│   ├── config-service-test.yml
│   └── config-service.yml
├── order-service.yml
└── user-service
    └── user-service-dev.yml