Spring Cloud构建微服务架构:分布式配置中心【Dalston版】

开发 开发工具 分布式
在本文中,我们将学习如何构建一个基于Git存储的分布式配置中心,并对客户端进行改造,并让其能够从配置中心获取配置信息并绑定到代码中的整个过程。

Spring Cloud Config是Spring Cloud团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分。其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息、加密/解密信息等访问接口;而客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。Spring Cloud Config实现了对服务端和客户端中环境变量和属性配置的抽象映射,所以它除了适用于Spring构建的应用程序之外,也可以在任何其他语言运行的应用程序中使用。由于Spring Cloud Config实现的配置中心默认采用Git来存储配置信息,所以使用Spring Cloud Config构建的配置服务器,天然就支持对微服务应用配置信息的版本管理,并且可以通过Git客户端工具来方便的管理和访问配置内容。当然它也提供了对其他存储方式的支持,比如:SVN仓库、本地化文件系统。

[[237179]]

在本文中,我们将学习如何构建一个基于Git存储的分布式配置中心,并对客户端进行改造,并让其能够从配置中心获取配置信息并绑定到代码中的整个过程。

准备配置仓库

  • 准备一个git仓库,可以在码云或Github上创建都可以。比如本文准备的仓库示例:http://git.oschina.net/didispace/config-repo-demo
  • 假设我们读取配置中心的应用名为config-client,那么我们可以在git仓库中该项目的默认配置文件config-client.yml:
  1. info: 
  2.   profile: default 

为了演示加载不同环境的配置,我们可以在git仓库中再创建一个针对dev环境的配置文件config-client-dev.yml:

  1. info: 
  2.   profile: dev 

构建配置中心

通过Spring Cloud Config来构建一个分布式配置中心非常简单,只需要三步:

  • 创建一个基础的Spring Boot工程,命名为:config-server-git,并在pom.xml中引入下面的依赖(省略了parent和dependencyManagement部分):
  1. <dependencies> 
  2.     <dependency> 
  3.         <groupId>org.springframework.cloud</groupId> 
  4.         <artifactId>spring-cloud-config-server</artifactId> 
  5.     </dependency> 
  6. </dependencies> 
  • 创建Spring Boot的程序主类,并添加@EnableConfigServer注解,开启Spring Cloud Config的服务端功能。
  1. @EnableConfigServer 
  2. @SpringBootApplication 
  3. public class Application { 
  4.  
  5.     public static void main(String[] args) { 
  6.         new SpringApplicationBuilder(Application.class).web(true).run(args); 
  7.     } 
  8.  
  • 在application.yml中添加配置服务的基本信息以及Git仓库的相关信息,例如:
  1. spring 
  2.   application: 
  3.     name: config-server 
  4.   cloud: 
  5.     config: 
  6.       server: 
  7.         git: 
  8.           uri: http://git.oschina.net/didispace/config-repo-demo/ 
  9. server: 
  10.   port: 1201 

到这里,使用一个通过Spring Cloud Config实现,并使用Git管理配置内容的分布式配置中心就已经完成了。我们可以将该应用先启动起来,确保没有错误产生,然后再尝试下面的内容。

如果我们的Git仓库需要权限访问,那么可以通过配置下面的两个属性来实现;

  • spring.cloud.config.server.git.username:访问Git仓库的用户名
  • spring.cloud.config.server.git.password:访问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://localhost:1201/config-client/dev/master,并获得如下返回:

  1.     "name""config-client"
  2.     "profiles": [ 
  3.         "dev" 
  4.     ], 
  5.     "label""master"
  6.     "version"null
  7.     "state"null
  8.     "propertySources": [ 
  9.         { 
  10.             "name""http://git.oschina.net/didispace/config-repo-demo/config-client-dev.yml"
  11.             "source": { 
  12.                 "info.profile""dev" 
  13.             } 
  14.         }, 
  15.         { 
  16.             "name""http://git.oschina.net/didispace/config-repo-demo/config-client.yml"
  17.             "source": { 
  18.                 "info.profile""default" 
  19.             } 
  20.         } 
  21.     ] 

我们可以看到该Json中返回了应用名:config-client,环境名:dev,分支名:master,以及default环境和dev环境的配置内容。

构建客户端

在完成了上述验证之后,确定配置服务中心已经正常运作,下面我们尝试如何在微服务应用中获取上述的配置信息。

  • 创建一个Spring Boot应用,命名为config-client,并在pom.xml中引入下述依赖:
  1. <dependencies> 
  2.     <dependency> 
  3.         <groupId>org.springframework.boot</groupId> 
  4.         <artifactId>spring-boot-starter-web</artifactId> 
  5.     </dependency> 
  6.     <dependency> 
  7.         <groupId>org.springframework.cloud</groupId> 
  8.         <artifactId>spring-cloud-starter-config</artifactId> 
  9.     </dependency> 
  10. </dependencies> 
  • 创建Spring Boot的应用主类,具体如下:
  1. @SpringBootApplication 
  2. public class Application { 
  3.  
  4.     public static void main(String[] args) { 
  5.         new SpringApplicationBuilder(Application.class).web(true).run(args); 
  6.     } 
  7.  

创建bootstrap.yml配置,来指定获取配置文件的config-server-git位置,例如:

  1. spring: 
  2.   application: 
  3.     name: config-client 
  4.   cloud: 
  5.     config: 
  6.       uri: http://localhost:1201/ 
  7.       profile: default 
  8.       label: master 
  9.  
  10. server: 
  11.   port: 2001 

上述配置参数与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中的配置信息才能被正确加载。

在完成了上面你的代码编写之后,读者可以将config-server-git、config-client都启动起来,然后访问http://localhost:2001/info ,我们可以看到该端点将会返回从git仓库中获取的配置信息:

  1.     "profile""default" 

另外,我们也可以修改config-client的profile为dev来观察加载配置的变化。

代码示例

样例工程将沿用之前在码云和GitHub上创建的SpringCloud-Learning项目,重新做了一下整理。通过不同目录来区分Brixton和Dalston的示例。

具体工程说明如下:

  • 基于Git仓库的配置中心:config-server-git
  • 使用配置中心的客户端:config-client

【本文为51CTO专栏作者“翟永超”的原创稿件,转载请通过51CTO联系作者获取授权】

戳这里,看该作者更多好文

责任编辑:武晓燕 来源: 51CTO专栏
相关推荐

2017-07-28 16:41:53

Spring Clou微服务架构

2018-05-23 15:58:27

Spring Clou微服务架构

2017-08-09 15:50:47

Spring Clou微服务架构

2017-08-10 11:15:05

Spring Clou微服务架构

2017-09-09 23:15:20

Spring Clou微服务架构路由

2018-03-02 16:11:29

Spring Clou分布式服务跟踪

2017-12-20 15:37:39

Spring Clou微服务架构

2018-04-16 14:56:56

微服务架构分布式服务

2018-04-18 16:07:49

Spring Clou微服务分布式

2018-04-09 13:56:13

微服务架构分布式

2018-03-13 16:42:26

分布式服务跟踪

2018-04-02 15:01:31

微服务架构分布式服务

2017-09-15 23:29:53

Spring Clou微服务架构过滤器

2017-06-26 09:06:10

Spring Clou微服务架构

2019-10-24 11:17:57

架构运维技术

2017-09-04 16:15:44

服务网关架构

2023-08-25 16:26:49

微服务架构

2023-09-12 22:58:51

分布式架构微服务

2018-07-09 09:27:10

Spring Clou微服务架构

2017-07-03 09:50:07

Spring Clou微服务架构
点赞
收藏

51CTO技术栈公众号