对于传统的单体应用而言,常使用配置文件来管理所有配置,比如 SpringBoot 的 application.yml 文件,但是在微服务架构中全部手动修改的话很麻烦而且不易维护。微服务的配置管理一般有以下需求:
- 集中配置管理,一个微服务架构中可能有成百上千个微服务,所以集中配置管理是很重要的。
- 不同环境不同配置,比如数据源配置在不同环境(开发、生产、测试)中是不同的。
- 运行期间可动态调整。例如,可根据各个微服务的负载情况,动态调整数据源连接池大小等。
- 配置修改后可自动更新。如配置内容发生变化,微服务可以自动更新配置。
综上所述,对于微服务架构而言,一套统一的,通用的管理配置机制是不可缺少的重要组成部分。常见的做法就是通过配置服务器进行管理。
二、SpringCloud Config 简介 2.1 简介SpringCloud Config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同的微服务应用的所有环境提供了一个中心化的外部配置。
SpringCloud Config 分为服务端和客户端两部分。
服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器,并为客户端提供获取配置信息、加密/解密信息等访问接口。
客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。
配置服务器默认采用 git 来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过 git 客户端工具来方便的管理和访问配置内容。
- 集中管理配置文件
- 不同环境不同配置,动态化的配置更新,分环境部署,比如 dev/test/prod/beta/release
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
- 当配置发生变动时,服务不需要重启即可感知到配置的变化,并应用新的配置
- 将配置信息以 REST 接口的形式暴露,可使用 post、curl 进行访问刷新。
Config Server 是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用 Git 存储配置文件的内容,也可以使用 SVN 存储,或者是本地文件存储。这里使用 Git 作为学习的环境。
3.1 登录 GitHub,创建 springcloud-config 仓库仓库创建完成后,添加三个文件,分别为 config-dev.yml,config-prod.yml,config-test.yml。
config-dev.ym 内容如下:
config: info: "master branch, springcloud-config/config-dev.yml version=1"
config-prod.ym 内容如下:
config: info: "master branch, springcloud-config/config-prod.yml version=1"
config-test.ym 内容如下:
config: info: "master branch, springcloud-config/config-test.yml version=1"
最后注意修改分支名称,将 main 修改为 master。
3.4 配置 application.ymlcloud2020 com.atguigu.springcloud 1.0-SNAPSHOT 4.0.0 cloud-config-center-3344 8 8 org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.projectlombok lombok org.springframework.boot spring-boot-starter-test
server:
port: 3344
spring:
application:
name: cloud-config-server
cloud:
config:
server:
git:
uri: https://github.com/penggithub123/springcloud-config.git
search-paths:
- springcloud-config
label: master
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
- 通过 spring.cloud.config.server.git.uri : 配置git服务地址
- 通过spring.cloud.config.server.git.username: 配置git用户名
- 通过spring.cloud.config.server.git.password: 配置git密码
git 服务地址获取:
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class, args);
}
}
3.6 测试
先启动 eureka7001 服务,再启动当前服务。然后访问:http://localhost:3344/master/config-dev.yml
访问:http://localhost:3344/master/config-prod.yml
访问:http://localhost:3344/master/config-test.yml
(1)/{label}/{application}-{profile}.yml
例如,master分支:
http://localhost:3344/master/config-dev.yml
http://localhost:3344/master/config-prod.yml
http://localhost:3344/master/config-test.yml
dev 分支:
http://localhost:3344/dev/config-dev.yml
http://localhost:3344/dev/config-prod.yml
http://localhost:3344/dev/config-test.yml
(2)/{application}-{profile}.yml
http://localhost:3344/config-dev.yml
http://localhost:3344/config-prod.yml
http://localhost:3344/config-test.yml
http://localhost:3344/config-xxxx.yml (不存在的配置)
(3)/{application}/{profile}[/{label}]
http://localhost:3344/config/dev/master
http://localhost:3344/config/prod/master
http://localhost:3344/config/test/master
总结:
/{name}-{profiles}.yml
/{label}-{name}-{profiles}.yml
label:分支(branch)
name :服务名
profiles:环境(dev/test/prod)



