SpringCloud 笔记5

前记

上一章使用了路由网关进行路由转发和安全验证,这章将搭建分布式的配置中心。

SpringCloud Config

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在 SpringCloud 中,有分布式配置中心组件 SpringCloud Config,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在 SpringCloud Config 组件中,分两个角色:Config Server 和 Config Client。

SpringCloud Config 为服务端和客户端提供了分布式系统的外部化配置支持。配置服务器为各应用的所有环境提供了一个中心化的外部配置。它实现了对服务端和客户端对 Spring Environment 和 PropertySource 抽象的映射,所以它除了适用于 Spring 构建的应用程序,也可以在任何其他语言运行的应用程序中使用。作为一个应用可以通过部署管道来进行测试或者投入生产,我们可以分别为这些环境创建配置,并且在需要迁移环境的时候获取对应环境的配置来运行。

Config 服务端

沿用之前的套路,新增一个 Module 作为服务端,并添加依赖:

1
2
3
4
5
6
7
8
<dependencies>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

</dependencies>

之前我对上面的 config-server 依赖写上了版本号,结果一直无法访问到配置内容,直接添加就行了,会根据 springboot 的版本选择版本的。


添加注解 @EnableConfigServer 开启配置服务器功能:

1
2
3
4
5
6
7
8
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}

配置文件 application.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server.port=20000

spring.application.name=configServer

# 配置Git仓库地址
spring.cloud.config.server.git.uri=https://github.com/Zoctan/spring-cloud-demo.git
# 连接仓库超时时间
spring.cloud.config.server.git.timeout=10
# 配置仓库路径
spring.cloud.config.server.git.search-paths=config
# 配置仓库的分支,默认为master
spring.cloud.config.label=master

# 如果是私有仓库,才要填下面的信息
# 访问git仓库的用户名
#spring.cloud.config.client.git.username=your username
# 访问git仓库的用户密码
#spring.cloud.config.client.git.password=your password

SpringCloud Config也提供本地存储配置的方式:

配置文件 application.properties

1
2
3
4
5
# Config Server会默认从应用的src/main/resource目录下检索配置文件
spring.profiles.active=native

# 也可以通过该属性来指定配置文件的位置
# spring.cloud.config.server.native.search-locations=file:/home/xx/properties/

但是为了支持更好的管理内容和版本控制的功能,还是推荐使用Git的方式。


然后在项目下建立 config 文件夹,分别添加不同的配置文件,用于给客户端 configClient 进行配置(后面创建的客户端名 spring.application.name=configClient):

  • configClient.properties
  • configClient-dev.properties
  • configClient-test.properties
  • configClient-prod.properties

分别给它们添加一个 from 属性:

  • from=git-default-1.0
  • from=git-dev-1.0
  • from=git-test-1.0
  • from=git-prod-1.0

记得先将上面的配置上传到Git
比如我上面的配置在 https://github.com/Zoctan/spring-cloud-demo 的 config 目录下

  • configClient 就是 application.name,不能有不同,否则取不到配置;
  • dev | test | prod 等是 profile;
  • label 是分支的名称。

URL与配置文件的映射关系如下:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

启动该配置程序,访问:

第一种映射:/{application}/{profile}[/{label}],不注明 label 默认访问 master。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ curl http://localhost:20000/configClient/dev/
{
"name": "configClient",
"profiles": [
"dev"
],
"label": null,
"version": "0ec6f9f1f9316fdeb2c86e06d943770bf6b40528",
"state": null,
"propertySources": [
{
"name": "https://github.com/Zoctan/spring-cloud-demo.git/config/configClient-dev.properties",
"source": {
"from": "git-dev-1.0"
}
},
{
"name": "https://github.com/Zoctan/spring-cloud-demo.git/config/configClient.properties",
"source": {
"from": "git-default-1.0"
}
}
]
}

客户端

新增一个 Module,命名为 configClient,并添加依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
<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>

普通的 Boot 应用:

1
2
3
4
5
6
7
@SpringBootApplication
public class ConfigClientApplication {

public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}

配置文件 bootstrap.properties

1
2
3
4
5
6
7
8
9
10
server.port=30000

spring.application.name=configClient

# 配置服务中心网址
spring.cloud.config.uri=http://localhost:20000/
# 使用的配置
spring.cloud.config.profile=dev
# 配置仓库的分支,默认为master
spring.cloud.config.label=master

上面的属性必须配置在 bootstrap.properties 中,才能正确加载 config 内容。
因为 config 的相关配置会先于 application.properties,而 bootstrap.properties的加载也是先于 application.properties

添加控制器:

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

// 绑定配置中的from属性
@Value("${from}")
private String from;

@GetMapping("/from")
public String from() {
return String.format("I'm from %s", from);
}
}

测试:

1
2
$ curl localhost:30000/from 
I'm from git-dev-1.0

可以看到读取的就是 configClient-dev.properties 下的 from 值。

Author

Zoctan

Posted on

2018-05-29

Updated on

2023-03-14

Licensed under