在跟我学Spring Cloud(Finchley版)-19-配置中心-Spring Cloud Config 一节中,已实现使用Git仓库做为Config Server的后端存储,本节详细探讨如何配置Git仓库。git
Config Server的占位符支持{application}、{profile}和{label}。github
示例:spring
server: port: 8080 spring: application: name: microservice-config-server cloud: config: server: git: uri: https://git.oschina.net/itmuch/{application} username: password:
使用这种方式,便可轻松支持一个应用对应一个Git仓库。同理,也可支持一个profile对应一个Git仓库。后端
模式匹配指的是带有通配符的{application}/{profile}名称的列表。若是{application}/{profile}不匹配任何模式,它将会使用spring.cloud.config.server.git.uri
定义的URI。app
spring: cloud: config: server: git: uri: https://github.com/spring-cloud-samples/config-repo repos: simple: https://github.com/simple/config-repo special: pattern: special*/dev*,*special*/dev* uri: https://github.com/special/config-repo local: pattern: local* uri: file:/home/configsvc/config-repo
该例中,对于simple仓库,它只匹配全部配置文件中名为simple的应用程序,它的模式等同于simple/*
。local仓库则匹配全部配置文件中以local开头的全部应用程序的名称。.net
不少场景下,咱们可能把配置文件放在了Git仓库子目录中,此时可使用search-paths指定,search-path一样支持占位符。日志
spring: cloud: config: server: git: uri: http://git.oschina.net/itmuch/spring-cloud-config-repo search-paths: foo,bar*
这样,Config Server就会在Git仓库根目录、foo子目录、以及全部以bar开始的子目录中查找配置文件。code
默认状况下,在配置被首次请求时,Config Server才会clone Git仓库。咱们也可以让Config Server在启动时就clone Git仓库,例如。server
spring: cloud: config: server: git: uri: https://github.com/spring-cloud-samples/config-repo repos: team-a: pattern: microservice-* clone-on-start: true uri: http://git.oschina.net/itmuch/spring-cloud-config-repo
将属性spring.cloud.config.server.git.repos.*.clone-on-start
设为true,便可让Config Server启动时clone指定Git仓库。ci
固然,也可以使用spring.cloud.config.server.git.clone-on-start = true
进行全局配置。
配置clone-on-start = true,可帮助Config Server启动时快速识别错误的配置源(例如无效的Git仓库)。
将如下包的日志级别设为DEBUG,便可打印Config Server请求Git仓库的细节。咱们可借助日志,更好地理解Config Server的Git仓库配置,同时,也便于咱们快速定位问题。
logging: level: org.springframework.cloud: DEBUG org.springframework.boot: DEBUG
http://www.itmuch.com/spring-cloud/finchley-20/