spring cloud 翻譯-4. Quick Start

Part II. Spring Cloud Config
Finchley.SR2
Spring Cloud Config爲分佈式系統中的外部化配置提供服務器端和客戶端支持。使用Config Server,您有一箇中心位置來管理跨所有環境的應用程序的外部屬性。客戶機和服務器上的概念與Spring環境和PropertySource抽象完全相同,因此它們非常適合Spring應用程序,但是可以與任何語言中運行的應用程序一起使用。隨着應用程序在部署管道中從開發到測試並進入生產,您可以管理這些環境之間的配置,並確保應用程序在遷移時具有它們需要運行的所有內容。服務器存儲後端的默認實現使用git,因此它很容易支持配置環境的標記版本,並且可以被用於管理內容的各種工具訪問。很容易添加替代的實現,並用Spring配置將它們插入。

4. 快速開始
快速開始使用Spring Cloud Config Server 的服務器和客戶端。
首先,啓動服務器,如下:

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

該服務是一個Spring Boot 應用程序,如果你願意,可以從你的IDE運行它(主類是 ConfigServerApplication)。
接着嘗試一下客戶端,如下:

    $ curl localhost:8888/foo/development
    {"name":"foo","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.迷你應用程序的環境用於枚舉屬性源並在JSON端點發布它們。    
HTTP服務具有以下形式的資源:

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

其中,application 作爲spring.config.name 被注入到SpringApplication(常規的Spring Boot 應用程序裏通常是application),概要文件是活動的概要文件(或逗號分隔的屬性列表),而是一個可選的git標籤(默認爲master)。
Spring Cloud Config 從git 倉庫(必須提供)中提取遠程客戶端的配置,如下面的例子所示:

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

4.1 客戶端使用
要在應用程序中使用這些特性,你可以建一個依賴sprin-cloud-config-client(例如,參考配置客戶端或示例應用程序的測試用例)的Spring Boot 應用程序。添加依賴最便捷的方式是使用Spring Boot starter org.springframework.cloud:spring-cloud-starter-config。Maven用戶還有一個父pom和BOM(spring-cloud-starter-parent),以及Gradle和Spring CLI用戶的Spring IO版本管理屬性文件。
以下示例顯示了典型的Maven配置:

   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>{spring-boot-docs-version}</version>
       <relativePath /> <!-- lookup parent from repository -->
   </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>{spring-cloud-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
               <plugin>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-maven-plugin</artifactId>
               </plugin>
        </plugins>
    </build>

   <!-- repositories also needed for snapshots and milestones -->

現在你可以創建一個標準的Spring Boot 應用程序,如以下的HTTP 服務器:

    @SpringBootApplication
    @RestController
    public class Application {

        @RequestMapping("/")
        public String home() {
            return "Hello World!";
        }

        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }

當這個HTTP服務器運行時,它從端口8888上的默認本地配置服務器(如果它正在運行)獲取外部配置。要修改啓動行爲,可以使用bootstrap.properties(類似於application.properties,但是對於應用程序上下文的引導階段)更改配置服務器的位置,如下例所示:

    spring.cloud.config.uri: http://myconfigserver.com


引導屬性在/env端點中顯示爲高優先級屬性源,如下面的示例所示。

    $ curl localhost:8080/env
    {
      "profiles":[],
      "configService:https://github.com/spring-cloud-samples/config-repo/bar.properties":{"foo":"bar"},
      "servletContextInitParams":{},
      "systemProperties":{...},
      ...
    }    

名爲configService:<遠程倉庫的URL>/<文件名>的屬性源包含具有bar值的foo屬性並且是最高優先級。
    【注意】屬性源名稱中的URL是Git倉庫,而不是配置服務器URL。    

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