对于传统的单体应用而言,常使用配置文件来管理所有配置,比如 SpringBoot 的 application.yml 文件,但是在微服务架构中全部手动修改的话很麻烦而且不易维护。微服务的配置管理一般有以下需求:
综上所述,对于微服务架构而言,一套统一的,通用的管理配置机制是不可缺少的重要组成部分。常见的做法就是通过配置服务器进行管理。
SpringCloud Config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同的微服务应用的所有环境提供了一个中心化的外部配置。
SpringCloud Config 分为服务端和客户端两部分。
服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器,并为客户端提供获取配置信息、加密/解密信息等访问接口。
客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。
配置服务器默认采用 git 来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过 git 客户端工具来方便的管理和访问配置内容。
Config Server 是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用 Git 存储配置文件的内容,也可以使用 SVN 存储,或者是本地文件存储。这里使用 Git 作为学习的环境。
仓库创建完成后,添加三个文件,分别为 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。



<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020artifactId>
<groupId>com.atguigu.springcloudgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-config-center-3344artifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-config-serverartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
dependencies>
project>
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);
}
}
先启动 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)