Config服务端配置

git

由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持svn和本地文件,但最推荐的还是Git,而且使用的是http/https访问的形式)

用你自己的账号在Github上新建一个名为sprincloud-config的新Repository

image-20200709173103519

仓库中添加config-dev.yml,config-prod.ymlconfig-test.yml,三个文件内容只有名字不同。创建dev分支,将文件中的master全都修改为dev

文件内容

1
2
config:
info: "master branch,springcloud-config/config-dev.yml version=1"

image-20200709175032189

image-20200709210008878

clone到本地使用git clone命令

image-20200709173247670

Model

新建Module模块cloud-config-center-3344它既为Cloud的配置中心模块cloudConfig Center

image-20200709173708709

pom

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
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

yml

image-20200709203113035

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server:
port: 3344

spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: https://github.com/kylincw/sprincloud-config.git #github仓库的ssh地址
search-paths:
- springcloud-config
label: master

#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka

主启动类

编写ConfigCenterMain3344通过@EnableConfigServer开启ConfigServer服务

image-20200709174523820

1
2
3
4
5
6
7
8
@SpringBootApplication
//开启ConfigServer服务
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344 .class,args);
}
}

测试

启动7001 3344,访问

Config客户端配置

新建cloud-config-client-3355

image-20200709203357479

pom

config客户端导入spring-cloud-starter-config

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
<dependencies>
<!--config-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

yml

Spring Cloud会创建一个“Bootstrap Context”,作为Spring应用的“Application Context”的父上下文。初始化的时候,“Bootstrap Context”负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的“Environment”。

“Bootstrap”属性有高优先级,默认情况下,他们不会被本地配置覆盖。“Bootstrap context”和“Application Context”有着不同的约定,所以新增一个“bootstrap.yml”文件,保证“Bootstrap Context”和“Application Contetxt”的配置分离

application.yml是用户级的资源配置项,bootstrap.yml是系统级的,优先级更加高。

要将Client模块的application.yml文件改为bootstrap.yml这是很关键的。因为bootstrap.yml是比application.yml先加载的。

创建bootstrap.yml

image-20200709204723663

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server:
port: 3355

spring:
application:
name: config-client
cloud:
#Config客户端配置
config:
label: master #分支名称
name: config #配置文件名称
profile: dev #配置后缀名称
uri: http://localhost:3344 #uri 综合起来 读取http://localhost:3344/master/config-dev.yml文件
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka

主启动类

创建ConfigClientMain3355

image-20200709204853877

1
2
3
4
5
6
7
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run( ConfigClientMain3355.class,args);
}
}

controller

image-20200709205111184

1
2
3
4
5
6
7
8
9
10
11
@RestController
public class ConfigClientController {

@Value("${config.info}")
private String configInfo;

@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}

测试

启动7001 3344 3355

image-20200709205311785

3344自测访问http://localhost:3344/master/config-dev.yml

image-20200709205338118

3355测试访问http://localhost:3355/configInfo

image-20200709205444516

成功实现了客户端3355访问SpringCloud Config3344通过GitHub获取配置信息!

但是问题随之而来,分布式配置的动态刷新,Linux运维修改GitHub上的master分支的config-dev.yml配置文件内容做调整,version变为2

image-20200709211953897

刷新3344,发现ConfigServer配置中心立刻响应http://localhost:3344/master/config-dev.yml

image-20200709212058733

但是访问3355却依旧没有发生改变http://localhost:3355/configInfo

image-20200709212108659

只有重启重新加载才更新

image-20200709210928691

难道每次运维修改配置文件,客户端都需要重启??

Config客户端动态刷新

pom

引入actuator监控

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

yml

暴露监控端口

image-20200709211143013

1
2
3
4
5
6
#暴露监控端口
management:
endpoints:
web:
exposure:
include: "*"

controller

加上@RefreshScope注解

image-20200709213035908

1
2
3
4
5
6
7
8
9
10
11
12
@RefreshScope
@RestController
public class ConfigClientController {

@Value("${config.info}")
private String configInfo;

@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}

测试

重新启动3355,启动成功后将master分支的config-devversion修改为3

访问3344,更新成功

image-20200709212410598

访问3355,依旧为2

image-20200709210928691

配置没有生效

此时需要运维人员发送Post请求刷新3355curl -X POST "http://localhost:3355/actuator/refresh"必须是Post请求

image-20200709213145453

刷新3355页面,version更新为3

image-20200709213202641

但是目前还是存在问题,如果有多个微服务都需要更新,则要发多次post请求,手动刷新。可否广播,一次通知,处处生效?如果有些服务不需要更新,能否精确的控制刷新?这都是目前不能做到的。