Fork me on GitHub

跟我学Spring Cloud(Finchley版)-19-配置中心-Spring Cloud Config

目录

经过前文讲解,至此,微服务架构已经日趋完善——现在已经可以做一个大型的应用了!然而,随着项目的迭代,微服务数目往往与日俱增,如何高效地管理配置成为我们必须解决的问题。本节来讨论如何使用Spring Cloud Config管理配置。

为什么要使用配置中心

  • 集中管理配置。一个使用微服务架构的应用系统可能会包含成百上千个微服务,因此集中管理配置是非常有必要的;
  • 不同环境,不同配置。例如,数据源配置在不同的环境(开发、测试、预发布、生产等)中是不同的;
  • 运行期间可动态调整。例如,我们可根据各个微服务的负载情况,动态调整数据源连接池大小或熔断阈值,并且在调整配置时不停止微服务;
  • 配置修改后可自动更新。如配置内容发生变化,微服务能够自动更新配置。

Spring Cloud Config简介

Spring Cloud Config为分布式系统外部化配置提供了服务器端和客户端的支持,它包括Config Server和Config Client两部分。由于Config Server和Config Client都实现了对Spring Environment和PropertySource抽象的映射,因此,Spring Cloud Config非常适合Spring应用程序,当然也可与任何其他语言编写的应用程序配合使用。

Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置内容(也可使用Subversion、MySQL、本地文件系统或Vault存储配置,本博客以Git为例进行讲解),因此可以很方便地实现对配置的版本控制与内容审计。

Config Client是Config Server的客户端,用于操作存储在Config Server中的配置属性。引入Spring Cloud Config后的架构如下:

Spring Cloud Config架构图

TIPS

Spring Cloud Config的GitHub:https://github.com/spring-cloud/spring-cloud-config

编写Config Server

示例

  • 加依赖

    1
    2
    3
    4
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
  • 加注解:@EnableConfigServer

  • 写配置:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    server:
    port: 8080
    spring:
    application:
    name: microservice-config-server
    cloud:
    config:
    server:
    git:
    # Git仓库地址
    uri: https://git.oschina.net/itmuch/spring-cloud-config-repo.git
    # Git仓库账号
    username:
    # Git仓库密码
    password:

    路径规则

Spring Cloud Config Server提供了RESTful API,可用来访问存放在Git仓库中的配置文件。

1
2
3
4
5
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

其中的{appliation}、{profile}、{label} 都是占位符。

TIPS

事实上,可使用Spring Cloud Config实现配置的“继承”与“组合”,举个例子——

假设有一个应用:microservice-foo ,其profile是dev,那么其实Spring Cloud Config会查找如下几个文件:

  • microservice-foo-dev.yml

  • microservice-foo.yml

  • application-dev.yml

  • application.yml

对于相同属性的配置,从上至下优先级逐渐递减;最终获得的配置属性是四个文件的组合。由此,不难分析,可如下规划几个配置文件:

  • microservice-foo-dev.yml 作为指定应用在指定profile下的配置文件
  • microservice-foo.yml 作为制定应用在任何profile下都通用的配置文件
  • application-dev.yml 作为所有应用在指定profile下的配置文件
  • application.yml 作为所有应用在任何profile下都通用的配置文件

测试

  • 访问http://localhost:8080/microservice-foo-dev.yml 可访问到Git仓库的microservice-foo-dev.properties 并组合application.properties

集成Config Client

编码

  • 加依赖

    1
    2
    3
    4
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
  • 加配置:applicaiton.yml

    1
    2
    server:
    port: 8081
  • 加配置:bootstrap.yml

    1
    2
    3
    4
    5
    6
    7
    8
    spring:
    application:
    name: microservice-foo # 对应config server所获取的配置文件的{application}
    cloud:
    config:
    uri: http://localhost:8080/
    profile: dev # profile对应config server所获取的配置文件中的{profile}
    label: master # 指定Git仓库的分支,对应config server所获取的配置文件的{label}

    其中:

    spring.application.name:对应Config Server所获取的配置文件中的{application} ;

    spring.cloud.config.uri:指定Config Server的地址,默认是http://localhost:8888

    spring.cloud.config.profile:profile对应Config Server所获取的配置文件中的{profile} ;

    spring.cloud.config.label:指定Git仓库的分支,对应Config Server所获取配置文件的{label}。

    值得注意的是,以上属性应配置在bootstrap.yml,而不是application.yml中。如果配置在application.yml中,该部分配置就不能正常工作。例如,Config Client会连接spring.cloud.config.uri的默认值 http://localhost:8888 ,而并非我们配置的 http://localhost:8080/

    Spring Cloud有一个“引导上下文”的概念,这是主应用程序上下文(Application Context)的父上下文。引导上下文负责从配置服务器加载配置属性,以及解密外部配置文件中的属性。和主应用程序加载application.* (yml或properties)中的属性不同,引导上下文加载bootstrap.* 中的属性。配置在bootstrap.* 中的属性有更高的优先级,因此默认情况下它们不能被本地配置覆盖。

  • 写代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    @RestController
    public class ConfigClientController {
    @Value("${profile}")
    private String profile;

    @GetMapping("/profile")
    public String hello() {
    return this.profile;
    }
    }

    测试

访问http://localhost:8081/profile 可返回Git仓库中的配置属性。

配套代码

Config Server

Config Client

相关文章

评论系统未开启,无法评论!