一、Config Server的Git仓库配置

1.1、占位符支持

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

server:  port: 5020spring:  application:    name: config-server  cloud:    config:      server:        git:          uri: https://git.oschina.net/wadjz/{application}          username: 用户名          password: 密码

1.2、模式匹配

模式匹配指的是带有通配符的{application}/{profile}名称的列表。如果{application}/{profile}不匹配任何模式,它将会使用spring.cloud.config.server.git.uri定义的URI

server:  port: 5020spring:  application:    name: config-server  cloud:    config:      server:        git:          uri: https://git.oschina.net/wadjz/spring-cloud-config.git        repos:           simple: https://git.oschina.net/simple/spring-cloud-config          special:             pattern: special*/dev*,*special*/dev*            uri: https://git.oschina.net/special/spring-cloud-config          loca:            pattern: loca*            uri: file:/home/conf/spring-cloud-config

上面代码说明,对于simple仓库,它只匹配所有配置文件中名为simple的应用程序。loca仓库则匹配所有配置文件中以loca开头的所有应用程序的名称。

1.3、搜索目录

很多场景下,可能把配置文件放在了Git仓库的子目录中,此时可以使用search-path指定,search-path同样支持占位符。

server:  port: 5020spring:  application:    name: config-server  cloud:    config:      server:        git:          uri: https://git.oschina.net/wadjz/spring-cloud-config.git          search-paths: foo,bar*

这样,Config Server就会在Git仓库根目录、foo子目录、以及所有以bar开始的子目录中查找配置文件。

1.4、启动时加载配置文件

默认情况下,在配置被首次请求时,Config Server才会clone Git仓库。也可以让Config Server启动时clone指定Git仓库。如下:

server:  port: 5020spring:  application:    name: config-server  cloud:    config:      server:        git:          uri: https://git.oschina.net/wadjz/spring-cloud-config.git          clone-on-start: true

将属性spring.cloud.config.server.git.clone-on-start设为true进行全局配置。

注意:配置clone-on-start=true可帮助Config Server启动时快速识别错误的配置源(如:无效的Git仓库)

提示:如何打印Config Server请求Git仓库的细节?

设定日志级别:debug

二、Config Server的健康状况指示器

Config Server自带了一个健康状态指示器,用于检查所配置的EnvironmentRepository是否正常工作。可使用Config Server的/health端点查询当前健康状态。

默认情况下,健康指示器向EnvironmentRepository请求的{application}的app,{profile}和{lable}是对应EnvironmentRepository实现的默认值。对于Git,{prpfile}是default,{lable}是master。同样也可以自定义健康状况指示器的配置,从而检查更多的{application}、自定义的{profile}以及自定义的{lable},如:

server:  port: 5020#端点的配置endpoints:  sensitive: true  shutdown:    enabled: truemanagement:  security:    enabled: falsespring:  application:    name: config-server  cloud:    config:      server:        git:          uri: https://git.oschina.net/wadjz/spring-cloud-config.git          username:           password:           clone-on-start: true        health:          repositories:            a-foo:               label: v2.0              profiles: dev              name: spring-cloud-demo

  

如需禁用健康状况指示器,可设置spring.cloud.config.server.health=false