springCloud(18):統一管理微服務配置-git配置與Server的健康狀況指示器

一、Config Server的Git倉庫配置

1.1、佔位符支持

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

server:
  port: 5020
spring:
  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: 5020
spring:
  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: 5020
spring:
  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: 5020
spring:
  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

<logger name="org.springframework.cloud" level="debug"/>
<logger name="org.springframework.boot" level="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: true
management:
  security:
    enabled: false
spring:
  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

  wKiom1mmKcLCc63eAACwhIcnzjI390.jpg


如需禁用健康狀況指示器,可設置spring.cloud.config.server.health=false

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章