一、背景
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。
Spring Cloud Config 分为服务端和客户端两个部分。其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并未客户端提供获取配置信息、加密/解密信息等访问接口. 它支持配置文件放在在配置服务的内存中,也支持放在远程Git仓库里
引入spring cloud config后,我们的外部配置文件就可以集中放置在一个git仓库里,再新建一个config server,用来管理所有的配置文件,维护的时候需要更改配置时,只需要在本地更改后,推送到远程仓库,所有的服务实例都可以通过config server来获取配置文件,这时每个服务实例就相当于配置服务的客户端config client,为了保证系统的稳定,配置服务端config server可以进行集群部署,即使某一个实例,因为某种原因不能提供服务,也还有其他的实例保证服务的继续进行。
二、快速入门
1.构建配置中心
- 创建一个基础的Spring Boot 工程,命名 config-server,依赖如下:
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
</properties>
<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>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 创建程序主类,添加@EnableConfigServer注解,开启Spring Cloud Config 的服务端功能。
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
远程Git仓库 如下:
- 在application.properties 中添加配置服务的基本信息及Git仓库的相关信息:
#服务端口
server.port=7001
#服务名称
spring.application.name=config-server
spring.cloud.config.server.git.uri=https://gitee.com/liaock/conifg-server
#配置文件所在的目录
spring.cloud.config.server.git.search-paths=/**
#git仓库的用户名
spring.cloud.config.username=******
#git仓库的密码
spring.cloud.config.password=******
在浏览器输入:http://localhost:7001/didispace/dev/config-label-test
返回结果如下:
{“name”:“didispace”,“profiles”:[“dev”],“label”:“config-label-test”,“version”:“044a5d940904460db11e7ca96b1075b5e154043a”,“state”:null,“propertySources”:[{“name”:“https://gitee.com/liaock/conifg-server/spring-cloud_in_action/didispace-dev.properties",“source”:{“name”:“lisi”,“age”:“18”,“version”:“dev”}},{“name”:“https://gitee.com/liaock/conifg-server/spring-cloud_in_action/didispace.properties”,“source”:{“from”:"git-default-2.0”}}]}
测试成功 !
2、仓库配置规则
配置信息的url和配置文件的映射关系如下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml或/{application}-{profile}.properties
- /{label}/{application}-{profile}.yml或/{label}/{application}-{profile}.properties
3、客户端配置映射
- 创建SpringBoot应用,命名 config-client,引入依赖,如下:
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 创建应用主类:
package com.gildata.configclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
- 创建bootstrap.properties 配置,来获取config-server位置,例如:
spring.application.name=didispace
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:7001/
server.port=7002
- 创建一个Restful 接口来返回配置中心的from属性,通过 @Value("${from}") 绑定配置服务中配置的 from 属性,具体如下:
package com.gildata.configclient.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by liaock on 2019/1/24
**/
@RefreshScope
@RestController
public class TestController {
@Value("${from}")
private String from;
@Autowired
private Environment evn;
@RequestMapping("/from")
public String from(){
return this.from;
//return evn.getProperty("from","undefined");
}
}
启动 config-client应用,并访问:http://localhost:7002/from
返回:git-default-1.0
测试完成 !
三、服务端详解
1.基础架构
- 远程 Git 仓库
- Config Server
- 本地 Git 仓库
- service A 、Service B : 具体的微服务应用,它们指定了Config Server 的地址,从而实现从外部化获取应用自己要用的配置信息.
客户端从配置管理中获取配置流程:
1、应用启动,根据bootstrap.properties中配置的应用名{application}、环境名{profile}、分支名{label},行配置中心获取配置信息。
2、ConfigServer根据自己维护的Git仓库信息和客户端传递过来的配置定位信息去查找配置信息。
3、通过git clone命令将找到的配置信息下载到ConfigServer的文件系统中。
4、ConfigServer创建Spring的ApplicationContext实例,并从git本地仓库中加载配置文件,最后将这些配置文件内容读取出来返回给客户端
5、客户端应用在获得外部配置文件后加载到客户端ApplicationContext实例,该配置内容的优先级高于客户端Jar包内部的配置内容,所以在Jar包中重复的内容将不再被加载。