Spring Cloud 配置中心采用数据库存储配置内容

转自:Spring Cloud Config采用数据库存储配置内容【Edgware+】

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;

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

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

@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')");
} }

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

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

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

这里主要涉及几个配置:

  • spring.profiles.active=jdbc:必须设置,将配置中心的存储实现切换到jdbc的方式
  • spring.cloud.config.server.jdbc.sql:非必须,这里由于采用mysql数据源,keyvalue是保留关键词,原生的实现语句会报错,所以需要重写一下这句查询语句(如果存储的表结构设计不同于上面准备的内容,也可以通过这个属性的配置来修改配置的获取逻辑)
  • spring.datasource.*:存储配置信息的数据源配置,这里采用mysql,开发者根据自己实际情况修改
  • flyway.locations:flyway加载schema创建sql的位置

服务端配置验证

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

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

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

  • curl http://localhost:10020/config-client/stage/,获取信息config-client服务stage环境的配置内容,根据上面的数据准备,我们会获得如下返回内容:
{
"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,获取信息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"
}
}
]
}

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

总结

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

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

本文示例

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

springcloud(六):Spring Cloud 配置中心采用数据库存储配置内容的更多相关文章

  1. springcloud(五):Spring Cloud 配置中心的基本用法

    Spring Cloud 配置中心的基本用法 1. 概述 本文介绍了Spring Cloud的配置中心,介绍配置中心的如何配置服务端及配置参数,也介绍客户端如何和配置中心交互和配置参数说明. 配置中心 ...

  2. Spring Cloud配置中心(Config)

    Spring Cloud配置中心(Config) Spring Cloud是现在流行的分布式服务框架,它提供了很多有用的组件.比如:配置中心.Eureka服务发现. 消息总线.熔断机制等. 配置中心在 ...

  3. spring cloud 配置中心

    1. spring cloud配置中心server 1.1 创建git仓库 首先在github上搭建一个存储配置中心的仓库,需要创建两个分支,一个是master,一个是dev分支.自己学习可以用公开库 ...

  4. 记录一个 spring cloud 配置中心的坑,命令行端口参数无效,被覆盖,编码集问题无法读取文件等.

    spring cloud 配置中心 结合GIT , 可以运行时更新配置文件.发送指令让应用重新读取配置文件. 最近在测试服务器实现了一套,结果CPU 实用率暴增,使用docker compose启动 ...

  5. Spring Cloud Config采用数据库存储配置内容

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

  6. (七)Spring Cloud 配置中心config

      spring cloud config是一个基于http协议的远程配置实现方式. 通过统一的配置管理服务器进行配置管理,客户端通过http协议主动的拉取服务的的配置信息,完成配置获取. 下面我们对 ...

  7. Spring Cloud配置中心搭建(集成Git)

    1. 在Github(或其他)创建配置中心仓库bounter-config-repo,然后在仓库创建两个配置文件:simon.properties.susan.properties,链接如下: htt ...

  8. Spring Cloud配置中心客户端读取配置

    微服务连接配置中心来实现外部配置的读取. 引入依赖 <dependencies> <dependency> <groupId>org.springframework ...

  9. Spring Cloud配置中心内容加密

    从配置获取的配置默认是明文的,有些像数据源这样的配置需要加密的话,需要对配置中心进行加密处理. 下面使用对称性加密来加密配置,需要配置一个密钥,当然也可以使用RSA非对称性加密,但对称加密比较方便也够 ...

随机推荐

  1. SVG和canvas渲染的性能比较

    1.什么是SVG? 描述: 一种使用XML描述的2D图形的语言 SVG基于XML意味着,SVG DOM中的每个元素都是可用的,可以为某个元素附加Javascript事件处理器. 在 SVG 中,每个被 ...

  2. 解决VS2008,重新生成解决方案,很慢

    正所谓:“工欲善其事,必先利其器“.我也算是深受其害了,特把经验分享出来为大伙分忧! 在刚来公司的时候,使用的公司提供的VS2008作为开发工具,有一个非常让人不爽的问题,就是在重新编译代码(重新生成 ...

  3. 简洁实用Socket框架DotNettySocket

    目录 简介 产生背景 使用方式 TcpSocket WebSocket UdpSocket 结尾 简介 DotNettySocket是一个.NET跨平台Socket框架(支持.NET4.5+及.NET ...

  4. Compatibility模式安装windows7后改为AHCI模式无法启动Windows7的解决办法

    在用Compatibility模式安装Windows 7后,再在BIOS中去开启SATA硬盘的AHCI功能的话,就会出现无法启动的情况.只有改回Compatibility模式后,系统才恢复正常.经过试 ...

  5. JS扫雷小游戏

    HTML代码 <title> 扫雷 </title> <!-- ondragstart:防拖拽生成新页面 oncontextmenu:屏蔽右键菜单--> <b ...

  6. ZooKeeper实现生产-消费者队列

    [欢迎关注公众号:程序猿讲故事 (codestory),及时接收最新文章] 生产-消费者队列,用于多节点的分布式数据结构,生产和消费数据.生产者创建一个数据对象,并放到队列中:消费者从队列中取出一个数 ...

  7. 用原生JS实现AJAX和JSONP

    前端开发在需要与后端进行数据交互时,为了方便快捷,都会选择JQuery中封装的AJAX方法,但是有些时候,我们只需要JQuery的AJAX请求方法,而其他的功能用到的很少,这显然是没必要的.其实,原生 ...

  8. 【C语言基础】unsigned short类型用于循环的一个难点

    我在我的知识星球:“C语言解惑课堂”里的第一篇提出一个问题:[第1篇][C语言基础][unsigned short类型用于循环的一个难点]要查看更多的C语言难点解析或者需要提问的同学,微信扫扫文末我的 ...

  9. Linux配置部署_新手向(一)——CentOS系统安装

    目录 前言 VMware 开始安装 系统安装 小结 @ 前言 最近忙过一件人生大事之后,终于稍微有点时间鼓捣东西,之前net core相关的基础已经鼓捣的差不多了,既然net core跨平台,那就来体 ...

  10. css布局之居中

    CSS布局之居中 本文主要是介绍水平居中,垂直居中,还有水平垂直居中的方法 水平居中 1.行内元素水平居中 使用text-align:center;就可以实现行内元素的水平居中,但是记得要在父元素中设 ...