Spring Cloud之Finchley版学习(二十)-Spring Cloud Config-Git仓库配置详解

栏目: 编程工具 · 发布时间: 6年前

内容简介:在Config Server的占位符支持{application}、{profile}和{label}。示例:

Spring Cloud之Finchley版学习(十九)-配置中心-Spring Cloud Config 一节中,已实现使用Git仓库作为Config Server的后端存储,本节详细探讨如何配置Git仓库。

一、占位符支持

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

示例:

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。

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开头的所有应用程序的名称。

三、搜索目录

很多场景下,我们可能把配置文件放在了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开始的子目录中查找配置文件。

四、启动时加载配置文件

默认情况下,在配置被首次请求时,Config Server才会clone Git仓库。我们也可让Config Server在启动时就clone Git仓库,例如。

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仓库。

当然,也可使用 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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C语言编程:一本全面的C语言入门教程(第三版)

C语言编程:一本全面的C语言入门教程(第三版)

(美)Stephen Kochan / 张小潘 / 电子社博文视点资讯有限公司 / 2006年 / 59.00元

本书是极负盛名的C语言入门经典教材,其第一版发行至今已有20年的历史。本书内容详实全面,由浅入深,示例丰富,并在每个章节后面附有部分习题,非常适合读者自学使用。除此之外,《C语言编程》一书对于C语言标准的最新进展、C语言常见开发工具以及管理C语言大型项目等重要方面,也进行了深入浅出的说明。一起来看看 《C语言编程:一本全面的C语言入门教程(第三版)》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

SHA 加密
SHA 加密

SHA 加密工具

html转js在线工具
html转js在线工具

html转js在线工具