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。    

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