1、简介
Spring Cloud Config为分布式系统外部化配置提供了服务器端和客户端的支持,它包括Config Server和Config Client两部分。由于Config Server和Config Client都实现了对 Spring Environment和PropertySource抽象的映射,因此,Spring Cloud Config非常适合Spring应用程序,当然也可与任何其他语言编写的应用程序配合使用。
Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置内容(也可使用Subversion、本地文件系统或Vault 存储配置),因此可以方便的实现对配置的版本控制与内容审计。
Config Client 是Config Server的客户端,用于操作存储在Config Server中的配置属性。
好处:
集中管理配置。一个使用微服务架构的应用系统可能会包含成百上千个微服务,因此集中管理配置是非常有必要的。
不同环境,不同配置。例如数据源配置在不同的环境(开发、测试、预发布、生 产等)中是不同的。
运行期间可动态调整。例如我们可根据各个微服务的负载情况,动态调整数据源连接池大小或熔断阈值,并且在调整配置时不停止微服务。
配置修改后可自动更新。如配置内容发生变化,微服务能够自动更新配置。
2、集成
server端:
引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
启动类上添加@EnableConfigServer注解
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
添加配置文件application.yml
server:
port: 9800
添加配置文件bootstrap.yml
spring:
application:
name: config-server
cloud:
config:
server:
git:
# 配置Git仓库的地址及登录用户名和口令
uri: http://***
username: ***
password: ***
#配置文件分支
default-label: master
#配置文件所在根目录
search-path