Config 分布式配置中心

1、Config 分布式配置中心介绍

1-1、分布式系统面临的配置问题

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

SpringCloud 提供了 ConfigServer 来解决这个问题,我们每一个微服务自己带着一个 application.yml,上百个配置文件的管理…/(ㄒ o ㄒ)/~~

1-2、是什么

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

1-3、怎么玩

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

  • 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口
  • 客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用 git 来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过 git 客户端工具来方便的管理和访问配置内容。

1-4、能干嘛

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

1-5、与 GitHub 整合配置

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

官网:https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/

2、Config 配置总控中心搭建

2-1、服务端远程仓库配置

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

由上一步获得刚新建的 git 地址

1
2
HTTP:https://github.com/***/SpringCloudStudy.git
SSH:git@github.com:***/SpringCloudStudy.git

在仓库下新建三个文件

config-dev.yml

1
2
3
4
5
spring:
application:
name: springcloud-config-dev
profiles:
active: dev

config-prod.yml

1
2
3
4
5
spring:
application:
name: springcloud-config-prod
profiles:
active: prod

config-test.yml

1
2
3
4
5
spring:
application:
name: springcloud-config-test
profiles:
active: test

2-2、配置模块 cloud-config-center-3344

新建 Module 模块 cloud-config-center-3344,它即为 Cloud 的配置中心模块

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
50
51
52
53
<?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>springCloud2023</artifactId>
<groupId>com.jcvv.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

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

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- config Server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

<!--eureka-client config Server也要注册进服务中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>
<dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<groupId>com.jcvv.springcloud</groupId>
<artifactId>cloud-api-common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>

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 #注册进eurkea服务器的微服务名
cloud:
config:
server:
git:
uri: https://github.com/***/SpringCloudStudy.git #github上面的仓库名 ***为自己的用户名
# 搜索目录
search-paths:
- springcloud-config ###目录名称
# 读取分支
label: master

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

主启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.jcvv.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
* @Classname ConfigCenterMain3344
* @Description chengzhi
* @Created by chengzhi
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344
{
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class, args);
}
}

测试

2-3、文件命名规则

{application} 是应用名称,对应到配置文件上来,就是配置文件的名称部分。

{profile} 是配置文件的版本,我们的项目有开发版本、测试环境版本、生产环境版本,对应到配置文件上来就是以 application-{profile}.yml 加以区分,例如 application-dev.yml,application-prod.yml。

2-4、文件访问规则

不加分支名默认是 master

注意:/{application}/{profile}[/{label}]出的是 json 字符串

3、Config 客户端配置与测试

新建 cloud-config-client-3355

2-1、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
50
51
52
53
54
<?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>springCloud2023</artifactId>
<groupId>com.jcvv.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

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

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<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-2、bootstrap.yml

appllication.yml 是用户级的资源配置项
bootstrap.yml 是系统级的,优先级更加高
SpringCloud 会创建一个”Bootstrap Context”作为 Spring 应用的 ApplicationContext 的父上下文。

初始化的时候,Bootstrap Context 负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的 Environment。

Bootstrap 属性有高优先级,默认情况下,它们不会被本地配置覆盖。BootstrapContext 和 ApplicationContext、有着不同的约定,所以新增了一个 bootstrap.yml 文件,保证 BootstrapContext 和 ApplicationContext 配置的分离。

bootstrap.yml

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

spring:
application:
name: config-client
cloud:
#Config客户端配置
config:
label: master #分支名称
name: config #配置文件名称
profile: test #读取后缀名称 上述3个综合:master分支上config-test.yml的配置文件被读取
uri: http://localhost:3344 #配置中心地址
# 综合上面四个 即读取配置文件地址为: http://localhost:3344/master/config-test.yml

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

3、主启动类

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

4、controller 层

1
2
3
4
5
6
7
8
9
10
@RestController
public class ConfigClientController {
@Value("${config.info}") //gitee里的yml文件里的内容
private String configInfo;

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

5、 测试

6、修改远程仓库配置文件内容

在本地的文件夹下将 springcloud-config 仓库的克隆下来,方便修改文件内容,当然也可以在仓库直接修改。

1
git clone https://gitee.com/用户名/springcloud-config.git

修改 config-dev.yml 文件内容,其余两个类似

1
2
3
4
5
6
7
spring:
application:
name: springcloud-config-dev
profiles:
active: dev
config:
info: master branch, springcloud-config/config-dev.yml version=2

使用以下命令在本地修改后提交到远程仓库

  • git add .
  • git commit . -m “init yml”
  • git push origin master

7、再次测试

8、 存在问题

当远程仓库配置文件被修改后,3344 和 3355 都要变,3344 变了,但是 3355 必须要重启才可以更新。

4、Config 客户端之动态刷新

避免每次更新配置都要重启客户端微服务 3355,我们需要它能动态刷新,下面来修改 3355 模块

4-1、pom.xml 引入 actuator 监控

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

4-2、修改 YML,暴露监控端口

1
2
3
4
5
6
# 暴露监控端点 actuator刷新配置
management:
endpoints:
web:
exposure:
include: '*'

说明:因为手动刷新需要自己调用一个类似于健康检查的端点(接口),所以呢,我们需要把这个端点给暴露出来,以便外部可访问

4-3、Controller 类添加@RefreshScope

1
2
3
4
5
6
7
8
9
10
11
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}") //gitee里的yml文件里的内容
private String configInfo;

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

4-4、问题和解决

此时修改配置文件内容,再测:http://localhost:3355/configInfo,发现 3355 还是没有变化

需要运维人员发送 Post 请求刷新 3355

1
curl -X POST "http://localhost:3355/actuator/refresh"

再次请求:http://localhost:3355/configInfo,成功实现了客户端 3355 刷新到最新配置内容,避免了服务重启

4-5、思考

想想还有什么问题?

  • 假如有多个微服务客户端 3355/3366/3377……,每个微服务都要执行一次 post 请求,手动刷新?
  • 可否广播,一次通知,处处生效?
  • 我们想大范围的自动刷新,求方法

于是就有下面的消息总线!

相关链接:Bus消息总线