Spring Cloud Spring Boot mybatis 企业分布式微服务云(六)分布式配置中心【Dalston版】
2018-02-02 10:03
1136 查看
Spring Cloud Config是Spring Cloud团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分。其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息、加密/解密信息等访问接口;而客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。
Spring Cloud Config实现了对服务端和客户端中环境变量和属性配置的抽象映射,所以它除了适用于Spring构建的应用程序之外,也可以在任何其他语言运行的应用程序中使用。由于Spring Cloud Config实现的配置中心默认采用Git来存储配置信息,所以使用Spring Cloud Config构建的配置服务器,天然就支持对微服务应用配置信息的版本管理,并且可以通过Git客户端工具来方便的管理和访问配置内容。当然它也提供了对其他存储方式的支持,比如:SVN仓库、本地化文件系统。
假设我们读取配置中心的应用名为
创建一个基础的Spring Boot工程,命名为:
创建Spring Boot的应用主类,具体如下:
创建
上述配置参数与Git中存储的配置文件中各个部分的对应关系如下:
spring.application.name:对应配置文件规则中的
spring.cloud.config.profile:对应配置文件规则中的
spring.cloud.config.label:对应配置文件规则中的
这里需要格外注意:上面这些属性必须配置在bootstrap.properties中,这样config-server中的配置信息才能被正确加载。
在完成了上面你的代码编写之后,读者可以将config-server-git、config-client都启动起来,然后访问http://localhost:2001/info ,我们可以看到该端点将会返回从git仓库中获取的配置信息:
spring.cloud.config.uri:配置中心
![](https://static.oschina.net/uploads/space/2018/0201/094635_tRag_3776687.jpg)
另外,我们也可以修改config-client的profile为dev来观察加载配置的变化。源码来源
创建Spring Boot的应用主类,具体如下:
Spring Cloud Config实现了对服务端和客户端中环境变量和属性配置的抽象映射,所以它除了适用于Spring构建的应用程序之外,也可以在任何其他语言运行的应用程序中使用。由于Spring Cloud Config实现的配置中心默认采用Git来存储配置信息,所以使用Spring Cloud Config构建的配置服务器,天然就支持对微服务应用配置信息的版本管理,并且可以通过Git客户端工具来方便的管理和访问配置内容。当然它也提供了对其他存储方式的支持,比如:SVN仓库、本地化文件系统。
准备配置仓库
准备一个git仓库,可以在码云或Github上创建都可以。比如本文准备的仓库示例:http://git.oschina.net/didispace/config-repo-demo假设我们读取配置中心的应用名为
config-client,那么我们可以在git仓库中该项目的默认配置文件
config-client.yml:
info: profile: default为了演示加载不同环境的配置,我们可以在git仓库中再创建一个针对dev环境的配置文件
config-client-dev.yml:
info: profile: dev
构建配置中心
通过Spring Cloud Config来构建一个分布式配置中心非常简单,只需要三步:创建一个基础的Spring Boot工程,命名为:
config-server-git,并在
pom.xml中引入下面的依赖(省略了parent和dependencyManagement部分):
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies>
创建Spring Boot的应用主类,具体如下:
@SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }
创建
bootstrap.yml配置,来指定获取配置文件的
config-server-git位置,例如:
spring: application: name: config-client cloud: config: uri: http://localhost:1201/ profile: default label: master server: port: 2001
上述配置参数与Git中存储的配置文件中各个部分的对应关系如下:
spring.application.name:对应配置文件规则中的
{application}部分
spring.cloud.config.profile:对应配置文件规则中的
{profile}部分
spring.cloud.config.label:对应配置文件规则中的
{label}部分
这里需要格外注意:上面这些属性必须配置在bootstrap.properties中,这样config-server中的配置信息才能被正确加载。
在完成了上面你的代码编写之后,读者可以将config-server-git、config-client都启动起来,然后访问http://localhost:2001/info ,我们可以看到该端点将会返回从git仓库中获取的配置信息:
spring.cloud.config.uri:配置中心
config-server的地址
{ "profile": "default" }
![](https://static.oschina.net/uploads/space/2018/0201/094635_tRag_3776687.jpg)
另外,我们也可以修改config-client的profile为dev来观察加载配置的变化。源码来源
创建Spring Boot的应用主类,具体如下:
相关文章推荐
- Spring Cloud Spring Boot mybatis 企业分布式微服务云(六)分布式配置中心【Dalston版】
- SpringCloud SpringBoot mybatis 分布式微服务(二)Spring Boot属性配置文件详解
- Spring Cloud Spring Boot mybatis分布式微服务云架构(四)属性配置文件详解(2)
- Spring Cloud Spring Boot mybatis 企业分布式微服务云(九)服务容错保护(Hystrix断路器)【Dalston版】
- SpringCloud SpringBoot mybatis 分布式微服务(十五)Spring Boot中使用MyBatis注解配置详解
- Spring Cloud Spring Boot mybatis 企业分布式微服务云(一)服务注册与发现(Eureka)【Dalston版】
- Spring Cloud Spring Boot mybatis分布式微服务云架构(四)属性配置文件详解(2)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二十六)使用MyBatis注解配置详解(1)
- Spring Cloud Spring Boot mybatis 企业分布式微服务云(五)服务消费(Feign)【Dalston版】
- Spring Cloud Spring Boot mybatis 企业分布式微服务云(二)服务注册与发现(Consul)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二十七)使用MyBatis注解配置详解(2)
- Spring Cloud Spring Boot mybatis 企业分布式微服务云(三)服务消费(基础)【Dalston版】
- Spring Cloud Spring Boot mybatis 企业分布式微服务云(一)服务注册与发现(Eureka)【Dalston版】
- Spring Cloud Spring Boot mybatis分布式微服务云架构(三十四)注解配置与EhCache使用(2)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(三)属性配置文件详解(1)
- Spring Cloud Spring Boot mybatis 企业分布式微服务云(七)服务容错保护(Hystrix服务降级)【Dalston版】
- Spring Cloud Spring Boot mybatis 企业分布式微服务云(五)服务消费(Feign)【Dalston版】
- Spring Cloud Spring Boot mybatis分布式微服务云架构(四)属性配置文件详解(2)
- Spring Cloud Spring Boot mybatis 企业分布式微服务云(三)服务消费(基础)【Dalston版】
- Spring Cloud Spring Boot mybatis 企业分布式微服务云(七)服务容错保护(Hystrix服务降级)【Dalston版】