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

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

为何要使用配置中心

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

Spring Cloud Config简介

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

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

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

Spring Cloud Config架构图

TIPS服务器

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

编写Config Server

示例

  • 加依赖app

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
  • 加注解:@EnableConfigServer分布式

  • 写配置:微服务

    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仓库中的配置文件。测试

/{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

编码

  • 加依赖

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

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

    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.* 中的属性有更高的优先级,所以默认状况下它们不能被本地配置覆盖。

  • 写代码

    @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

本文首发

http://www.itmuch.com/spring-cloud/finchley-19/

干货分享

全是干货哦!

相关文章
相关标签/搜索