Spring Cloud Config 能做什么?我们可以将分布式系统的配置文件托管在Git仓库或者数据库中,Config Server 负责管理配置文件,以Restful的形式提供给其他服务,可以在任何其他语言的应用程序中使用,不依赖Spring Cloud全家桶。
本节目标
使用Spring Cloud 基于Git仓库搭建分布式配置中心
版本
Spring Cloud Greenwich.SR2
Spring Boot 2.1.7.RELEASE
Git仓库
在你喜欢的Git平台上建立一个仓库,创建一个目录,并建立几个配置文件(建议网络较慢的同学选择Gitee)
搭建Config Server
pom.xml1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-config-server</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
		...
</dependencies>
<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Greenwich.SR2</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
</dependencyManagement>
application.properties1
2
3
4
5
6
7
8
9
10server.port = 9001
spring.application.name = config-server
#表示配置中心所在仓库的位置
spring.cloud.config.server.git.uri = https://gitee.com/yintianwen7/cloud-config.git
#仓库路径下的的相对搜索位置,可以配置多个
spring.cloud.config.server.git.search-paths = demo
#git的用户名
spring.cloud.config.server.git.username = yourusername
#git的密码
spring.cloud.config.server.git.password = yourpassword
ConfigServerApplication.java1
2
3
4
5
6
7
8
9
public class ConfigServerApplication {
	public static void main(String[] args) {
		SpringApplication.run(ConfigServerApplication.class, args);
	}
}
至此,ConfigServer 搭建完成,我们可以访问ConfigServer 查看 cloud-config/demo 下的配置文件
访问方式如下:
- /{application}/{profile}[/{label}]
 - /{application}-{profile}.yml
 - /{label}/{application}-{profile}.yml
 - /{application}-{profile}.properties
 - /{label}/{application}-{profile}.properties
 
这里 label 为git 分支,默认master
例如访问 application-dev.yml,直接请求 localhost:9001/application/dev即可
可以尝试修改一下Git仓库中的配置文件,再访问Config Server,这时你会发现Config Server中的数据是实时的。
搭建 Client 应用访问Config Server
pom.xml1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-config</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
		...
</dependencies>
<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Greenwich.SR2</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
</dependencyManagement>
bootstrap.properties,注意这里是 bootstrap.properties,不然你会发现你请求的URI一直是localhost:88881
2
3
4
5spring.profiles.active=simple
spring.application.name=application
spring.cloud.config.label=master
#  拼接规则: uri/name/profile/label
spring.cloud.config.uri=http://localhost:9001
CloudConfig.java 配置文件的映射实体,同理也可以@Value 或者 Environment 对象读取属性1
2
3
4
5
6
7
8("cloud.config")
public class CloudConfig {
    private String a;
    private String b;
    private String c;
   // 省略 get set
}
ClientApplication.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(CloudConfig.class)
public class ClientApplication {
	public static void main(String[] args) {
		SpringApplication.run(ClientApplication.class, args);
	}
	
	private CloudConfig cloudConfig;
	// 刷新配置时 POST /actuator/refresh
	("config")
	public Object config() {
		return cloudConfig;
	}
}
启动 Client,查看日志,可以看到请求配置中心的URL
请求 localhost:8080/config ,验证配置文件是否读取成功
刷新客户端配置
Client 中读取的配置文件,并不是实时的,我们可以通过修改Git仓库中的文件来验证这点。那么如何刷新配置呢?
pom.xml 引入1
2
3
4<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
bootstrap.properties 追加1
management.endpoints.web.exposure.include=*
对需要刷新的Bean,添加注解@RefreshScope1
2
3
4
("cloud.config")
public class CloudConfig {
}
POST /actuator/refresh,即可刷新配置
Config Server 加密解密
允许数据以加密形式存储在Git仓库,配置中心负责对加密的数据进行解密,然后提供给客户端应用。
由于篇幅问题,这里不讲了,感兴趣的小伙伴参考 Spring Cloud构建微服务架构:分布式配置中心(加密解密)
配置中心高可用
方案1:使用传统负载均衡器
方案2:将client、config server 注册到Spring Cloud注册中心,通过注册中心访问配置中心,具体代码参考👇
Demo
参考
http://blog.didispace.com/spring-cloud-starter-dalston-3-2/
https://segmentfault.com/a/1190000012908853