十二、统一管理微服务配置之Spring Cloud Config


为什么要统一管理微服务配置

对于Spring Boot单体应用,在启动时指定spring.profiles.active={profile}来加载不同环境下的配置。

在微服务架构中,这种方式未必适用,微服务架构对配置管理有着更高的要求,如:

  • 集中管理配置:一个使用微服务建构的应用系统 Kenneth 会包含成百上千个微服务,因此集中管理配置是非常有必要的。
  • 不同环境、不同配置:例如数据源配置在不同的环境(开发、测试、预发布、生产等)是不同的。
    运行期动态调整:例如可根据各个微服务的负载情况,动态调整数据源连接池大小或熔断阈值等,并且调整时不停止服务。
    配置修改后可自动更新:如噢诶之内容发生变化,微服务能够自动更新配置。

Sping Cloud Config简介

  • Spring Cloud Config为分布式系统外部化配置提供了服务端和客户端的支持,包含Config ServerConfig Client两部分。
  • Config Server是一个集中式、可扩展的配置服务器,它可以集中管理应用程序各个环境下的配置,默认使用 Git 存储配置内容。
  • Config ClientConfig Server的客户端,微服务在启动时,请求Config Server以获取配置内容,同时会缓存这些属性。
    config架构图

编写Config Server

  1. 创建 git 仓库,新建配置文件:
microservice.properties
microservice-dev.properties
microservice-test.properties
microservice-production.properties

内容分别是:

profile=default-1.0
profile=dev-1.0
profile=test-1.0
profile=production-1.0

创建config-label-v2.0分支,并将配置文件中的 1.0 改为 2.0。

  1. pom添加依赖:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
  1. 在启动类添加@EnableConfigServer注解。

4) 在application.yml中配置:

server:
  port: 1010
spring:
  application:
    name: microservice-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/qliang0816/springCloudTestConfigServer
          username:
          password:

可以通过Config Server端点获取配置文件内容,端点与配置文件映射关系如下:

/{application}/{profile}/[{label}]
/{label}/{application}-{profile}.properties
/{application}-{profile}.properties
/{label}/{application}-{profile}.yml
/{application}-{profile}.yml
  • {application}:微服务名称
  • {profile}:文件名对应的 dev,test,production 等后缀
  • {label}:对应 Git 仓库的分支,默认是 master

访问http://127.0.0.1:1010/microservice-foo/dev,获得如下结果:

{
  "name": "microservice-foo",
  "profiles": ["dev"],
  "label": null,
  "version": "4c756555be6e85bfdd15e89d19aee173ec548643",
  "state": null,
  "propertySources": [
    {
      "name": "https://github.com/qliang0816/springCloudTestConfigServer/microservice-foo-dev.properties",
      "source": {
        "profile": "dev-1.0-bus"
      }
    },
    {
      "name": "https://github.com/qliang0816/springCloudTestConfigServer/microservice-foo.properties",
      "source": {
        "profile": "default-1.0"
      }
    }
  ]
}

访问http://127.0.0.1:1010/microservice-foo-dev.properties,获得如下结果:

profile: dev-1.0

访问http://127.0.0.1:1010/config-label-v2.0/microservice-foo-dev.properties,获得如下结果:

profile: dev-2.0

编写Config Client

  1. pom添加依赖:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  1. 创建配置文件application.yml
server:
  port: 1011
  1. 创建配置文件bootstrap.yml
spring:
  application:
    #    对应的config server所获取的配置文件的{application}
    name: microservice-foo
  cloud:
    config:
      uri: http://localhost:1010/
      #      对应的config server所获取的配置文件的{profile}
      profile: dev
      #      指定Git仓库的分支,对应的config server所获取的配置文件的{label}
      label: master

Spring Cloud中有”引导上下文”的概念,这是主应用程序的父上下文。引导上下文负责从配置服务器加载配置属性,以及解密外部配置文件中的属性。和主应用程序加载application.*中的属性不同,引导上下文加载bootstrap.*中 的属性,配置在bootstrap.*中 属性具有更高的优先级,默认情况下不能被本地配置覆盖。

若需禁用引导过程,可设置spring.cloud.bootstrap.enabled=false

  1. 编写 Controller
@RestController
public class ConfigClientController {

    @Value("${profile}")
    private String profile;

    @GetMapping("/profile")
    public String hello() {
        return this.profile;
    }
}

4.2 访问http://127.0.0.1:1011/profile,返回如下结果:

dev-1.0

Config Server的 Git 仓库皮质详解

占位符支持

Config Server的占位符支持{application}{profile}{label}

spring:
  application:
    name: microservice-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/qliang0816/{application}
          username:
          password:

这种方式,可支持一个应用对应一个 Git 仓库。也可支持一个 profile 对应一个 Git 仓库。

模式匹配
spring:
  application:
    name: microservice-config-server
  cloud:
    config:
      server:
        git:
          #          如果{application}/{profile}不匹配任何模式,将使用此URI
          uri: https://github.com/spring-cloud-samples/config-repo
          repos:
            #            匹配所有配置文件中名为simple的应用程序
            simple: https://github.com/simple/config-repo
            special:
              pattern: special*/dev/*,*special*/dev*
              uri: https://github.com/special/config-repo
            #            匹配所有配置文件中以local开头的所有应用程序的名称
            local:
              pattern: local*
              uri: file:/home/configsvc/config-repo
搜索目录
spring:
  application:
    name: microservice-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/qliang0816/springCloudTestConfigServer
          #          foo子目录以及以bar开始的子目录
          search-paths: foo,bar*
启动时加载配置文件

默认情况下,首次请求配置时,Config Server克隆 Git 仓库,也可以通过 clone-on-start 设置Config Server启动时是否克隆 git 仓库:

spring:
  application:
    name: microservice-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/qliang0816/springCloudTestConfigServer
          repos:
            team-a:
              pattern: microservice-*
              clone-on-start: true
              uri: https://github.com/qliang0816/springCloudTestConfigServer2

全局配置:spring.cloud.config.server.git.clone-on-start: true


Config Server也可以对敏感数据进行加解密——官方地址


该文章摘自《Spring Cloud 与 Docker 微服务架构实战(第二版)》

以上


文章作者: Qliang
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Qliang !
评论
 上一篇
十三、统一管理微服务配置之Spring Cloud Bus自动刷新配置 十三、统一管理微服务配置之Spring Cloud Bus自动刷新配置
使用/refresh端点手动刷新配置给客户端的 Controller 添加@RefreshScope注解,即可用POST访问/refresh端点刷新配置。 使用Sping Cloud Bus自动刷新配置为什么使用Sping Cloud Bu
2019-01-29
下一篇 
十一、微服务网关之Zuul的Hystrix隔离策略和线程池 十一、微服务网关之Zuul的Hystrix隔离策略和线程池
Spring Cloud中,Zuul默认已经整合了Hystrix,而Hystrix有隔离策略——THREAD及SEMAPHORE。 隔离策略 默认情况下,Zuul的隔离策略是SEMAPHORE 。 可设置zuul.ribbonIsolati
2019-01-27
  目录