-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
总结.txt
78 lines (63 loc) · 3.57 KB
/
总结.txt
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
本Project演示Config Server从本地读取配置文件:
Config Server 可以从本地仓库读取配置文件,也可以从远处Git 仓库读取。本地仓库是指将所有的配置文件统一写在Config Server 工程目录下。
Config Sever 暴露HttpAPI 接口, ConfigClient 通过调用Config Sever 的H即API 接口来读取配置文件。
一、 构建Config Server
1. 在Config-server工程pom.xml文件增加如下引用:(父POM文件自己注意查看)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2. 在ConfigServerApplication类上新增@EnableConfigServer注解,开启config server功能。
3. 配置application.yml文件
spring:
cloud:
config:
server:
native:
search-locations: classpath:/shared
profiles:
active: native
application:
name: config-server
server:
port: 8769
#配置说明:
#1. spring.profiles.active=native 用来配置Config Server从本地读取配置文件
#2. spring.cloud.config.server.native.search-locations指定配置文件路径
4. 在resources/shared目录下新建config-client-dev.yml配置文件,配置数据如下:
server:
port: 8762
foo: foo version 1
二、 构建Config client
1.在config-client工程的pom.xml文件中新增如下引用: (父POM文件自己注意查看)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2.在config-client的配置文件bootstrap.yml中做配置。【 注意: bootstrap.yml相对于appliation具有优先的执行顺序。】
spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:8769
fail-fast: true
profiles:
active: dev
#配置说明:
# spring.cloud.config.url 指定configServer的访问url
# spring.cloud.config.fail-fast 表示如果没有读取成功,则执行快速失败
# sprisng.profiles.active表示读取dev环境的配置文件
# 所有的配置加起来,config-client就会去读config-server/resource/shared目录下面的 config-client-dev.yml文件
3.在ConfigClientApplication类中写一个API接口,读取配置文件foo变量,并通过API返回。
4.启动config-server工程,启动config-client工程。启动config-client工程会在控制台的日志中发现config-client向url为
http://localhost:8769的Config Server读取了配置文件,最终config-client程序启动的端口为8762,8762端口是在
config-server/resource/shared目录中的config-client-dev.yml文件中配置的。由此可以见config-client向config-server中
成功读取配置文件。
日志内容如: Fetching config from server at : http://localhost:8769
5. 访问http://localhost:8762/foo,浏览器显示:
foo version 1