您的位置:首页 > 编程语言 > Java开发

开始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 Cloud Config Client

客户端,仍然新建立一Spring boot的项目。加入
spring-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] 
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: