一 代码位置
https://github.com/cakin24/spring-cloud-code/tree/master/ch14-3
二 启动consul
consul agent -dev -ui
三 客户端配置
spring:
application:
name: consul-config-customize
cloud:
consul:
config:
format: yaml # Consul 中 Value 配置格式为 yaml
prefix: configuration # Consul 中配置文件目录为 configuration, 默认为 config
default-context: app # 去该目录下查找缺省配置,默认为 application
profile-separator: ':' # profiles配置分隔符,默认为‘,’
data-key: data # 如果指定配置格式为 yaml 或者 properties, 则需要该值作为key,默认为 data
四 在Consul的配置页面配置如下数据
按照同样的方式再配置一条数据
foo:
bar:
name: cakin_test
server:
port: 8083
注意上面输入时用空格,不要用Tab键,否则启动consul-config-customize会报错。
五 测试
1 启动consul-config-customize
2019-11-24 14:51:56.624 INFO 17576 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8083 (http)
2019-11-24 14:51:56.661 INFO 17576 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
启动了Consul的8083端口
2 浏览器输入:http://localhost:8083/getName
高级配置生效。