SpringCloudConfig开发配置及使用文档

SpringCloudConfig开发配置及使用文档

开发配置

ConfigServe

  • 依赖配置
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
buildscript {
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 = 'sprint-cloud-config-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.cloud:spring-cloud-config-server')
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
#uri配置可为远程的git仓库文件i.e:http://domain.com/project/repo,下面的为本地git仓库配置
spring.cloud.config.server.git.uri=file:${user.home}/IdeaProjects/remote-application-config
#search-paths配置uri下面可扫面的文件下,默认是指扫描uri目录,子目录不扫描.可配置多个,以逗号隔开
spring.cloud.config.server.git.search-paths=hello_world
server.port=8888
  • git仓库目录结构
1
2
3
4
5
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
  • 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
    41
    buildscript {
    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
    11
    spring.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 world
  • tips

    1.在需要刷新配置的class前加上@RefreshScope注解,这样刷新等待时候就会刷新这一部分而不必restart

    2.在启动配置的地方加上@EnableDiscoveryClient注解

使用说明

  • 1.在configClient下建立一个controller内容如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

    @RefreshScope
    public class {

    @Value("${hello.world.name}")
    private String name;

    @RequestMapping("/")
    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
    3
    ConfigServicePropertySourceLocator(line:148){
    String path = "/{your-prefix}/{name}/{profile}";
    }

相关demo

1.spring-cloud-config-client-demo

2.spring-cloud-config-serve-demo

参考文档

1.spring-cloud-config-document

猜你喜欢

转载自www.cnblogs.com/liuzhongrong/p/12361541.html