Spring Cloud 配置服务
2016-11-20 19:11
281 查看
Spring Cloud 配置服务
1. 配置服务简介
产生背景:传统开发中,我们通常是将系统的业务无关配置(数据库,缓存服务器)在properties中配置,在这个文件中不会经常改变,但随着系统规模的扩大,项目成员越来越多,会有越来越多的伙伴更改配置文件,开发、测试、生产环境分离,因配置产生的问题越来越多。完全可以避免因配置文件的导致的问题产生,配置服务应运而生。
什么是配置服务?
将配置统进行集中管理,提供一配置服务,开发、测试、生产环境均可直接从配置服务器中读取配置信息。大致就是,应用在启动后从配置服务器中获取配置信息,加入到环境中。这样所有的配置服务都可以进行集中管理。
顶层架构设计如图所示。
2. SpringBoot搭建配置服务:
pom.xml添加依赖:<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
application.yml或者bootstrap.yml添加scm相关配置
spring: profiles: active: git cloud: config: enabled: true server: git: uri: http://ip:port/test/remoteConfig.git username: username password: password
3. 应用系统使用配置服务
pom.xml中添加配置:<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
application.yml或者bootstrap.yml添加配置服务器:
spring: cloud: config: name: db profile: dev label: master uri: http://ip:port enabled: true profiles: active: db
4. 服务器端实现:
服务器端实现比较简单,大致流程是:当服务器接收到配置服务请求的时候,会首先从git仓库中pull最新的配置信息到本地仓库,然后服务器从本地仓库中读取所需要的配置信息返回给客户端。5. 客户端实现:
spring-cloud-config-client.jar中的Spring.factories有如下一段配置# Bootstrap components org.springframework.cloud.bootstrap.BootstrapConfiguration=\ org.springframework.cloud.config.client.ConfigServiceBootstrapConfiguration,\ org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration
很明显关键入口为ConfigServiceBootstrapConfiguration,关键配置为
@Bean @ConditionalOnProperty(value = "spring.cloud.config.enabled", matchIfMissing = true) public ConfigServicePropertySourceLocator configServicePropertySource(ConfigClientProperties properties) { ConfigServicePropertySourceLocator locator = new ConfigServicePropertySourceLocator( properties); return locator; }
向spring容器中注入了ConfigServicePropertySourceLocator,容器在初始化的时候会调用locate方法,其流程如图所示。
关键方法为RestTemplate的exchange方法。其主要功能为的调用远程服务器的接口获取相关配置。
相关文章推荐
- spring-cloud构架微服务(1)-全局配置
- spring-cloud 实现更新配置不用重启服务 @FreshScope
- spring cloud+dotnet core搭建微服务架构:配置中心
- Spring Cloud Config统一管理微服务配置
- spring cloud+dotnet core搭建微服务架构:配置中心续(五)
- spring-cloud构架微服务(2)-全局配置二
- SpringCloud微服务架构搭建(二):配置中心
- spring cloud+dotnet core搭建微服务架构:配置中心(四)
- Spring Cloud构建微服务架构:分布式配置中心
- 基于spring-cloud相关技术整合,实现接口调用、服务容错、动态路由配置等
- Spring Cloud构建微服务架构(四)分布式配置中心
- 干货实操:微服务Spring Cloud 系列(一) Spring cloud Config 用SVN做配置仓库
- Spring Cloud构建微服务架构(四)分布式配置中心(续)
- (十)整合spring cloud云服务架构 - commonservice-config配置服务搭建
- Spring Cloud构建微服务架构(四)分布式配置中心
- Spring Cloud构建微服务架构(四)分布式配置中心
- 《Spring Cloud Netflix》 -- 服务注册和服务发现-Eureka的常用配置
- Spring Cloud构建微服务架构(四)分布式配置中心(续)
- 统一配置服务——spring cloud config
- spring cloud+.net core搭建微服务架构:配置中心续(五)