在前面文章基础上进行,请参考:

Spring Cloud config ------ 基础使用

Spring Cloud config ------ 认证和安全

Spring Cloud config ------ 动态更新及spring cloud bus优化

大多数教程和项目中一般使用git或svn等作为配置中心的配置库,方便进行一些集中管理和版本控制。但也有项目需求是使用jdbc的方式进行配置管理,例如把服务的配置放在mysql里,这样的好处就是,可以针对配置中心,方便开发出一些对外接口,例如一些用户可配置的动态改更新的参数。

本文介绍jdbc方式的配置库,所有的更改和操作只与配置中心服务端有关,以下均指服务端操作。

一 jdbc(mysql)配置库

bootstrap.properties中配置如下:

spring.profiles.active=jdbc

表示使用jdbc的方式进行配置管理,在application.properties中配置数据库信息如下:

#数据库配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/spring_cloud?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password={cipher}AQCPxBIKwfqAxlCZa7zHQyWheWTd7/6Ym0shJ8MO9KzT4eRE3dFUesuOzBf3PqZMZsJxwoXLm9IFsh57nOz4DP7SXSPlznz9+544jCgXFeNnZ2wv45qPxP6E3iOln1ei3WYLiRHQW0eDLyEOI7eYSNK4EGI/EgUpz1Iwpz5uuepj7HYqN5wqAyPHNZk0raEtINDyc9uHENy9J8h3QnNT7S6cL7CDeN97xpFSL6C6Xm0Xt6rnmKkUTd5kl3LZk0HihL6dUT6NsEzNwul0+l0CxJg3PcuxFTUWMOQ8QT8HU/2o4zv0LKiLA0sMl1PKLssglAyUQ8/kuE73KkoJ9/TDqh3/1h0WKtbYJw39hBlazD2uugKje68dcuPaiv7/ryDA1Hg=
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.cloud.config.server.jdbc.sql=select keye,valuee from properties where application=? and profilee=? and label=?

由于需要查询数据库,所以需要添加数据相关依赖:

        <!--####################################数据库#########################################--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency>

使用jpa进行数据库操作,新建entity类Properties,如下:

@Entity
@GenericGenerator(name = "jpa-uuid",strategy = "uuid")
public class Properties {@Id@GeneratedValue(generator = "jpa-uuid")private String uid;private String application;private String profilee;private String label;private String keye;private String valuee;public String getUid() {return uid;}public void setUid(String uid) {this.uid = uid;}public String getApplication() {return application;}public void setApplication(String application) {this.application = application;}public String getLabel() {return label;}public void setLabel(String label) {this.label = label;}public String getProfilee() {return profilee;}public void setProfilee(String profilee) {this.profilee = profilee;}public String getKeye() {return keye;}public void setKeye(String keye) {this.keye = keye;}public String getValuee() {return valuee;}public void setValuee(String valuee) {this.valuee = valuee;}
}

需包含字段:application、profile、label、key、value(profile、key、value是mysql的保留关键字,所以都加了个e)。

最重要的配置就是application.properties中的配置:

spring.cloud.config.server.jdbc.sql=select keye,valuee from properties where application=? and profilee=? and label=?

意思就是:配置中心客户端实例获取配置时,服务端怎么去查询其相关的配置(application、profile、label唯一确定一个客户端配置)。

从上面的信息来看,数据库的配置表名称以及字段都可以随便定义,是需要在spring.cloud.config.server.jdbc.sql中指明即可。

启动服务端以及客户端,调用客户端的测试接口即可获取mysql中的相关配置。

二 自定义jdbc获取配置方式

从上面的信息看出,我们只是配了profile为jdbc方式,然后配了数据库连接信息以及配置表和查询sql,我们知道,查询数据库是需要相关的类来支撑的,比如jdbctemplate,jpa的repository等,那么我们没有配这些信息,它是怎么去查询数据库配置表的呢?

自问自答,就是用了JdbcEnvironmentRepository,源码也很简单:

public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered {private int order;private final JdbcTemplate jdbc;private String sql;private final PropertiesResultSetExtractor extractor = new PropertiesResultSetExtractor();public JdbcEnvironmentRepository(JdbcTemplate jdbc, JdbcEnvironmentProperties properties) {this.jdbc = jdbc;this.order = properties.getOrder();this.sql = properties.getSql();}public void setSql(String sql) {this.sql = sql;}public String getSql() {return this.sql;}public Environment findOne(String application, String profile, String label) {String config = application;if(StringUtils.isEmpty(label)) {label = "master";}if(StringUtils.isEmpty(profile)) {profile = "default";}if(!profile.startsWith("default")) {profile = "default," + profile;}String[] profiles = StringUtils.commaDelimitedListToStringArray(profile);Environment environment = new Environment(application, profiles, label, (String)null, (String)null);if(!application.startsWith("application")) {config = "application," + application;}ArrayList applications = new ArrayList(new LinkedHashSet(Arrays.asList(StringUtils.commaDelimitedListToStringArray(config))));ArrayList envs = new ArrayList(new LinkedHashSet(Arrays.asList(profiles)));Collections.reverse(applications);Collections.reverse(envs);Iterator var9 = applications.iterator();while(var9.hasNext()) {String app = (String)var9.next();Iterator var11 = envs.iterator();while(var11.hasNext()) {String env = (String)var11.next();Map next = (Map)this.jdbc.query(this.sql, new Object[]{app, env, label}, this.extractor);if(!next.isEmpty()) {environment.add(new PropertySource(app + "-" + env, next));}}}return environment;}public int getOrder() {return this.order;}public void setOrder(int order) {this.order = order;}
}

集成了EnvironmentRepository, Ordered接口,实现了EnvironmentRepository的findOne方式,此方法就是怎么去获取某服务的配置项的,也可以看到是使用了jdbctemplate去进行数据库查询的。构造函数中获取参数JdbcEnvironmentProperties的order和sql信息,源码如下:

public class JdbcEnvironmentProperties implements EnvironmentRepositoryProperties {private static final String DEFAULT_SQL = "SELECT KEY, VALUE from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?";private int order = 2147483637;private String sql = "SELECT KEY, VALUE from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?";public JdbcEnvironmentProperties() {}public int getOrder() {return this.order;}public void setOrder(int order) {this.order = order;}public String getSql() {return this.sql;}public void setSql(String sql) {this.sql = sql;}
}

看到这里是不是已经很清晰明了了,sql就是我们application.properties中配置的sql。所以如果application中不进行配置,数据库字段需要和此源码保持一致。

因此,如果项目有特殊需求,也可以自定义类实现EnvironmentRepository, Ordered接口,我们有个项目使用了Brixton.SR5版本,而且需要使用mysql进行配置管理,而Brixton.SR5版还没有JdbcEnvironmentRepository的此种实现方式,因此是自定义了自己的类来做的。

具体代码参考:github

Spring Cloud config ------ jdbc(mysql)配置库相关推荐

  1. Go微服务 - 第八部分 - 使用Viper和Spring Cloud Config进行集中配置

    第八部分: Go微服务 - 使用Viper和Spring Cloud Config进行集中配置 在第八部分,我们探索Go微服务中使用Spring Cloud Config进行集中配置. 简介 考虑到微 ...

  2. Spring Cloud Config服务端配置细节(一)

    上篇文章我们看了Spring Cloud中分布式配置中心的一个基本使用,这里边还涉及到许多细节,本文我们就来看看服务端配置中的一些细节. 本文是Spring Cloud系列的第二十三篇文章,了解前二十 ...

  3. Spring Cloud Config服务端配置细节(二)之加密解密

    在微服务架构中,由于独立的服务个数众多,加上前期测试工作量大,一些原本由运维人员维护的敏感信息会被我们直接写在微服务中,以提高开发效率,但是这种明文存储方式显然是非常危险的,所以我们要对这些信息进行加 ...

  4. (十九)Alian 的 Spring Cloud Config 集群配置

    目录 一.简介 1.1.第一步 二.maven依赖 三.配置 3.1.application.properties 3.2.主类 四.客户端修改(支付系统) 4.1 maven依赖 4.2 支付系统主 ...

  5. Spring Cloud Config 集中式配置

    2019独角兽企业重金招聘Python工程师标准>>> 本指南通过Spring cloud config服务器引导你建立和消费配置. 你要构建什么? 你需要设置一个config服务器 ...

  6. 第十二章:Spring Cloud Config Server 的配置

    ###1.为什么要使用config 集中管理 不通环境不通配置 运行期间动态调整配置 自动刷新 ###2.用法入门 导入pom <dependency><groupId>org ...

  7. javaconfig配置mysql_spring cloud config使用mysql存储配置文件

    spring cloud config使用mysql存储配置文件 1.结构图 2.pom.xml: 4.0.0 com.didispace config-server-db 1.0.0 jar con ...

  8. 《深入理解 Spring Cloud 与微服务构建》第十三章 配置中心 Spring Cloud Config

    <深入理解 Spring Cloud 与微服务构建>第十三章 配置中心 Spring Cloud Config 文章目录 <深入理解 Spring Cloud 与微服务构建>第 ...

  9. (七)Alian 的 Spring Cloud Config 配置中心(客户端)

    目录 一.背景 二.maven依赖 三.配置文件 四.验证 一.背景   通过上一篇文章,我们已经搭建了配置中心了,接下里我们继续改造我们的订单服务了,之前我们的订单服务的数据库配置还是写在配置文件中 ...

最新文章

  1. win2008server设置问题
  2. docker容器时区与宿主机不一致的解决方法
  3. 【 C 】回调函数简记
  4. 企业级服务器固态硬盘,用户如何选择企业级SSD?_Intel服务器CPU_企业存储技术与评测-中关村在线...
  5. 微信小程序走出国门,国际化将指日可待?
  6. OBTW的完整形式是什么?
  7. Mysql limit 子查询
  8. c语言编译说文件不存在,c语言编译时缺少头文件,库的解决办法
  9. 语音识别芯片的工作原理和分类
  10. Docker详解(十四)——Docker网络类型详解
  11. Java商城 架构演化
  12. android手机内存其他文件夹里,别再胡乱清理手机内存了,1秒清空这些文件夹,手机瞬间腾出50G...
  13. oracle数据库中的同义词,Oracle创建数据库同义词
  14. 表格thead设置border无效的原因之一
  15. 密码学系列之四:一文搞懂序列密码
  16. Excel 序号自动增长,变更
  17. QtCreator添加文件夹
  18. 亲测-分享最新微信付费进群收费进群系统源码-附带搭建教
  19. android openCV检测图像的基本特征,包括Canny边缘检测、Harris角点检测、霍夫直线检测-基于Android studio
  20. 【LaTeX教程】九.Latex常见数学公式模板

热门文章

  1. docker-compose搭建seafile开源版私有云盘
  2. HarmonyOS实战 —基于hi3861芯片鸿蒙2.0的避坑指南
  3. 【数据应用技巧】基于快速GeoHash,实现海量商品与商圈的高效匹配
  4. Soul API 网关源码学习《二》
  5. 按年份、季度、月份、日期查询SQL语句
  6. 什么是P2P,O2O,B2B,B2C,C2C模式
  7. 第二本第七章 Linux无人值守安装脚本kickstart
  8. android pmem内存,android内存管理-ION/PMEM【转】
  9. 【附源码】计算机毕业设计java智能仓储设备管理系统设计与实现
  10. J2SE-Java基础