分布式系统面临的问题——配置问题

      微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要有配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。SpringCloud提供了ConfigServer来解决这个问题。

是什么?

官网:

SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。

怎么玩

SpringCloud Config分为服务端和客户端两部分

服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口

客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息,配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

能干嘛

  • 集中管理配置文件
  • 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
  • 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
  • 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
  • 将配置信息以REST接口的形式暴露,post/curl访问刷新即可

1.Config server与client配置与测试及动态刷新

config服务端通过git地址访问github(gitee)上的文件

新建一个项目,上传到github(gitee),模拟运维人员进行操作,实现远程与本地的整体一致性。
我的例子是建一个 springcloud-config仓库,包含三个yml文件,下面会读取文章文件中的信息

springcloud-study-config-client.yml

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
spring:
profiles:
active:
- dev
---
server:
port: 8201
spring:
profiles: dev
application:
name: springcloud-study-config-client
eureka:
client:
service-url:
defaultZone: http://eureka-dev.com:7001/eureka/
---
server:
port: 8202
spring:
profiles: test
application:
name: springcloud-study-config-client
eureka:
client:
service-url:
defaultZone: http://eureka-test.com:7001/eureka/

1.1. 新建module cloud-config-center-3344,是cloud的配置中心模块

1.2 pom.xml

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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.atguigu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>cloud-config-center-3344</artifactId>

<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>
</project>

1.3 application.yml

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

spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: https://gitee.com/zhangxin123456/springcloud-config.git #github仓库上面的git仓库名字
##搜索目录
search-paths: #可省略
- springcloud-config
#读取分支
label: master #可省略 默认mater

eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka #注册进eureka

1.4 主启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//EnableConfigServer
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class,args);
}
}


1.5 windows下修改hosts文件,增加映射

127.0.0.1 config-3344.com

1.6 启动3344 ,测试

直接在文件名上加入状态也行

这样就读到了github上的文件内容。说一下这个访问地址的组合,前面我们配置了hosts文件映射,所以能用config-3344.com代替localhost,master 指的是git仓库的master分支,config-test.yml 指的就是这个分支上的文件,就是这样访问的。

配置读取规则

官网定义,只介绍常用的三种:

第一种

第二种去掉了 {label},不过不在yml配置,默认访问master分支,如果访问不存在的文件,返回空

第三种和第一种差不多,label放在了后面,但是返回的是一个json串,我们可以自己解析里面的内容

小总结:

2.Config客户端配置与测试

客户端不直接访问github,而是通过服务端访问。

2.1 新建module cloud-config-client-3355

2.2 pom.xml

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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.atguigu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>cloud-config-client-3355</artifactId>

<dependencies>
<!--不带server了,说明是客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-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>
</project>

2.3 yml 文件

这里我们要学一个新的yml文件——bootstrap.yml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#bootstrap.yml
server:
port: 3355

spring:
application:
name: config-client
cloud:
#Config客户端配置
config:
label: master #分支名称
name: application #配置文件名称
profile: dev #读取后缀名称 上述3个综合:application-dev.yml的配置文件被读取 http://config-3344.com:3344/master/application-dev.yml
uri: http://localhost:3344 #配置中心地址 表示通过这个服务端访问


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


2.4 主启动类

1
2
3
4
5
6
7
8
9
10
11
12
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class,args);
}
}

2.5 业务类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//将配置中心以REST接口的形式暴露,然后客户端访问
package com.atguigu.springcloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo; //要访问的3344上的信息

@GetMapping("/configInfo") //请求地址
public String getConfigInfo(){
return configInfo;
}
}

2.6 启动3355,测试

客户端通过访问服务端REST暴露的接口 访问到dev

这样就成功实现了客户端3355访问 Config服务端3344 通过github获取配置信息

但是。现在修改config-dev.yml 配置并提交到github上,比如加个变量age或者版本号version。问题随之而来,分布式配置的动态刷新问题

Linux运维修改github上的配置文件内容做调整

刷新3344,发现ConfigServer配置中心立刻响应,因为直接连的github

刷新3355,发现ConfigClient客户端没有任何响应

3355没有变化,除非自己重启或者重新加载,难道每次运维修改配置文件,客户端都需要重启?实际生产当中某些微服务加载是很慢的,那就要使用动态刷新。

3.Config客户端之动态刷新

避免每次更新配置都需要重启客户端微服务3355

3.1 修改3355模块,引入actuator图形化监控

意思是说 我自己发生变化了能被别人监控到

1
2
3
4
5
6
<!--PS:gateway是不能加actuator的-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

3.2 修改yml,暴露监控端口

1
2
3
4
5
6
#添加配置,暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"

3.3. 业务类controller加@RefreshScope,刷新功能

1
2
3
4
5
6
7
8
9
10
11
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo; //要访问的3344上的信息

@GetMapping("/configInfo") //请求地址
public String getConfigInfo(){
return configInfo;
}
}

3.4 重启3355,测试

先来看3355 是否正常访问

正常,访问到 version=2,现在修改github文件,改为3

测试server端3344,正常

测试client端3355,多次刷新,还是2,没有改变

别急,此时需要运维人员发送Post请求刷新3355,@RefreshScope的作用就是自动获悉刷新的内容
必须是Post请求,使用curl命令,稍等一下,出现下面界面,激活3355

(用postman测试也行,我这里直接doc窗口了)
再来测试,3355也更新了

这样就避免了每次修改都要重启客户端服务,虽然需要自己发Post请求,但是也比重启好,两害相权取其轻。

想想还有什么问题?

假设有多个微服务客户端3355/3366/3377。。。每个微服务都需要执行一次post请求,可以写一个脚本,批量执行。但是还有没有更优化的方法?

我们想大范围的自动刷新,可否广播?一次通知,处处生效?
所以引入了SpringCloud Bus消息总线,下篇博客继续。