欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!

程序员文章站 2023-10-27 09:32:04
本文导读: Spring Cloud Config 基本概念 Spring Cloud Config 客户端加载流程 Spring Cloud Config 基于消息总线配置 Spring Cloud Config 中的占位符 Spring Cloud Config 仓库最佳实践 Spring Cl ......

本文导读:

  • spring cloud config 基本概念
  • spring cloud config 客户端加载流程
  • spring cloud config 基于消息总线配置
  • spring cloud config 中的占位符
  • spring cloud config 仓库最佳实践
  • spring cloud config 健康检查问题剖析

本文主要介绍 spring cloud config 基本概念、实践过的配置及遇到的问题进行剖析。关于如何启动运行配置中心可以参考官方 demo。

本文使用的spring cloud 版本:edgware.sr3

spring cloud config 基本概念

spring cloud config 用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持。

  • 服务端:分布式配置中心,独立的微服务应用,用来连接配置仓库(git)并为客户端提供获取配置信息、加密/解密等访问接口。、
  • 客户端:微服务架构中各个微服务应用和基础设施,通过指定配置中心管理应用资源与业务相关的配置内容,启动时从配置中心获取和加载配置信息

scc作用:

实现了对服务端和客户端中环境变量和属性配置的抽象映射。

scc优势:

默认采用 git 存储配置信息,天然支持对配置信息的版本管理。

spring cloud config架构图:
Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!

基于消息总线的架构图:
Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!

如上图所示,架构图中的几个主要元素作用:
远程 git 仓库:

用来存储配置文件的地方。

config server:

分布式配置中心,微服务中指定了连接仓库的位置以及账号密码等信息。

本地 git 仓库:

在 config server 文件系统中,客户单每次请求获取配置信息时,config server 从 git 仓库获取最新配置到本地,然后在本地 git 仓库读取并返回。当远程仓库无法获取时,直接将本地仓库内容返回。

servera/b:

具体的微服务应用,他们指定了 config server 地址,从而实现外部化获取应用自己想要的配置信息。应用启动时会向 config server 发起请求获取配置信息进行加载。

消息中心:

上述第二个架构图是基于消息总线的方式,依赖的外部的 mq 组件,目前支持 kafka、rabbitmq。通过 config server 配置中心提供的 /bus/refresh endpoint 作为生产者发送消息,客户端接受到消息通过http接口形式从 config server 拉取配置。

服务注册中心:

可以将 config server 注册到服务注册中心上比如 eureka,然后客户端通过服务注册中心发现config server 服务列表,选择其中一台 config server 来完成健康检查以及获取远端配置信息。

spring cloud config 客户端加载流程

客户端应用从配置管理中获取配置执行流程:

1)应用启动时,根据 bootstrap.yml 中配置的应用名 {application}、环境名 {profile}、分支名 {label},向 config server 请求获取配置信息。

2)config server 根据自己维护的 git 仓库信息与客户端传过来的配置定位去查找配置信息。

3)通过 git clone 命令将找到的配置下载到 config server 的文件系统(本地git仓库)

4)config server 创建 spring 的 applicationcontext 实例,并从 git 本地仓库中加载配置文件,最后读取这些配置内容返回给客户端应用。

5)客户端应用在获取外部配置内容后加载到客户端的 applicationcontext 实例,该配置内容优先级高于客户端 jar 包内部的配置内容,所以在 jar 包中重复的内容将不再被加载。

spring cloud config 基于消息总线配置

config server 作为配置中心 pom.xml 引入:

<dependency>
    <groupid>org.springframework.cloud</groupid>
   <artifactid>spring-cloud-config-server</artifactid>
</dependency>
<dependency>
       <groupid>org.springframework.cloud</groupid>
        <artifactid>spring-cloud-starter-bus-kafka</artifactid>
</dependency>
<dependency>
        <groupid>org.springframework.boot</groupid>
       <artifactid>spring-boot-starter-security</artifactid>
</dependency>
<dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-actuator</artifactid>
</dependency>

config server 配置文件(yml格式):

server:
  port: ${config_server_port:8021}
spring:
  application:
    name: letv-mas-config
    # 配置了该项,访问/bus/refresh?destination=应用名:spring.application.index,如果未指定spring.application.index默认使用「应用名:server.port」
    index: ${config_server_ip:127.0.0.1}
  cloud:
    config:
      server:
        git:
          # 基于 http 协议的单仓库,每一个应用创建一个目录,每个目录下创建配置文件
          uri: ${git_uri:http://xxxx/config.git}
          search-paths: '{application}'
          # 配置的 git 仓库基于 http 协议的,必须配置用户名和密码
          username: ${git_username:config_server}
          password: ${git_password:config@123}
          # 本地仓库目录设定
          basedir: /letv/app/mas/config/repos
          # 本地仓库如果有脏数据,则会强制拉取(默认是false)
          force-pull: true
          # 配置中心启动后从 git 仓库下载,如果uri配置中使用了 {application} 作为仓库名,这里要使用默认值false,否则启动报错.
          clone-on-start: false

management:
  security:
    enabled: false

# 用户认证,客户端应用接入时加入安全认证配置
security:
  user:
    name: config
    password: config2018
  basic:
    enabled: true
# 基于消息总线的 mq 配置
spring:
  cloud:
    stream:
      kafka:
        binder:
          zk-nodes: ${zk_nodes:localhost:2181}
          brokers: ${kafka_brokers:localhost:9092}
          requiredacks: -1
          configuration:
            security:
              protocol: sasl_plaintext
            sasl:
              mechanism: plain
          jaas:
            loginmodule: org.apache.kafka.common.security.plain.plainloginmodule
            options:
              username: test
              password: test-secret
    # 开启跟踪事件消息(默认是false)
    bus:
      trace:
        enabled: true
      # 自定义 topic 主题
      destination: test.springcloud.config

config client 作为客户端 pom.xml 引入:

<dependency>
    <groupid>org.springframework.cloud</groupid>
    <artifactid>spring-cloud-starter-config</artifactid>
</dependency>
<dependency>
       <groupid>org.springframework.cloud</groupid>
        <artifactid>spring-cloud-starter-bus-kafka</artifactid>
</dependency>
<dependency>
        <groupid>org.springframework.boot</groupid>
       <artifactid>spring-boot-starter-security</artifactid>
</dependency>
<dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-actuator</artifactid>
</dependency>

config client 配置文件(yml格式):

spring:
  application:
    name: letv-mas-client
    index: ${client_server_ip:127.0.0.1}:${server.port}
  profiles:
    active: ${client_profile:default}
    #include: busdev,streamdev
  cloud:
    config:
      uri: ${config_server_domain:http://config.xxx.cn/}
      failfast: true #the client will halt with an exception
      enabled: true
      # boostrap.yml 配置优先于启动参数变量 spring.profiles.active
      profile: ${spring.profiles.active:${client_profile:default}}
      label: master
      # 访问配置中心,用户安全认证
      username: config
      password: config2018
      # 激活定时任务,当 git 版本发生变更后加载最新配置上下文
      watcher:
        enabled: true
security:
  user:
    name: config
    password: config2018

# 基于消息总线的 mq 配置( kakfa 队列),如果zipkin中也使用 kafka 队列,那么需要通过binder 形式配置做隔离,否则会互相影响,无法下发配置消息。
spring:
  cloud:
    stream:
      # 自定义开关
      enabled: true
      # 指定中间件
      default-binder: config-kafka
      binders:
        config-kafka:
          type: kafka
          environment:
            spring:
              cloud:
                stream:
                  kafka:
                    binder:
                      zknodes: ${zk_nodes:localhost:2181}
                      brokers: ${kafka_brokers:localhost:9092}
                      # 生产者确认,0、1、-1,默认为1。0为不确认,1为leader单确认,-1为同步副本确认。-1的情况下消息可靠性更高。
                      required-acks: -1
                      # 是否自动创建topic,默认为true。设为false的情况下,依赖手动配置broker相关topic>配置,如果topic不存在binder则无法启动。
                      auto-create-topics: true
                      configuration:
                        security:
                          protocol: sasl_plaintext
                        sasl:
                          mechanism: plain
                      jaas:
                        loginmodule: org.apache.kafka.common.security.plain.plainloginmodule
                        options:
                          username: test
                          password: test-secret
    bus:
      # 是否启用bus
      enabled: true
      # bus 使用的队列或 topic,kafka 中的 topic,rabbitmq 中的 queue
      destination: test.springcloud.config
      trace:
        # 是否启用 bus 事件跟踪,可以通过 /trace 页面查看
        enabled: true
      refresh:
        # 是否发送 refresh 事件,开启时支持基于 config 文件变更的动态配置
        enabled: true
      env:
        # 是否开启 env 事件,开启时支持直接动态配置相应环境变量,如 /bus/env?arg1=value1&arg2=value2
        enabled: true

spring cloud config 中的占位符

占位符的使用:

这里的 {application} 代表了应用名,当客户端向 config server 发起获取配置请求时,config server 会根据客户端的 spring.application.name 信息来填充 {application} 占位符以定位配置资源的存储位置。

注意:{label} 参数很特别,如果 git 分支和标签包含 “/”,那么 {label} 参数在 http 的 url 中应用使用 “(_)” 替代,以避免改变了 uri 含义,指向到其他 uri 资源。

为什么要有占位符?

当使用 git 作为配置中心来存储各个微服务应用的配置文件时,uri 中的占位符的使用可以帮助我们规划和实现通用的仓库配置。

spring cloud config 仓库最佳实践

本地仓库:
spring cloud 的 d、e 版本中默认存储到 /var/folders/ml/9rww8x69519fwqlwlt5jrx700000gq/t/config-repo-2486127823875015066目录下。

在 b 版本中,未实际测试过,存储到临时目录 /tmp/config-repo-随机数目录下。
为了避免一些不可预知的问题,我们设置一个固定的本地git仓库目录。

通过 spring.cloud.config.server.git.basedir=${user.home}/local-config-repo 如果${user.home}目录下发现local-config-repo不存在,在config server启动后会自动创建,并从git远程仓库下载配置存储到这个位置。

远程仓库实践:
单仓库目录:
每一个项目对应一个仓库
spring.cloud.config.server.git.uri=https://gitee.com/ldwds/{application}

多仓库目录:
同一个仓库下,每个项目一个目录
spring.cloud.config.server.git.uri=https://gitee.com/ldwds/config-repo-demo.git
spring.cloud.config.server.git.search-paths='{application}'

1)单仓库目录注意事项:

spring.cloud.config.server.git.uri=[https://gitee.com/ldwds/config-repo-demo/](https://gitee.com/ldwds/config-repo-demo/)  
spring.cloud.config.serversearch-paths:’{application}'

客户端应用启动前,在 config-repo-demo 仓库下创建子目录,子目录名称就是配置中指定的spring.application.name 应用名。
否则,工程中引用的属性找不到,会报如下错误:
caused by: java.lang.illegalargumentexception: could not resolve placeholder 'from' in value "${from}"

2)多仓库目录注意事项:
这种方式不能设置参数
spring.cloud.config.server.git.force-pull=truespring.cloud.config.server.git.clone-on-start=true 
否则启动会报错,也很好理解因为使用了 {applicatoin} 作为占位符,没有指明具体的仓库名,所以无法强制拉取远程仓库配置。

如果你设置了本地仓库目录比如 spring.cloud.config.server.git.basedir=/data/config-repos/local-config-repo config server 启动后会自动创建 /data/config-repos 目录,并创建 config-repo-随机数命名的仓库名录,这个仓库下的内容来自于健康检查的默认仓库app。

客户端应用启动后,会根据 {application} 应用名去查找该仓库,config server 从匹配 git 仓库并 clone 到 config-repo-随机数的目录下。

如果 config server 重启了,客户端应用通过 /bus/refresh 刷新配置,因为并没有缓存之前的仓库名,所以会自动创建一个 config-repo-随机数 的仓库目录并从 git clone 数据。
如果 config server 已有本地仓库,客户端重启或 /bus/refresh 刷新配置则 config server 不会重建新的仓库。

配置中心本地仓库执行原理:
本地仓库是否存在根据 basedir 目录下是否包含.git 隐藏文件。如果本地仓库不存在,则从远端仓库 clone 数据到本地;如果本地仓库存在,则从远程仓库 fetch 最新数据到本地;然后 checkout 到指定 label,从远端仓库 merge 数据,并获取当前 label 分支最新的head 版本,以及默认的应用名 app 作为环境信息返回。

spring cloud config健康检查问题剖析

健康检查 pom.xml 中引入:

<dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-actuator</artifactid>
</dependency>

添加上述依赖后,默认开启健康检查。如果不需要健康检查,可以通过 spring.cloud.config.server.health.enabled=false 参数设定关闭。

如果配置为: spring.cloud.config.server.git.uri=[https://gitee.com/ldwds/config-repo-demo.git](https://gitee.com/ldwds/config-repo-demo.git)
默认开启了健康检查,我开始认为默认检查应用名称为 app,profiles 为 default,label 为 null进行监控(源码中看到的)。

但是 git 配置仓库下并没有 app 应用,此时访问 /health,监控状态仍然是 up?

{
    "status": "up"
}

上述理解是错误的,原因分析如下:

这个主要跟配置中心指定的 git 仓库地址有关系,如果仓库地址指定的是 {application} ,检查监视器会将 {application} 替换为默认应用名 app 作为仓库地址,此时会在 {user.home} 目录下创建 config-repo-随机数作为 {application} 应用的本地仓库(如:/users/liudewei1228/config-repo-7949870192520306956)。

即使设置了 spring.config.server.git.basedir=${user.home}/local-config-repo/ 也不会被使用到。
为什么?因为 {application} 作为仓库,是个动态的,可能会有多个 {application} 项目仓库,所以不会使用 basedir 特定目录作为本地仓库。

如下参数设置健康检查的配置:

spring.cloud.config.server.health.repositories.config-repo-demo.name=应用名
spring.cloud.config.server.health.repositories.config-repo-demo.label=分支
spring.cloud.config.server.health.repositories.config-repo-demo.profiles=环境变量

找到环境信息即显示状态 up,此过程出现任何异常(如找不到仓库 nosuchrespositoryexception)就会显示 down 状态。

在uri中包含 {application} 作为仓库情况下,客户端应用在启用前需提前创建好spring.application.name=config-client应用名作为仓库,否则会导致无法启用。(因为 {application} 被认为是一个项目仓库,并不是一个目录)。

源码详见:configserverhealthindicator.java 的 dohealthcheck 方法。
配置正确仓库的 name、label、profiles,访问 /health 接口显示 sources,这个 sources 中的地址无法访问的,实际只是一个标识的作用。

访问/health结果:

{
        "status": "up",
        "repositories": [
                {
                        "sources": [
                                "https://gitee.com/ldwds/config-repo-demo/config-client/config-client.properties";            
            ],
                        "name": "config-client",
                        "profiles": [
                                "default"            
            ],
                        "label": "master"        
        }    
    ]
}

否则,找不到指定仓库的信息,只会显示如下信息:

{
        "status": "up",
        "repositories": [
                {
                        "name": "config-client",
                        "profiles": [
                                "default"            
            ],
                        "label": "master"        
        }    
    ]
}

最新 spring cloud config 改进了很多问题,大家可以结合官网进一步学习了解。

本文对 spring cloud config (spring cloud e 版本)的基本概念、基于消息总线的配置使用、仓库目录实践、健康检查的实践以及实践中遇到的问题进行了剖析,希望有使用到这个配置中心的朋友们有所帮助。

目前微服务架构中选型时,推荐使用国内开源的配置中心:apollo配置中心(携程开源)、nacos注册&配置中心(阿里巴巴开源)。

欢迎关注我的公众号,扫码关注,解锁更多精彩文章,与你一同成长~
Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!