本文共 1649 字,大约阅读时间需要 5 分钟。
Spring Cloud Config支持在Git, SVN和本地存放配置文件,使用Git或者SVN存储库可以很好地支持版本管理,Spring默认配置是使用Git存储库。在本案例中将使用OSChina提供的Git服务存储配置文件。为了能让Config客户端准确的找到配置文件,需要了解application, profile和label的概念
org.springframework.cloud spring-cloud-config-server
@SpringBootApplication@EnableConfigServerpublic class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); }}
配置appllication.yml 文件,设置服务器端口号和配置文件Git仓库的链接
spring: cloud: config: server: git: uri: https://github.com/csyeva/eductoconfig username: password:
/{application}/{profile}[/{label}]/{application}-{profile}.yml/{application}-{profile}.properties/{label}/{application}-{profile}.properties
org.springframework.cloud spring-cloud-starter-config
在bootstrap.yml中设置客户端名称spring.application.name和Config服务器的地址
spring: cloud: config: uri: http://localhost:8080 profile: dev label: master application: name: provider
@RestControllerpublic class ConfigClientController { @Value("${profile}") private String profile; @GetMapping("/profile") public String getProfile() { return this.profile; }}
转载地址:http://caonn.baihongyu.com/