开始Spring Cloud Config
2017-03-07 11:42
465 查看
什么是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地址用
www.xxx.com代替了,另行修改下。
@EnableConfigServer
构建Spring Cloud Config Server,只需要一个@EnableConfigServer
@Configuration @EnableAutoConfiguration @EnableConfigServer public class App { public static void main(String... args) { SpringApplication.run(App.class, args).getEnvironment(); } }1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
在到resource下面,添加
application.yml,加上配置
server: port: 8888 spring: profiles: active: subversion cloud: config: server: svn: uri: https://www.xxx.com/svn/demo/demo-config-repo[/code]1 2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
添加pom.xml配置, 需要带入spring boot、spring cloud 和svn的jar<!--spring cloud parent version--> <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Angel.SR3</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <start-class>com.xxx.App</start-class> <java.version>1.8</java.version> </properties> <dependencies> <!--spring cloud config server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!--config server need read svn--> <dependency> <groupId>org.tmatesoft.svnkit</groupId> <artifactId>svnkit</artifactId> <version>1.8.10</version> </dependency> <!--spring boot test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <!--mvn spring-boot:run 命令--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
在https://www.xxx.com/svn/demo/demo-config-repo下面提交一个文件,比如demo-development.properties
运行App.class, 访问http://localhost:8888/{application}/{profile}/{label},比如:http://localhost: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" } } ] }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{application} 匹配客户端的 “spring.application.name”
{profile} 匹配客户端的”spring.active.profiles”
{label} 如果是svn匹配trunk/branchs等.
尝试下提供svn的属性,在访问,发现配置信息以及发生变化了。客户端,仍然新建立一Spring boot的项目。加入
Spring Cloud Config Clientspring-cloud-config-client包<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency>1
2
3
4
1
2
3
4
添加bootstrap.yml到resources下面。加入配置spring: cloud: config: name: loupan profile: development label: trunk uri: http://localhost:8888[/code]1 2
3
4
5
6
7
1
2
3
4
5
6
7
这里的http://localhost:8888是刚刚启动的Spring Cloud Config Server的应用。2015-08-27 18:01:03.751 INFO 11520 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource [name='https://www.xxx.com/svn/demo/demo-config-repo/trunk/demo-developement.properties'], MapPropertySource [name='https://www.xxx.com/svn/demo/demo-config-repo/trunk/demo.properties']]]1
2
1
2
启动信息里面找到这样的日志,就成功了。它会自动加载的项目里面,你可以使用Spring的自动配置方便的使用外部配置。
例如直接在application.properties里面使用spring.profiles.active = ${demo.env}1
1
或者@Configuration public class DemoConfig { @Value("${demo.env}") public String env; ...1
2
3
4
5
6
7
1
2
3
4
5
6
7
另外,他还提供了很多方式来满足需求。比如,修改了配置后,可以$ curl -X POST http://localhost:8080/refresh[/code]1 1
来刷新配置。$ curl -X POST http://localhost:8080/restart[/code]
相关文章推荐
- Spring Cloud Config-快速开始
- Spring Cloud Config-快速开始
- Spring Cloud Config-快速开始
- Spring Cloud Config教程(四)快速开始
- Spring Cloud Config-快速开始
- 开始Spring Cloud Config
- Spring-Cloud-Config快速开始
- Spring-Cloud 学习之旅 --- 快速开始(一)
- springcloud(第二篇)springcloud config 修改配置
- Spring Cloud Config 快速入门
- spring cloud config 详解
- spring cloud config使用
- 【新技术】Spring Cloud微服务实战免费公开课开始啦~~~
- Spring Cloud Config
- Spring Cloud实战(一)-Spring Cloud Config Server
- Spring Cloud Config
- springcloud-config client配置文件为什么要用bootstrap命名
- springcloud(第一篇)springcloud config 入门
- Spring Cloud 笔记实战之 Spring Cloud Config
- Spring cloud config 文件加载环境