开始Spring Cloud Config

什么是Spring Cloud Config

Spring Cloud Config项目提供了一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分。

Spring Cloud Config Sever的管理git或svn的外部配置,集中配置到所有客户端。

Spring Cloud Config Client根据Spring框架的Environment和PropertySource从Spring Cloud Config Sever获取配置。

所有要开始Spring Cloud Config,一定要先了解Spring Boot、Environment、PropertySource 、Profile等一些技术 。

Spring Cloud官网上提供了默认的基于git的配置,下面例子基于svn, svn地址用代替了,另行修改下。

@EnableConfigServer

构建Spring Cloud Config Server,只需要一个@EnableConfigServer

{(String… args) {SpringApplication.run(App.class, args).getEnvironment();}}

在到resource下面,,添加application.yml,加上配置

server: port: 8888spring: profiles:active: subversion cloud:config:server:svn:uri: https://www.xxx.com/svn/demo/demo-config-repo

添加pom.xml配置, 需要带入spring boot、spring cloud 和svn的jar

>org.springframework.cloud>Angel.SR3>>1.8>>org.springframework.cloud>>org.tmatesoft.svnkit>1.8.10>>test>>></plugins></build>

在https://www.xxx.com/svn/demo/demo-config-repo下面提交一个文件,比如demo-development.properties

运行App.class, 访问 :8888/{application}/{profile}/{label},比如::8888/dmeo/development/trunk 成功了。

{name: “demo”,profiles: [“developement”],label: “trunk”,propertySources: [{name: “https://www.xxx.com/svn/demo/demo-config-repo/trunk/demo.properties”,source: {demo.env: “default”}}]}

尝试下提供svn的属性,在访问,发现配置信息以及发生变化了。

Spring Cloud Config Client

客户端,仍然新建立一Spring boot的项目。加入spring-cloud-config-client包

>spring-cloud-config-client</artifactId></dependency>

添加bootstrap.yml到resources下面。加入配置

spring: cloud:config:name: loupanprofile: developmentlabel: trunkuri: http://localhost:8888

这里的:8888是刚刚启动的Spring Cloud Config Server的应用。

201511520 : : ]]

启动信息里面找到这样的日志,就成功了。它会自动加载的项目里面,你可以使用Spring的自动配置方便的使用外部配置。 例如直接在application.properties里面使用

spring.profiles.active = ${demo.env}

或者

@Configurationpublic class DemoConfig {@Value(“${demo.env}”)public String env;…

另外,他还提供了很多方式来满足需求。比如,修改了配置后,可以

//localhost:8080/refresh

来刷新配置。

//localhost:8080/restart

。。。

那我想明天可以是我的来世。

开始Spring Cloud Config

相关文章:

你感兴趣的文章:

标签云: