写于:2018-12-18 08:52:37

code.7z

一、Introduce

官方文档介绍

or

官方介绍

Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. With the Config Server you have a central place to manage external properties for applications across all environments.

粗略翻译就一句话:Spring Cloud Config 就是为分布式环境准备的,用来将所有的配置集中进行管理。

二、Features

2.1、Server Features

官方介绍

  • HTTP, resource-based API for external configuration (name-value pairs, or equivalent YAML content)

    (HTTP,为外部配置提供基于资源的API(键值对,或者等价的YAML内容)

  • Encrypt and decrypt property values (symmetric or asymmetric)

    (属性值的加密和解密(对称加密和非对称加密))

  • Embeddable easily in a Spring Boot application using @EnableConfigServer

    Spring boot 应用只需要 @EnableConfigServer 就能够实现功能)

2.2、Client Features

Config Client features (for Spring applications)(客户端特性)

  • Bind to the Config Server and initialize Spring Environment with remote property sources.

    绑定 Config Server,加载远程配置,进行 Spring Environment 的初始化

  • Encrypt and decrypt property values (symmetric or asymmetric).

    属性值的加密和解密(对称加密和非对称加密)

  • @RefreshScope for Spring @Beans that want to be re-initialized when configuration changes.

    使用 @RefreshScope,在配置变更的时候,重新初始化 Bean

  • Use management endpoints: 【通过 actuator 提供的相关web访问端点】

    • /env for updating Environment and rebinding @ConfigurationProperties and log levels.
    • /refreshfor refreshing the @RefreshScope beans.
    • /restart for restarting the Spring context (disabled by default).
    • /pause and /resume for calling the Lifecycle methods (stop() and start() on the ApplicationContext).
  • Bootstrap application context: a parent context for the main application that can be trained to do anything (by default, it binds to the Config Server and decrypts property values).

    Bootstrap :spring Cloud 上下文,也是主程序的父上下文,所有关于 Config Server 的配置默认配置在其中

三、Getting Started

案例:构建 config server 和 config client ,模拟配置拉取。 友链: 《配置文件加载优先级》

3.1、项目结构如下

01.png

3.2、服务端 config-server

引入依赖

  1. <!-- web -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <!-- config server 服务 -->
  7. <dependency>
  8. <groupId>org.springframework.cloud</groupId>
  9. <artifactId>spring-cloud-config-server</artifactId>
  10. </dependency>

Application.java 启动类追加注解

  1. @EnableConfigServer
  2. @SpringBootApplication
  3. public class Application {
  4. ......
  5. }

bootstrap.properties 配置如下

  1. # applicationname
  2. spring.application.name = config-server
  3. # port
  4. server.port = 28080
  5. # git config
  6. spring.cloud.config.server.git.uri = ${git仓库地址}
  7. spring.cloud.config.server.git.search-paths = /case-1/**
  8. spring.cloud.config.server.git.username = ${git仓库用户名}
  9. spring.cloud.config.server.git.password = ${git仓库密码}
  10. # 配置文件所在的分支
  11. spring.cloud.config.label = master

准备配置文件,并发到git仓库中

02.png

启动服务,并测试

直接通过 url 访问:http://{ip}:{port}/{配置文件前缀}/{环境}

如: [http://127.0.0.1:28080/config-client/dev](http://127.0.0.1:28080/config-client/dev)
03.png

3.3、客户端 config-client

引入依赖

  1. <!-- web -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <!-- spring cloud config client -->
  7. <dependency>
  8. <groupId>org.springframework.cloud</groupId>
  9. <artifactId>spring-cloud-config-server</artifactId>
  10. </dependency>

bootstrap.properties 配置如下

  1. # 对应git 中配置文件的前缀
  2. spring.cloud.config.name = config-client
  3. # 配置文件环境
  4. spring.cloud.config.profile = dev
  5. # 配置服务中心
  6. spring.cloud.config.uri = http://127.0.0.1:28080/

新增测试 API

  1. /** 获取服务名称:spring.application.name **/
  2. @RequestMapping("/get-name")
  3. public String getApplicationName(){
  4. return Optional.ofNullable(environment.getProperty("spring.application.name")).orElse("未知");
  5. }

启动客户端

更改配置中的 spring.cloud.config.profile,测试配置文件的变化。
[自行测试]