在之前的《Spring Cloud构建微服务架构:分布式配置中心》一文中,我们介绍的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(如果您不了解可以看一下这篇文章)

  1. <parent>

  2.    <groupId>org.springframework.boot</groupId>

  3.    <artifactId>spring-boot-starter-parent</artifactId>

  4.    <version>1.5.11.RELEASE</version>

  5.    <relativePath/>

  6. </parent>

  7. <dependencies>

  8.    <dependency>

  9.        <groupId>org.springframework.cloud</groupId>

  10.        <artifactId>spring-cloud-config-server</artifactId>

  11.    </dependency>

  12.    <dependency>

  13.        <groupId>org.springframework.boot</groupId>

  14.        <artifactId>spring-boot-starter-jdbc</artifactId>

  15.    </dependency>

  16.    <dependency>

  17.        <groupId>org.flywaydb</groupId>

  18.        <artifactId>flyway-core</artifactId>

  19.        <version>5.0.3</version>

  20.    </dependency>

  21.    <dependency>

  22.        <groupId>mysql</groupId>

  23.        <artifactId>mysql-connector-java</artifactId>

  24.        <version>5.1.21</version>

  25.    </dependency>

  26. </dependencies>

  27. <dependencyManagement>

  28.    <dependencies>

  29.        <dependency>

  30.            <groupId>org.springframework.cloud</groupId>

  31.            <artifactId>spring-cloud-dependencies</artifactId>

  32.            <version>Edgware.SR3</version>

  33.            <type>pom</type>

  34.            <scope>import</scope>

  35.        </dependency>

  36.    </dependencies>

  37. </dependencyManagement>

第二步:准备schema创建文件。在 resources下创建 schema目录,并加入 V1__Base_version.sql文件,具体内容如下:

  1. CREATE TABLE `properties` (

  2.  `id` int(11) NOT NULL,

  3.  `key` varchar(50) NOT NULL,

  4.  `value` varchar(500) NOT NULL,

  5.  `application` varchar(50) NOT NULL,

  6.  `profile` varchar(50) NOT NULL,

  7.  `label` varchar(50) NOT NULL,

  8.  PRIMARY KEY (`id`)

  9. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

该脚本会在程序运行时由flyway自动执行

第三步:创建应用主类,具体如下:

  1. @EnableConfigServer

  2. @SpringBootApplication

  3. public class ConfigServerBootstrap {

  4.    public static void main(String[] args) {

  5.        ApplicationContext context = SpringApplication.run(ConfigServerBootstrap.class);

  6.        // 测试用数据,仅用于本文测试使用

  7.        JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);

  8.        jdbcTemplate.execute("delete from properties");

  9.        jdbcTemplate.execute("INSERT INTO properties VALUES(1, 'com.didispace.message', 'test-stage-master', 'config-client', 'stage', 'master')");

  10.        jdbcTemplate.execute("INSERT INTO properties VALUES(2, 'com.didispace.message', 'test-online-master', 'config-client', 'online', 'master')");

  11.        jdbcTemplate.execute("INSERT INTO properties VALUES(3, 'com.didispace.message', 'test-online-develop', 'config-client', 'online', 'develop')");

  12.        jdbcTemplate.execute("INSERT INTO properties VALUES(4, 'com.didispace.message', 'hello-online-master', 'hello-service', 'online', 'master')");

  13.        jdbcTemplate.execute("INSERT INTO properties VALUES(5, 'com.didispace.message', 'hello-online-develop', 'hello-service', 'online', 'develop')");

  14.    }

  15. }

这里增加了一些测试用数据,以便于后续的配置读取验证。

第四步:配置 application.properties,具体内容如下:

  1. spring.application.name=config-server-db

  2. server.port=10020

  3. spring.profiles.active=jdbc

  4. spring.cloud.config.server.jdbc.sql=SELECT `KEY`, `VALUE` from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?

  5. spring.datasource.url=jdbc:mysql://localhost:3306/config-server-db

  6. spring.datasource.username=root

  7. spring.datasource.password=

  8. spring.datasource.driver-class-name=com.mysql.jdbc.Driver

  9. flyway.locations=/schema

这里主要涉及几个配置:

  • spring.profiles.active=jdbc:必须设置,将配置中心的存储实现切换到jdbc的方式

  • spring.cloud.config.server.jdbc.sql:非必须,这里由于采用mysql数据源, key、 value是保留关键词,原生的实现语句会报错,所以需要重写一下这句查询语句(如果存储的表结构设计不同于上面准备的内容,也可以通过这个属性的配置来修改配置的获取逻辑)

  • spring.datasource.*:存储配置信息的数据源配置,这里采用mysql,开发者根据自己实际情况修改

  • flyway.locations:flyway加载schema创建sql的位置

服务端配置验证

完成了上一节内容之后,我们就已经构建一个通过数据酷来存储配置内容的配置中心了,下面我们可以通过配置中心暴露的端点来尝试读取配置。

第一步:先将上面构建的配置中心启动起来。

第二步:验证配置信息获取:

  • curl http://localhost:10020/config-client/stage/,获取信息 config-client服务 stage环境的配置内容,根据上面的数据准备,我们会获得如下返回内容:

  1. {

  2.    "name": "config-client",

  3.    "profiles": [

  4.    "stage"

  5.    ],

  6.    "label": null,

  7.    "version": null,

  8.    "state": null,

  9.    "propertySources": [

  10.        {

  11.            "name": "config-client-stage",

  12.            "source": {

  13.            "com.didispace.message": "test-stage-master"

  14.            }

  15.        }

  16.    ]

  17. }

  • curl http://localhost:10020/hello-service/stage/develop,获取信息 hello-service服务, stage环境, develop标签的配置内容,根据上面的数据准备,我们会获得如下返回内容:

  1. {

  2.    "name": "hello-service",

  3.    "profiles": [

  4.        "online"

  5.    ],

  6.    "label": "develop",

  7.    "version": null,

  8.    "state": null,

  9.    "propertySources": [

  10.        {

  11.            "name": "hello-service-online",

  12.            "source": {

  13.                "com.didispace.message": "hello-online-develop"

  14.            }

  15.        }

  16.    ]

  17. }

关于如何访问Spring Cloud Config构建配置中心获取配置信息的详细内容 ,可以查看前文:《Spring Cloud构建微服务架构:分布式配置中心》,本文不做详细介绍。

总结

本文主要具体介绍了在Spring Cloud Config在Edgware版本开始新增的JDBC存储的使用思路,具体使用实际上还有很多可以优化的空间,比如:索引的优化、查询语句的优化;如果还需要进一步定制管理,对于表结构的优化也是很有必要的。

最后,安利一个基于Spring Cloud Config的配置管理项目:https://github.com/dyc87112/spring-cloud-config-admin,正在紧锣密鼓的开发中,尽情期待!

本文示例

读者可以根据喜好选择下面的两个仓库中查看 config-server-db和 config-client两个项目:

  • Github:https://github.com/dyc87112/SpringCloud-Learning/

  • Gitee:https://gitee.com/didispace/SpringCloud-Learning/

- END -

 往期推荐:

  • 死磕Java系列:

  1. 深入分析ThreadLocal

  2. 深入分析synchronized的实现原理

  3. 深入分析volatile的实现原理

  4. Java内存模型之happens-before

  5. Java内存模型之重排序

  6. Java内存模型之分析volatile

  7. Java内存模型之总结

  8. J.U.C之AQS简介

  9. J.U.C之AQS:CLH同步队列

  10. J.U.C之AQS同步状态的获取与释放

  11. J.U.C之AQS阻塞和唤醒线程

  12. J.U.C之重入锁:ReentrantLock

  13. J.U.C之读写锁:ReentrantReadWriteLock

  14. J.U.C之Condition

  15. J.U.C之并发工具类:CyclicBarrier

  16. J.U.C之并发工具类:Semaphore

  17. J.U.C之并发工具类:CountDownLatch

  18. J.U.C之并发工具类:Exchanger

……

  • Spring系列:

  1. Spring Cloud Zuul中使用Swagger汇总API接口文档

  2. Spring Cloud Config Server迁移节点或容器化带来的问题

  3. Spring Cloud Config对特殊字符加密的处理

  4. Spring Boot使用@Async实现异步调用:使用Future以及定义超时

  5. Spring Cloud构建微服务架构:分布式配置中心(加密解密)

  6. Spring Boot快速开发利器:Spring Boot CLI

……

可关注我的公众号

深入交流、更多福利

扫码加入我的知识星球

点击“阅读原文”,看本号其他精彩内容

Spring Cloud Config采用数据库存储配置内容相关推荐

  1. Spring Cloud Config采用数据库存储配置内容【Edgware+】

    在之前的<Spring Cloud构建微服务架构:分布式配置中心>一文中,我们介绍的Spring Cloud Server配置中心采用了Git的方式进行配置信息存储.这一设计巧妙的利用Gi ...

  2. Spring Cloud Config采用Git存储时两种常用的配置策略

    由于Spring Cloud Config默认采用了Git存储,相信很多团队在使用Spring Cloud的配置中心时也会采用这样的策略.即便大家都使用了Git存储,可能还有各种不同的配置方式,本文就 ...

  3. 为Spring Cloud Config插上管理的翅膀

    最近一致在更新Spring Cloud Config的相关内容,主要也是为这篇埋个伏笔,相信不少调研过Spring Cloud Config的用户都会吐槽它的管理能力太弱.因此,就有了下面为讲推荐的这 ...

  4. spring cloud config将配置存储在数据库中

    点击上方"方志朋",选择"置顶或者星标" 你的关注意义重大! Spring Cloud Config Server最常见是将配置文件放在本地或者远程Git仓库, ...

  5. spring cloud config将配置存储在数据库中 1

    转载请标明出处: https://blog.csdn.net/forezp/... 本文出自方志朋的博客 Spring Cloud Config Server最常见是将配置文件放在本地或者远程Git仓 ...

  6. 分布式配置中心:spring cloud config

    分布式配置中心:spring cloud config 前言 ​ Spring Cloud Config是Spring Cloud团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集 ...

  7. 【SpringCloud】四、Spring Cloud Config

    Spring Cloud Config 前言 一.什么是配置中心 1. 为什么需要分布式配置中心 2.常用分布式配置中心框架 二.什么是Spring Cloud Config? 1.Springclo ...

  8. spring cloud 入门系列七:基于Git存储的分布式配置中心--Spring Cloud Config

    我们前面接触到的spring cloud组件都是基于Netflix的组件进行实现的,这次我们来看下spring cloud 团队自己创建的一个全新项目:Spring Cloud Config. 它用来 ...

  9. Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!

    本文导读: Spring Cloud Config 基本概念 Spring Cloud Config 客户端加载流程 Spring Cloud Config 基于消息总线配置 Spring Cloud ...

最新文章

  1. Sap Byd Soap使用 SSL 客户端证书
  2. NR 5G RRC无线资源控制
  3. [YTU]_2803( 判断字符串是否为回文)
  4. python释放变量内存_Python尚学堂高淇|1113引用的本质栈内存,堆内存,内存的示意图,标识符,变量的声明初始化,垃圾回收机制...
  5. WindowsServer2012 R2 64位中文标准版(IIS8.5)下手动搭建PHP环境详细图文教程(二)安装IIS8.5...
  6. 美图HTTPS优化探索与实践
  7. HBase+Phoenix整合入门--集群搭建
  8. 深入理解消息队列(场景,对比,原理和设计思想)
  9. 工作流Flowable实战篇
  10. nginx 413 Request Entity Too Large
  11. linux系统ftp优化,Linux vsftp 部署优化
  12. 深入浅出VMware——虚拟机暂停后生成的文件
  13. EasyUI - 操作 Tree 控件
  14. MapReduce:详解Shuffle过程
  15. R2B fpga flow script
  16. pp助手苹果版_吃药提醒助手ios版下载-吃药提醒助手苹果版下载v1.0
  17. 现代办公自动化教程 计算机基础教育系列,新编办公自动化综合应用教程 高职计算机大类专业基础课 林婧 朱强第1章 现代办公自动化基础.ppt...
  18. 计算机及网络维护工程师专业问题
  19. “开心网”10亿“卖身” !
  20. EXCEL如何将平均值加减标准差设置为科学计数法显示

热门文章

  1. apache solr远程代码执行漏洞(cve-2019-0193)
  2. linux c ping实现
  3. Linux 网络编程—— libpcap 详解
  4. 为学Linux,我看了这些书
  5. Java学习之do-while-if语句实操
  6. 算法 - 冒泡排序(C#)
  7. Linux内核之时间系统
  8. 订单信息修改java模型图,java毕业设计_springboot框架的物流运输管理系统订单管理...
  9. mysql 5.7 存储引擎_mysql5.7——innodb存储引擎总结
  10. #修改margin_springboot+jpa+tymeleaf实现信息修改功能