在之前的 Spring Cloud 构建微服务架构: 分布式配置中心 http://blog.didispace.com/spring-cloud-starter-dalston-3/ 一文中, 我们介绍的 Spring Cloud Server 配置中心采用了 Git 的方式进行配置信息存储. 这一设计巧妙的利用 Git 自身机制以及其他具有丰富功能的 Git 服务端产品, 让 Spring Cloud Server 在配置存储和管理的上避开了很多与管理相关的复杂实现, 使其具备了配置中心存储配置和读取配置的基本能力; 而更上层的管理机制, 由于不具备普遍适用性, 所以 Spring Cloud Server 并没有自己去实现这部分内容, 而是通过 Git 服务端产品来提供一部分实现, 如果还需要更复杂的功能也能自己实现与定义. 即便如此, 对于 Spring Cloud Server 默认使用 Git 来存储配置的方案一直以来还是饱受争议. 所以, 本文将介绍一下 Spring Cloud Config 从 Edgware 版本开始新增的一种配置方式: 采用数据库存储配置信息.
构建配置中心服务端
第一步: 创建一个基础的 Spring Boot 项目, 在 pom.xml 中引入几个主要依赖:
spring-cloud-config-server
: 配置中心的基础依赖
spring-boot-starter-jdbc
: 由于需要访问数据库, 所以需要加载 jdbc 的依赖
mysql-connector-java
:MySQL 数据库的连接包
flyway-core : 该内容非强制, 主要用来管理 schema(如果您不了解可以看一下这篇文章)
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.11.RELEASE</version>
- <relativePath/>
- </parent>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-config-server</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-jdbc</artifactId>
- </dependency>
- <dependency>
- <groupId>org.flywaydb</groupId>
- <artifactId>flyway-core</artifactId>
- <version>5.0.3</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.21</version>
- </dependency>
- </dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>Edgware.SR3</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
第二步: 准备 schema 创建文件. 在 resources 下创建 schema 目录, 并加入
V1__Base_version.sql
- CREATE TABLE `properties` (
- `id` int(11) NOT NULL,
- `key` varchar(50) NOT NULL,
- `value` varchar(500) NOT NULL,
- `application` varchar(50) NOT NULL,
- `profile` varchar(50) NOT NULL,
- `label` varchar(50) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- @EnableConfigServer
- @SpringBootApplication
- public class ConfigServerBootstrap {
- public static void main(String[] args) {
- ApplicationContext context = SpringApplication.run(ConfigServerBootstrap.class);
- // 测试用数据, 仅用于本文测试使用
- JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
- jdbcTemplate.execute("delete from properties");
- jdbcTemplate.execute("INSERT INTO properties VALUES(1,'com.didispace.message','test-stage-master','config-client','stage','master')");
- jdbcTemplate.execute("INSERT INTO properties VALUES(2,'com.didispace.message','test-online-master','config-client','online','master')");
- jdbcTemplate.execute("INSERT INTO properties VALUES(3,'com.didispace.message','test-online-develop','config-client','online','develop')");
- jdbcTemplate.execute("INSERT INTO properties VALUES(4,'com.didispace.message','hello-online-master','hello-service','online','master')");
- jdbcTemplate.execute("INSERT INTO properties VALUES(5,'com.didispace.message','hello-online-develop','hello-service','online','develop')");
- }
- }
- spring.application.name=config-server-db
- server.port=10020
- spring.profiles.active=jdbc
- spring.cloud.config.server.jdbc.sql=SELECT `KEY`, `VALUE` from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
- spring.datasource.url=jdbc:mysql://localhost:3306/config-server-db
- spring.datasource.username=root
- spring.datasource.password=
- spring.datasource.driver-class-name=com.mysql.jdbc.Driver
- flyway.locations=/schema
- {
- "name": "config-client",
- "profiles": [
- "stage"
- ],
- "label": null,
- "version": null,
- "state": null,
- "propertySources": [
- {
- "name": "config-client-stage",
- "source": {
- "com.didispace.message": "test-stage-master"
- }
- }
- ]
- }
- curl http://localhost:10020/hello-service/stage/develop
- {
- "name": "hello-service",
- "profiles": [
- "online"
- ],
- "label": "develop",
- "version": null,
- "state": null,
- "propertySources": [
- {
- "name": "hello-service-online",
- "source": {
- "com.didispace.message": "hello-online-develop"
- }
- }
- ]
- }
- Github:https://github.com/dyc87112/SpringCloud-Learning/ https://github.com/dyc87112/SpringCloud-Learning/tree/master/3-Edgware
- Gitee:https://gitee.com/didispace/SpringCloud-Learning/ https://gitee.com/didispace/SpringCloud-Learning/tree/master/3-Edgware
来源: http://www.tuicool.com/articles/Zr2AfuY