1. 程式人生 > >Spring Cloud Config教程(四)快速開始

Spring Cloud Config教程(四)快速開始

sof 應用 highlight 屬性 技術分享 添加 插入 標簽 yam

Spring Cloud Config為分布式系統中的外部配置提供服務器和客戶端支持。使用Config Server,您可以在所有環境中管理應用程序的外部屬性。客戶端和服務器上的概念映射與Spring EnvironmentPropertySource抽象相同,因此它們與Spring應用程序非常契合,但可以與任何以任何語言運行的應用程序一起使用。隨著應用程序通過從開發人員到測試和生產的部署流程,您可以管理這些環境之間的配置,並確定應用程序具有遷移時需要運行的一切。服務器存儲後端的默認實現使用git,因此它輕松支持標簽版本的配置環境,以及可以訪問用於管理內容的各種工具。很容易添加替代實現,並使用Spring配置將其插入。

啟動服務器:

$ cd spring-cloud-config-server
$ ../mvnw spring-boot:run

該服務器是一個Spring Boot應用程序,所以您可以從IDE運行它,而不是喜歡(主類是ConfigServerApplication)。然後嘗試一個客戶端:

$ curl localhost:8888/foo/development
{"name":"development","label":"master","propertySources":[
  {"name":"https://github.com/scratches/config-repo/foo-development.properties","source":{"bar":"spam"}},
  {"name":"https://github.com/scratches/config-repo/foo.properties","source":{"foo":"bar"}}
]}

定位資源的默認策略是克隆一個git倉庫(在spring.cloud.config.server.git.uri),並使用它來初始化一個迷你SpringApplication。小應用程序的Environment用於枚舉屬性源並通過JSON端點發布。

HTTP服務具有以下格式的資源:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

其中“應用程序”作為SpringApplication中的spring.config.name註入(即常規的Spring Boot應用程序中通常是“應用程序”),“配置文件”是活動配置文件(或逗號分隔列表的屬性),“label”是可選的git標簽(默認為“master”)。

Spring Cloud Config服務器從git存儲庫(必須提供)為遠程客戶端提供配置:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo

技術分享圖片源碼來源


Spring Cloud Config教程(四)快速開始