Spring Cloud Config Server 最常见是将配置文件放在本地或者远程 Git 仓库, 放在本地是将将所有的配置文件统一写在 Config Server 工程目录下, 如果需要修改配置, 需要重启 config server; 放在 Git 仓库, 是将配置统一放在 Git 仓库, 可以利用 Git 仓库的版本控制. 本文将介绍使用另外一种方式存放配置信息, 即将配置存放在 MySQL 中.
整个流程: Config Sever 暴露 Http API 接口, Config Client 通过调用 Config Sever 的 Http API 接口来读取配置 Config Server 的配置信息, Config Server 从数据中读取具体的应用的配置. 流程图如下:
案例实战
在本案例中需要由 2 个工程, 分为 config-server 和 config-client, 其中 config-server 工程需要连接 MySQL 数据库, 读取配置; config-client 则在启动的时候从 config-server 工程读取. 本案例 Spring Cloud 版本为 Greenwich.RELEASE,Spring Boot 版本为 2.1.0.RELEASE.
工程 | 描述 |
---|---|
config-server | 端口 8769,从数据库中读取配置 |
config-client | 端口 8083,从 config-server 读取配置 |
搭建 config-server 工程
创建工程 config-server, 在工程的 pom 文件引入 config-server 的起步依赖, MySQL 的连接器, jdbc 的起步依赖, 代码如下:
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-config-server</artifactId>
- </dependency>
- <dependency>
- <groupId>MySQL</groupId>
- <artifactId>MySQL-connector-java</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-jdbc</artifactId>
- </dependency>
在工程的配置文件 application.YAML 下做以下的配置:
- spring:
- profiles:
- active: jdbc
- application:
- name: config-jdbc-server
- datasource:
- url: jdbc:MySQL://127.0.0.1:3306/config-jdbc?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=GMT+8
- username: root
- password: 123456
- driver-class-name: com.MySQL.jdbc.Driver
- cloud:
- config:
- label: master
- server:
- jdbc: true
- server:
- port: 8769
- spring.cloud.config.server.jdbc.sql: SELECT key1, value1 from config_properties where APPLICATION=? and PROFILE=? and LABEL=?
其中, spring.profiles.active 为 spring 读取的配置文件名, 从数据库中读取, 必须为 jdbc.spring.datasource 配置了数据库相关的信息, spring.cloud.config.label 读取的配置的分支, 这个需要在数据库中数据对应. spring.cloud.config.server.jdbc.sql 为查询数据库的 sql 语句, 该语句的字段必须与数据库的表字段一致.
在程序的启动文件 ConfigServerApplication 加上 @EnableConfigServer 注解, 开启 ConfigServer 的功能, 代码如下:
- @SpringBootApplication
- @EnableConfigServer
- public class ConfigServerApplication {
- public static void main(String[] args) {
- SpringApplication.run(ConfigServerApplication.class, args);
- }
- }
初始化数据库
由于 Config-server 需要从数据库中读取, 所以读者需要先安装 MySQL 数据库, 安装成功后, 创建 config-jdbc 数据库, 数据库编码为 utf-8, 然后在 config-jdbc 数据库下, 执行以下的数据库脚本:
- CREATE TABLE `config_properties` (
- `id` bigint(20) NOT NULL AUTO_INCREMENT,
- `key1` varchar(50) COLLATE utf8_bin NOT NULL,
- `value1` varchar(500) COLLATE utf8_bin DEFAULT NULL,
- `application` varchar(50) COLLATE utf8_bin NOT NULL,
- `profile` varchar(50) COLLATE utf8_bin NOT NULL,
- `label` varchar(50) COLLATE utf8_bin DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
其中 key1 字段为配置的 key,value1 字段为配置的值, application 字段对应于应用名, profile 对应于环境, label 对应于读取的分支, 一般为 master.
插入数据 config-client 的 2 条数据, 包括 server.port 和 foo 两个配置, 具体数据库脚本如下:
- insert into `config_properties` (`id`, `key1`, `value1`, `application`, `profile`, `label`) values('1','server.port','8083','config-client','dev','master');
- insert into `config_properties` (`id`, `key1`, `value1`, `application`, `profile`, `label`) values('2','foo','bar-jdbc','config-client','dev','master');
搭建 config-client
在 config-client 工程的 pom 文件, 引入 web 和 config 的起步依赖, 代码如下:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-Web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-config</artifactId>
- </dependency>
在程序的启动配置文件 Bootstrap.YAML 做程序的相关配置, 一定要是 Bootstrap.YAML, 不可以是 application.YAML,Bootstrap.YAML 的读取优先级更高, 配置如下:
- spring:
- application:
- name: config-client
- cloud:
- config:
- uri: http://localhost:8769
- fail-fast: true
- profiles:
- active: dev
其中 spring.cloud.config.uri 配置的 config-server 的地址, spring.cloud.config.fail-fast 配置的是读取配置失败后, 执行快速失败. spring.profiles.active 配置的是 spring 读取配置文件的环境.
在程序的启动文件 ConfigClientApplication, 写一个 RestAPI, 读取配置文件的 foo 配置, 返回给浏览器, 代码如下:
- @SpringBootApplication
- @RestController
- public class ConfigClientApplication {
- public static void main(String[] args) {
- SpringApplication.run(ConfigClientApplication.class, args);
- }
- @Value("${foo}")
- String foo;
- @RequestMapping(value = "/foo")
- public String hi(){
- return foo;
- }
- }
依次启动 2 个工程, 其中 config-client 的启动端口为 8083, 这个是在数据库中的, 可见 config-client 从 config-server 中读取了配置. 在浏览器上访问 http://localhost:8083/foo, 浏览器显示 bar-jdbc, 这个是在数据库中的, 可见 config-client 从 config-server 中读取了配置.
参考资料
源码下载
扫一扫, 支持下作者吧
来源: https://juejin.im/post/5c6e827ae51d452da9672094