SpringCloud-配置中心 Config Github

在分布式系统中,每一个功能模块都能拆分成一个独立的服务,一次请求的完成,可能会调用很多个服务协调来完成,为了方便服务配置文件统一管理,更易于部署、维护,所以就需要一个地方来管理这些配置信息。

在 spring cloud Config 就提供了这样的能力,通过集中化管理的方式,支持配置文件放在在配置服务的内存中远程 Git 仓库以及Subversion。

本篇将通过一个简单的 demo ,使用 spring cloud Config 原生提供的基于 Git 的方式来实现微服务体系下的配置管理功能。

环境准备

类别
JDK
1.8.0_162
SOFABoot/SpringBoot
3.0.0/2.0.x.RELEASE
SpringCloud
Finchley.RC1
IDE
IDEA

工程背景

本节将通过 SOFABoot 来集成 Spring Cloud Config ,以 git 作为存储,来实现分布式环境下的配置管理。本工程的父工程仍然是《SpringCloud-Eureka 服务注册》中构建的父工程。

由于我们是以 git 来存储配置文件的,因此我们需要在 github 上新建一个存储配置文件的空间,为了更方面的模拟,这里创建了两个配置文件:

  • glmapper-dev.properties
1
2
3
name=leishu@glmapper_dev
blog=http://www.glmapper.com
version=dev
  • glmapper-pre.properties
1
2
3
name=leishu@glmapper_pre
blog=http://www.glmapper.com
version=pre

github 地址:https://github.com/glmapper/glmapper-config-repository

新建 sofa-config-server

右击 sofa-eureka-parent 父工程 -> New -> Module,这里选择 Maven 工程;

  • artifactId:sofa-config-server

修改 pom 文件

这里直接引入 config 的依赖即可

1
2
3
4
5
6
7
8
9
10
11
12
13
<parent>
<artifactId>sofa-eureka-parent</artifactId>
<groupId>com.alipay.sofa</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sofa-config-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>

配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#服务端口
server:
port: 8091
#服务名称
spring:
application:
name: sofa-config-server
#服务的git仓库地址
cloud:
config:
server:
git:
uri: https://github.com/glmapper/glmapper-config-repository
search-paths: /**
username: glmapper_2018@163.com
password: ******
#指定分支
label: master
#服务注册中心
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/

如果你的 github 仓库是公开的话,就不需要输入账户和密码就可以访问。

启动类

在启动类上加 @EnableConfigServer 注解,激活对配置中心的支持

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

启动 & 验证

因为配置中心作为一个独立的服务,所以不需要依赖其他服务的先启动,直接运行当前程序即可。这里我们首先需要验证下 server 端是否已经成功拉取到了 github 上面的配置信息:

访问:http://localhost:8091/glmapper/pre/master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
"name": "glmapper",
"profiles": [
"pre"
],
"label": "master",
"version": "f9e8c1f2825d23031cb13d40e396a23c0f975d2d",
"state": null,
"propertySources": [
{
"name": "https://github.com/glmapper/glmapper-config-repository/glmapper-pre.properties",
"source": {
"name": "leishu@glmapper-pre",
"blog": "http://www.glmapper.com",
"version": "pre"
}
}
]
}

OK ,说明服务端已经成功拉取到了github上的配置文件了。

关于地址的说明:http://localhost:8091/glmapper/pre/master 。前半部分是ip和端口,没什么好说的。
glmapper/pre/master,因为我在github上新建的配置文件名是 glmapper-dev.properties 和 glmapper-pre .properties ;所以这里地址的规则就是 /glmapper/pre ,后面的 master 可带可不带,区别在于返回的 JSON 数据 label 是 null 还是 master,label 指向分支。

客户端

本节构建一个简单的客户端工程 sofa-config-client ,用于从 sofa-config-server 上获取配置文件并展示。sofa-config-client 同样基于《SpringCloud-Eureka 服务注册》中构建的父工程。

修改 pom 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<parent>
<artifactId>sofa-eureka-parent</artifactId>
<groupId>com.alipay.sofa</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>sofa-config-client</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

引入 spring-cloud-starter-config 和 spring-boot-starter-web 依赖。

配置文件

配置文件包括两个,一个是 application.yml ,另一个是 bootstrap.yml

  • application.yml
1
2
3
4
5
spring:
application:
name: sofa-config-client
server:
port: 8099
  • bootstrap.yml
1
2
3
4
5
6
7
spring:
cloud:
config:
name: glmapper
profile: pre
uri: http://localhost:8091/ #指向配置中心的地址
label: master

启动类

启动类不需要做什么修改,也不需要额外加什么注解

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

资源类

新建一个资源类,用于输出展示拉取到的配置信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@RestController
public class ConfigClientController {

@Value("${name}")
private String name;

@Value("${blog}")
private String blog;

@Value("${version}")
private String version;

@RequestMapping("/config")
public String config(){
return "name:"+name+" ,blog:"+blog+" ,version:"+version;

}
}

启动和运行

先后启动配置中心服务端和客户端程序。在浏览器中输入:http://localhost:8099/config ,返回如下:

1
name:leishu@glmapper-pre ,blog:http://www.glmapper.com ,version:pre

我们尝试下将 github 中 glmapper-pre.properties 这个配置文件进行修改,看下是否在这能获取到最新的依赖,修改之后,如下:

1
2
3
name=leishu@glmapper_pre_update
blog=http://www.glmapper.com
version=pre

重新刷新浏览器地址,返回如下:

1
name:leishu@glmapper-pre ,blog:http://www.glmapper.com ,version:pre

这里并没有发生任何变换,因为 SpringBoot 项目只会在项目启动时才会获取一次配置文件信息,当我们修改了 github 上的配置文件之后,当前的配置中心客户端并没有主动去获取配置值,所以不会有新的值,我们获取到的还是旧的值。那么下面通过修改和增加一些组件和配置来实现不停服动态更新配置。

配置动态更新

要实现配置的动态更新,需要借助于 springboot 的 actuator 监控模块。所有需要在客户端pom文件中引入 actuator 的依赖信息。

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

配置文件 application.yml 中增加配置,将/actuator/refresh 断点暴露出来,注意不要配置在 boostrap.yml 中。

1
2
3
4
5
6
7
8
9
10
spring:
application:
name: sofa-config-client
server:
port: 8099
management:
endpoints:
web:
exposure:
include: refresh

资源类中开启更新机制,在 ConfigClientController 类中增加 @RefreshScope 注解,然后重启客户端。

首先执行:http://localhost:8099/config ,得到结果如下:

1
name:leishu@glmapper-pre ,blog:http://www.glmapper.com ,version:pre<br />

更新 github 上配置文件的值,将 name 改为 name:leishu@glmapper-pre,通过 curl 或者 postman 执行下 刷新:

1
2
> curl -X POST http://localhost:8099/actuator/refresh
> ["config.client.version","name"]%

再次刷新执行 http://localhost:8099/config ,结果如下:

1
name:leishu@glmapper-pre-update ,blog:http://www.glmapper.com ,version:pre
作者

卫恒

发布于

2018-12-31

更新于

2022-04-23

许可协议

评论