SpringCloud之Config Client 配置过程讲解
org.springframework.boot spring-boot-starter-parent 2.0.3.RELEASE UTF-8 UTF-8 1.8 Finchley.RELEASE org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
application.yml
server: port: 8080 # 指定监听端口 # 加载所有的端点。默认只加载了 info / health management: endpoints: web: exposure: include: "*"
bootstrap.yml
spring: application: name : configClient01 cloud: config: profile: test label: master uri: http://localhost:8090
配置解释:
spring.cloud.config.*:默认情况下,Config Server 会提供来自/{name}/{profile}/{label}的属性来源,其中 Config Client 中的默认绑定是:
"name" = ${spring.application.name} "profile" = ${spring.profiles.active} (actually Environment.getActiveProfiles()) "label" = "master"
但是,所有这些都可以通过设置spring.cloud.config.*(其中*为“name”,“profile”或“label”)来覆盖。(这里我们使用的是这种方式)
label 对于回滚到以前版本的配置非常有用;使用默认的 Config Server 实现时,label 可以是 git label,branch name 或者 commit id。label 也可以作为逗号分隔的列表提供,在这种情况下,列表中的项目将逐个尝试,直到一个成功。例如,当在一个 feature 分支上开发时,可能希望将 label 与分支对应,但将其设置为可选的(例如,spring.cloud.config.label = myfeature,develop)。
spring.cloud.config.uri:Config Server 的地址; spring.application.name:配置应用名称(有注册中心时用于对应用进行标识),这里暂时没有什么用处(也被上边覆盖掉了);
只需要修改 search-paths,至于为什么这样做,请看这里:《Spring Cloud 探索 | 分布式配置中心(Config Server)》
spring: cloud: config: server: git: search-paths : '{application}'