项目源码地址:https://gitee.com/lilyssh/high-concurrency
Spring Boot 2.0.4 集成 spring-cloud-config 2.0.1。
一、配置config服务端
新建配置服务项目,如config-server。
1. 在config-server项目中添加依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 1<dependencyManagement>
2 <dependencies>
3 <dependency>
4 <groupId>org.springframework.cloud</groupId>
5 <artifactId>spring-cloud-config</artifactId>
6 <version>2.0.1.RELEASE</version>
7 <type>pom</type>
8 <scope>import</scope>
9 </dependency>
10 </dependencies>
11</dependencyManagement>
12
13<dependencies>
14 <dependency>
15 <groupId>org.springframework.cloud</groupId>
16 <artifactId>spring-cloud-config-server</artifactId>
17 </dependency>
18</dependencies>
19
20
2. 创建配置仓库
在github上新建一个空项目:https://gitee.com/lilyssh/config-repo 作为配置仓库。
在config-repo项目中新建一个公用的配置文件application-dev.yml,内容如下:
1
2
3
4
5
6
7
8
9 1logging:
2 level:
3 cn.lilyssh: info
4
5 dubbo:
6 registry:
7 address: zookeeper://ssh.qianxunclub.com:2181
8
9
再新建一个数据库配置文件mysql-dev.yml,内容如下:
1
2
3
4
5
6
7
8
9
10
11 1spring:
2 datasource:
3 druid:
4 url: jdbc:mysql://db.qianxunclub.com:3306/demo
5
6mybatis-plus:
7 global-config:
8 db-config:
9 id-type: AUTO
10
11
3. 在config-server项目application.yml中配置仓库信息
1
2
3
4
5
6
7
8
9
10 1spring:
2 cloud:
3 config:
4 server:
5 git:
6 uri: https://gitee.com/lilyssh/config-repo.git
7 basedir: config/data
8 search-paths: /**
9
10
- uri:上一步创建的配置仓库地址。
- basedir: 把远程仓库数据下载到本地的目录。由于源码中写着会访问到父级目录,所以此处需要设置为两级目录。
- search-paths: 要下载的远程仓库的目录。
4. 测试访问远程仓库配置
在浏览器中,输入地址http://localhost:8888/{app}/{profile} 由于刚在远程仓库建的配置文件名为application-dev.yml,所以需要访问
http://localhost:8888/application/dev 进行测试,会看到:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 1{
2 "name":"application",
3 "profiles":[
4 "dev"
5 ],
6 "label":null,
7 "version":"9fdeb7331e3e78da436b0a404707c1873fe72076",
8 "state":null,
9 "propertySources":[
10 {
11 "name":"https://gitee.com/lilyssh/config-repo.git/application-dev.yml",
12 "source":{
13 "logging.level.cn.lilyssh":"info",
14 "spring.dubbo.registry.address":"zookeeper://ssh.qianxunclub.com:2181"
15 }
16 }
17 ]
18}
19
20
二、配置config客户端
1. 在order-provider项目中添加依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 1<dependencies>
2 <dependency>
3 <groupId>org.springframework.cloud</groupId>
4 <artifactId>spring-cloud-starter-config</artifactId>
5 </dependency>
6</dependencies>
7
8<dependencyManagement>
9 <dependencies>
10 <dependency>
11 <groupId>org.springframework.cloud</groupId>
12 <artifactId>spring-cloud-config</artifactId>
13 <version>2.0.1.RELEASE</version>
14 <type>pom</type>
15 <scope>import</scope>
16 </dependency>
17 </dependencies>
18</dependencyManagement>
19
20
2. 新建配置文件bootstrap.yml
1
2
3
4
5
6
7
8 1spring:
2 cloud:
3 config:
4 name: application,mysql
5 profile: dev
6 uri: http://config.qianxunclub.com
7
8
- name: 配置文件名的前半部分,如“db-dev.yml”中的db。如果是多个,以逗号分隔。
- profile: 配置文件名的后半部分,如"db-dev.yml中得dev。
- uri: 配置服务地址。
三、测试
大功告成!