1. 程式人生 > >《Spring Cloud Config官方文件》快速啟動

《Spring Cloud Config官方文件》快速啟動

原文連結

第二部分 Spring Cloud 配置

1.3.5.BUILD-SNAPSHOT

Spring Cloud 配置為分散式系統中的外部配置提供伺服器和客戶端支援。藉助Config Server,您可以在所有環境中管理應用程式的外部屬性。客戶端和伺服器上的概念與Spring Environment 和  PropertySource 抽象是一樣的,所以它們非常適合Spring應用程式,但可以與任何執行在任何語言的應用程式一起使用。當應用程式從開發到測試轉移到部署管道時,您可以管理這些環境之間的配置,並確保應用程式具有在遷移時所需執行的所有內容。伺服器儲存後端的預設實現使用git,因此它可以輕鬆地支援標記版本的配置環境,並且可以通過廣泛的工具來訪問管理內容。使用Spring配置新增替代實現並插入它們是很容易的。

4.快速啟動

啟動伺服器:

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

伺服器是一個Spring Boot應用程式,所以如果你喜歡(主類是 ConfigServerApplication ),你可以從你的IDE執行它 。然後嘗試一個客戶端:

$ 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倉庫(at  spring.cloud.config.server.git.uri )並使用它來初始化一個mini  SpringApplication 。迷你應用程式的 Environment  用於列舉屬性源並通過JSON節點發布它們。

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

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

其中在 SpringApplication中(即:在一個普通的Spring Boot的應用程式中是作為標準“應用”)“application”是作為 spring.config.name 來注入的,“profile”是一個活動的配置檔案(或逗號分隔的屬性列表),和“label”是一種可選的git標籤(預設為“master”)。

Spring Cloud Config Server從git倉庫(必須提供)中為遠端客戶端提取配置:

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

4.1客戶端使用

要在應用程式中使用這些功能,只需將其構建為依賴於 spring-cloud-config-client 的 Spring Boot應用程式(例如,請參閱 config-client 的測試用例或示例應用程式)。新增依賴項的最簡便的方法是通過Spring Boot啟動器 org.springframework.cloud:spring-cloud-starter-config 。 對於Maven使用者還有一個父pom和BOM(spring-cloud-starter-parent),還有一個針對 Gradle和Spring CLI使用者的Spring IO版本管理屬性檔案。示例Maven配置:

pom.xml中。

<parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>1.3.5.RELEASE</version>
       <relativePath /> <!-- lookup parent from repository -->
   </parent>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Brixton.RELEASE</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);
    }

}

執行時,它將從埠為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> / <檔名>”的屬性源包含屬性“foo”和屬性值“bar”,並且是最高優先順序)。

屬性源名稱中的URL是git倉庫中的地址而不是配置伺服器URL。