Spring Cloud 配置服务

Spring Cloud 配置服务

1. 配置服务简介

  • 产生背景:
    传统开发中,我们通常是将系统的业务无关配置(数据库,缓存服务器)在properties中配置,在这个文件中不会经常改变,但随着系统规模的扩大,项目成员越来越多,会有越来越多的伙伴更改配置文件,开发、测试、生产环境分离,因配置产生的问题越来越多。完全可以避免因配置文件的导致的问题产生,配置服务应运而生。
  • 什么是配置服务?
    将配置统进行集中管理,提供一配置服务,开发、测试、生产环境均可直接从配置服务器中读取配置信息。大致就是,应用在启动后从配置服务器中获取配置信息,加入到环境中。这样所有的配置服务都可以进行集中管理。
  • 顶层架构设计如图所示。
    client-server-scm

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方法,其流程如图所示。
ConfigServicePropertySourceLocator.locate
关键方法为RestTemplate的exchange方法。其主要功能为的调用远程服务器的接口获取相关配置。

posted @ 2016-11-20 19:11  dragonfei  阅读(13836)  评论(0编辑  收藏  举报