Spring Cloud Config
简介
Spring Cloud Config是Spring Cloud团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分。其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息、加密/解密信息等访问接口;而客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。Spring Cloud Config实现了对服务端和客户端中环境变量和属性配置的抽象映射,所以它除了适用于Spring构建的应用程序之外,也可以在任何其他语言运行的应用程序中使用。由于Spring Cloud Config实现的配置中心默认采用Git来存储配置信息,所以使用Spring Cloud Config构建的配置服务器,天然就支持对微服务应用配置信息的版本管理,并且可以通过Git客户端工具来方便的管理和访问配置内容。当然它也提供了对其他存储方式的支持,比如:SVN仓库、本地化文件系统。
在本文中,我们将学习如何构建一个基于Git存储的分布式配置中心,并对客户端进行改造,并让其能够从配置中心获取配置信息并绑定到代码中的整个过程。
快速入门 【Dalston版】
准备配置仓库
准备一个git仓库,可以在码云或Github上创建都可以。比如本文准备的仓库示例:https://github.com/13849141963/spring-config-file
假设我们读取配置中心的应用名为springcloud-config
,那么我们可以在git仓库中该项目的默认配置文件application-dev.properties:[开发环境]
configFile=configFile dev version 1
为了演示加载不同环境的配置,我们可以在git仓库中再创建两个环境的配置文件application-test.properties: [测试环境]
configFile=configFile test version 1
application-prod.properties:[生产环境]
configFile=configFile prod version 1
构建配置中心
通过Spring Cloud Config来构建一个分布式配置中心非常简单,只需要三步:
1、创建一个基础的Spring Boot工程,命名为:springcloud-config-server,并在pom.xml
中引入下面的依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、在application.properties
中添加配置服务的基本信息以及Git仓库的相关信息,例如:
spring.application.name=springcloud-config-server
server.port=8080
# 配置git仓库地址
spring.cloud.config.server.git.uri=https://github.com/13849141963/spring-config-file/
# 配置仓库的分支
spring.cloud.config.label=master
# 配置仓库路下的相对搜索位置.可以配置多个
spring.cloud.config.server.git.search-paths=respoitory
# 访问git仓库的用户名
spring.cloud.config.server.git.username=13849141963
# 访问git仓库的用户密码 如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写
spring.cloud.config.server.git.password=********
3、创建Spring Boot的程序主类,并添加@EnableConfigServer
注解,开启Spring Cloud Config的服务端功能。
@EnableConfigServer
@SpringBootApplication
public class SpringcloudConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudConfigServerApplication.class, args);
}
}
到这里,使用一个通过Spring Cloud Config实现,并使用Git管理配置内容的分布式配置中心就已经完成了。我们可以将该应用先启动起来,确保没有错误产生。完成了这些准备工作之后,我们就可以通过浏览器、POSTMAN或CURL等工具直接来访问到我们的配置内容了。访问配置信息的URL与配置文件的映射关系如下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
上面的url会映射{application}-{profile}.properties
对应的配置文件,其中{label}
对应Git上不同的分支,默认为master。我们可以尝试构造不同的url来访问不同的配置内容,比如,要访问master分支,config-client应用的dev环境,就可以访问这个url:http://10.0.45.103:8080/spring-config-file/prod,http://10.0.45.103:8080/spring-config-file/test,http://10.0.45.103:8080/spring-config-file/test,并获得如下返回:
{
"name":"spring-config-file",
"profiles":[
"test"
],
"label":master,
"version":null,
"state":null,
"propertySources":[
{
"name":"https://github.com/13849141963/spring-config-file/respoitory/application-test.properties",
"source":{
"config-file":"configFile test version 1"
}
},
{
"name":"https://github.com/13849141963/spring-config-file/respoitory/application-dev.properties",
"source":{
"config-file":"configFile dev version 1"
}
},
{
"name":"https://github.com/13849141963/spring-config-file/respoitory/application-prod.properties",
"source":{
"config-file":"configFile prod version 1"
}
}
]
}
我们可以看到该Json中返回了应用名:spring-config-file,环境名:dev,分支名:master,以及test环境,dev环境和prod环境的配置内容。
通过启动配置服务器springcloud-config-server的控制台中输出了下面的内容,配置服务器在从Git中获取配置信息后,会存储在springcloud-config-sever的文件系统中,实质上springcloud-confi-server是通过git clone命令将配置内容复制了一份在本地存储,然后读取这些内容并返回给微服务应用进行加载。
Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@110b791: startup date [Mon Nov 05 19:17:14 CST 2018]; root of context hierarchy
Adding property source: file:/C:/Users/ADMINI~1/AppData/Local/Temp/config-repo-4125257711968764934/respoitory/application-dev.properties
Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@110b791: startup date [Mon Nov 05 19:17:14 CST 2018]; root of context hierarchy
conf ig-server通过Git在本地仓库暂存,可以有效防止当Git仓库出现故障而引起无法加载配置信息的情况。我们可以通过断开网络,再次发起http://localhost:7001/didispace/prod/config-label-test请求,在控制台中可输出如下内容。可以看到,springcloud-config-server提不无法从远程获取该分支内容的报错信息:Could not pull remote for config-label-test,但是它依然会为该请求返回配置内容,这些内容源于之前访问时存于springcloud-config-server本地文件系统中的配置内容。
构建客户端
在完成了上述验证之后,确定配置服务中心已经正常运作,下面我们尝试如何在微服务应用中获取上述的配置信息。
1、创建一个Spring Boot应用,命名为springcloud-config-client
,并在pom.xml
中引入下述依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、创建bootstrap.yml
配置,来指定获取配置文件的config-server-git
位置,例如:
spring.application.name=springcloud-config-client
# 指明远程仓库的分支
spring.cloud.config.label=master
# dev开发环境配置文件 test测试环境 pro正式环境
spring.cloud.config.profile=dev
# 配置中心config-server的地址
spring.cloud.config.uri=http://10.0.45.103:8080/
server.port=7001
上述配置参数与Git中存储的配置文件中各个部分的对应关系如下:
- spring.application.name:对应配置文件规则中的
{application}
部分 - spring.cloud.config.profile:对应配置文件规则中的
{profile}
部分 - spring.cloud.config.label:对应配置文件规则中的
{label}
部分 - spring.cloud.config.uri:配置中心
config-server
的地址
这里需要格外注意:上面这些属性必须配置在bootstrap.properties中,这样config-server中的配置信息才能被正确加载。
3、创建 controller测试获取远程配置文件服务器中的数据
@RestController
public class TestGetConfigController {
@Value("${configFile}")
String configFile;
@RequestMapping(value = "/configFile")
public String getConfigFile(){
return configFile;
}
}
4、启动类
@SpringBootApplication
public class SpringcloudConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudConfigClientApplication.class, args);
}
}
在完成了上面你的代码编写之后,读者可以将springcloud-config-server、springcloud-config-client都启动起来,然后访问http://10.0.45.103:7001/getConfigFile ,我们可以看到该端点将会返回从git仓库中获取的配置信息:
configFile=configFile dev version 1
这就说明,springcloud-config-client从springcloud-config-server获取了config-file的属性,而springcloud-config-server是从git仓库读取的,如图:
代码示例
项目github地址:git@github.com:13849141963/spring-cloud.git
具体工程说明如下:
- 基于Git仓库的配置中心:spring-config-file github地址:git@github.com:13849141963/spring-config-file.git
- 配置中心客户端:springcloud-config-client
- 配置中心服务端:springcloud-config-server
基础架构:
在动手实践了上面关于Spring Cloud Config的基础入门内容之后,在这里我们深入理解一下它是如何运作起来的。下图所示的萣上一节我们所构建案例的基本结构其中,主要包含下面几个要素。
服务端:
- 远程Git仓库:用来存储配置文件的地方,上例中我们用来存储计划应用名为 didispace 的多环境配置文件! didispace-{profile} .properties.
- Config Server:这是我们上面构建的分布式配置中心,springcloud-config-server工程,在该工程中指定了所要连接的Git仓库位置以及账户、密码等连接信息。
- 本地Git仓库:在springcloud-config-server的义件系统中,每次客户端请求获取配罝信息时,springcloud-config-server从Git仓库中获取最新配置到本地,然后在本地Git仓库中读取并返回。 当远程仓库无法获取时,直接将本地内容返回。
- Service A、 Service B:具体的微服务应用,它们指定了 spriongcloud-config-server的地址,从而实现从外部化获取应用自己要只的配置信息。这些应用在启动的时候,会向springcloud-config-server请求获取配罝信息来进行加载
客户端: 应用从配置管理中获取配置信息遵从下面的执行流程:
1、应用后动时,根据 bootstrap. properties中配置的应用名{ application},坏境名{profile}、分支名{label},向ConfigServer请求获取配置信息
2、CmifigServer根据自己维护的Git仓库信息和客户端传递过来的配置定位信息去査找配置信息。
3、通过git clone命令将找到的配置信息下载到Config Server的文件系统中。
4、Config Server 创建 Spring 的 ApplicationContent 实例,并从Git 本地仓库中加载配置文件,最后将这些配置内容读取出来返回给客户端应用。
5、客户端应用在获得外部配置文件后加载到客户端的 ApplicationContext实例,该配置内容的优先级高于客户端Jar包内部的配置内容,所以在ja i包中重复的内容将不再被加载。
Config Server巧妙地通过git clone将配罝信息存于本地,起到了缓存的作用,即使当Git服务端无法访问的时候,依然可以取Config Server中的缓存内容进行使用。