SpringCloudConfig开发配置及使用文档
开发配置
ConfigServe
- 依赖配置
1 |
buildscript { |
- 文件配置
配置文件为项目resources文件下的application.properties文件
1 |
#uri配置可为远程的git仓库文件i.e:http://domain.com/project/repo,下面的为本地git仓库配置 |
- git仓库目录结构
1 |
/{application}/{profile}[/{label}] |
tips
1.对应的配置在client中配置spring.cloud.config为prefix的配置,具体请看下面的client配置
2.配置项目应该加上@EnableConfigServer注解
3.spring web项目集成cloud config的时候最好加上映射的前缀
1
spring.cloud.config.server.prefix=remote-config
ConfigClient
依赖配置
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大专栏 SpringCloudConfig开发配置及使用文档r/>35
36
37
38
39
40
41buildscript {
ext {
springBootVersion = '1.4.3.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'spring-cloud-client-demo'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
repositories {
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
maven { url "http://clojars.org/repo/" }
maven { url 'https://repo.spring.io/libs-milestone' }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator")
compile('org.springframework.cloud:spring-cloud-starter-config')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR3"
}
}文件配置
配置文件为项目resources文件下的application.properties文件
1
2
3
4
5
6
7
8
9
10
11spring.application.name=hello_world
##client配置
spring.cloud.config.discovery.serviceId=db23de
spring.cloud.config.label=master
spring.cloud.config.profile=db23de
spring.cloud.config.uri=http://localhost:8888
###需要用户名密码时可配置下面两个
#spring.cloud.config.password=password
#spring.cloud.config.username=username
##client配置
hello.world.name=hello worldtips
1.在需要刷新配置的class前加上@RefreshScope注解,这样刷新等待时候就会刷新这一部分而不必restart
2.在启动配置的地方加上@EnableDiscoveryClient注解
使用说明
1.在configClient下建立一个controller内容如下
1
2
3
4
5
6
7
8
9
10
11
12
public class {
"${hello.world.name}") (
private String name;
"/") (
public String index() {
return name;
}
}
2.有序启动configServe和configClinet
3.访问client的根路径,浏览器返回的是 hello world
4.在git.uri(configServe中配置的)路径下建立文件命令为hello_world-db23de.properties({applicationName}-{profile}.properties}),这里采用的是这一种文件格式.内容为hello.world.name=hello db23de
5.访问client/refresh,post请求
6.访问client的根路径,浏览器返回的是 hello db23de
7.client要加上访问远程访问的前缀需要覆盖一下spring cloud config client的源码
1
2
3ConfigServicePropertySourceLocator(line:148){
String path = "/{your-prefix}/{name}/{profile}";
}
相关demo
1.spring-cloud-config-client-demo
2.spring-cloud-config-serve-demo