Spring Cloud 系列之配置中心 Config
1.1 简介
1.1.1 概述
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在 Spring Cloud 中,有分布式配置中心组件 Spring Cloud Config,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程 git 仓库中。Spring Cloud Config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
在 Spring Cloud Config 组件中,分服务端与客户端,服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用 git 来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过 git 客户端工具来方便的管理和访问配置内容。
1.1.2 相关依赖
<!-- 服务端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- 客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
1.1.3 bootstrap.yml
bootstrap.yml 可以理解成系统级别的一些参数配置,这些参数一般是不会变动的。application.yml 可以用来定义应用级别的,如果搭配 Spring Cloud Config 使用 application.yml 里面定义的文件可以实现动态替换。bootstrap.yml 先于 application.yml 加载
1.2 配置中心(config server)
1.2.1 创建 git 配置仓库
GitHub 在国内的速度比较慢我们这里使用 Gitee 创建 ☞ 地址。注意配置文件的命名规则要遵循官网给出的 xxx-xxx 格式,不要随意取名。
1.2.2 创建基础项目工程
☞ 项目地址
1.2.3 Config 配置文件
spring:
cloud:
config:
server:
git:
# 仓库地址
uri: https://gitee.com/java-software/spring-cloud-config.git
# 以下配置可以不配
# 登录账户
username: username
# 登录密码
password: password
# 配置文件分支, 不配默认 master
default-label: master
# 配置文件所在根目录, 不配默认当前根目录
search-paths: spring-cloud-config
1.2.4 Config 启动类
/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/11/10 * @description */
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
1.2.5 访问配置文件
如下所示 Spring Cloud Config 定义了一套访问规则,我们可以在浏览器直接访问到 git 上的配置文件,一般使用 /{application}-{profile}.yml
、/{label}/{application}-{profile}.yml
两种,一种默认 master 分支,一种可以指定分支。
我们使用 postman 访问,可以发现访问存在的配置文件会返回详细配置,访问不存在的配置文件会返回空。注意这里是从配置中心获取的,而不是直接从 git 仓库获取的。
我们修改以下 git 上面的配置文件,将器 version 改为 2,刷新请求可以发现获取到的配置文件已经更新,说明配置中心的配置与 git 上面的同步。
1.2.6 其他配置
spring:
profiles:
# 修改为本地配置, 默认是 git
active: native
cloud:
config:
server:
native:
# 指定本地文件夹路径
search-locations: file:C:\Users\softw\Desktop\config
# 开启安全验证,需要引入 security 依赖才可使用,注意可能需要关闭 csrf
spring:
security:
user:
name: root
password: root
1.3 config client
1.3.1 配置文件
# 配置方式一
spring:
cloud:
config:
name: config
# 若是又多个可用逗号分开,config-mysql.yml config-mq.yml ☞ mysql,mq
profile: mysql
label: master
# 可以使用 http://user:password@127.0.0.1:8010 提供账户也可单独提供
uri: http://127.0.0.1:8010
# 若 config 开启了密码验证则需要配置服务器的用户名密码,此配置会覆盖 uri 中的配置
username: root
password: root
# 配置方式二,使用注册中心可用
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server
name: config
profile: mysql
label: master
1.3.2 启动类
/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/11/10 * @description */
@SpringBootApplication
@EnableDiscoveryClient
public class InfoApplication {
public static void main(String[] args) {
SpringApplication.run(InfoApplication.class, args);
}
}
1.3.3 请求服务
请求业务模块,返回数据表明该服务已经获取到配置中心的配置,现在我们将 git 上配置文件中 version 修改为 3,看该服务是否会同步更新。我们发现配置中心的更新了但是客户端(即该服务)没有更新。
重启该服务发现配置更新了,这是由于服务启动时从配置中心拿过来配置文件,但是之后配置中心更新了,客户端不知道文件更新了依旧使用旧的配置文件。那么我们之后每次修改配置文件之后岂不是都需要重启服务?这也太麻烦了。
1.4 Config 动态刷新
1.4.1 手动刷新
☞ 添加 actuator 监控
<!-- 刷新服务由 actuator 组件提供 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
☞ 暴露监控端口
management:
endpoints:
web:
exposure:
include: "*"
☞ 增加刷新注解
/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/11/10 * @description info 模块控制层 */
@RestController
@RefreshScope // 这个注解会给范围内的每个 bean 创建个代理对象,如果刷新 bean,则下次访问 bean 时将创建一个新实例
@RequestMapping("/info")
public class InfoController {
@Value("${sql.name}")
private String name;
@Value("${sql.version}")
private String version;
@GetMapping("/get")
public Object get() {
return "名称:" + name + ", 版本:" + version;
}
}
☞ 请求服务
what? 以上配置都加了,为什么还是没有更新?还需要运维人员发送 POST 请求取通知服务去更新配置。
这里我们可以使用 postman 工具去请求 http://127.0.0.1:8081/actuator/refresh
通知服务进行更新,也可使使用 curl 命令 curl -X POST "http://127.0.0.1:8081/actuator/refresh"
去通知服务更新。更新完成后就可以获取最新的配置了。但是这样一旦服务多了也很麻烦,当然一个脚本也能解决。
1.4.2 自动刷新
凭 Spring Cloud Config 自身暂时没有办法完成自动刷新,所以消息总线诞生了,二者是一对双生子不离不弃,Spring Cloud Config 通过 Spring Cloud Bus 去通知各个服务刷新配置,详情见 ☞ Spring Cloud 系列之消息总线 Bus
☞ 源码
还没有评论,来说两句吧...