package com.itmuch.cloud;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class ConfigClientController { @Value("${profile}") private String profile; //可以获取本地application.yml配置文件profile: ssss的值 @GetMapping("/profile") public String getProfile() { System.out.println(this.profile); return this.profile; }}
package com.itmuch.cloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); }}
application.yml
server: port: 8081# profile: ssss# 通过localhsot:8081/profile就可以访问到config server里面的配置,如果本地有profile属性就加载本地的profile属性#
bootstrap.yml
# springcloud里面有一个启动上下文,用来加载远端的配置就是config server里面的配置# 先加载bootstrap.yml加载config server里面的配置,然后去加载application.yml里面的配置# spring: cloud: config: uri: http://localhost:8080 #config server的端口 profile: dev label: master # 当configserver的后端存储是Git时,默认就是master application: name: foobar #本工程的名字,去config server的git仓库里面找foobar-dev.yml文件
4.0.0 com.itmuch.cloud microservice-spring-cloud 0.0.1-SNAPSHOT microservice-config-client jar UTF-8 org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-web
package com.itmuch.cloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication@EnableConfigServerpublic class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); }}
application.yml
server: port: 8080spring: cloud: config: server: git: uri: https://git.oschina.net/it-much/config-repo-51cto-video username: password: # git里面 foobar-dev.yml内容profile: profile-dev,给工程foobar使用的。# git里面application.yml内容profile: profile-default,档foobar工程找不到foobar-dev.yml时候使用。
application.yml.bak1-基础使用方式
server: port: 8080spring: cloud: config: server: git: uri: https://git.oschina.net/it-much/config-repo-51cto-video
application.yml.bak2-通配符
server: port: 8080spring: cloud: config: server: git: uri: https://git.oschina.net/it-much/{application}
application.yml.bak3-模式匹配
server: port: 8080spring: cloud: config: server: git: uri: https://git.oschina.net/it-much/config-repo-51cto-video # 公用 repos: simple: https://git.oschina.net/it-much/simple special: pattern: special*/dev*,special*/test* uri: https://git.oschina.net/it-much/special
application.yml.bak4-搜索路径
server: port: 8080spring: cloud: config: server: git: uri: https://git.oschina.net/it-much/config-repo-51cto-video # 公用 search-paths: - foo # foo路径 - bar # bar路径
application.yml.bak5-cloneOnStart
server: port: 8080spring: cloud: config: server: git: uri: https://git.oschina.net/it-much/config-repo-51cto-video # 公用 clone-on-start: true repos: simple: https://git.oschina.net/it-much/simple special: pattern: special*/dev*,special*/test* uri: https://git.oschina.net/it-much/special cloneOnStart: false
application.yml.bak6-账号密码
server: port: 8080spring: cloud: config: server: git: uri: https://git.oschina.net/it-much/config-repo-51cto-video username: password:
4.0.0 com.itmuch.cloud microservice-spring-cloud 0.0.1-SNAPSHOT microservice-config-server jar UTF-8 org.springframework.cloud spring-cloud-config-server