Spring Cloud Config采用数据库存储配置内容
在之前的《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(如果您不了解可以看一下这篇文章)
<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数据源,key
、value
是保留关键词,原生的实现语句会报错,所以需要重写一下这句查询语句(如果存储的表结构设计不同于上面准备的内容,也可以通过这个属性的配置来修改配置的获取逻辑)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-db
和config-client
两个项目:
- Github:https://github.com/dyc87112/SpringCloud-Learning/
- Gitee:https://gitee.com/didispace/SpringCloud-Learning/
如果您对这些感兴趣,欢迎star、follow、收藏、转发给予支持!
Spring Cloud Config采用数据库存储配置内容的更多相关文章
- springcloud(六):Spring Cloud 配置中心采用数据库存储配置内容
Spring Cloud 配置中心采用数据库存储配置内容 转自:Spring Cloud Config采用数据库存储配置内容[Edgware+] Spring Cloud Server配置中心采用了G ...
- Spring Cloud Config采用Git存储时两种常用的配置策略
由于Spring Cloud Config默认采用了Git存储,相信很多团队在使用Spring Cloud的配置中心时也会采用这样的策略.即便大家都使用了Git存储,可能还有各种不同的配置方式,本文就 ...
- Spring Cloud Config 实现配置中心,看这一篇就够了
Spring Cloud Config 是 Spring Cloud 家族中最早的配置中心,虽然后来又发布了 Consul 可以代替配置中心功能,但是 Config 依然适用于 Spring Clou ...
- spring cloud config将配置存储在数据库中
Spring Cloud Config Server最常见是将配置文件放在本地或者远程Git仓库,放在本地是将将所有的配置文件统一写在Config Server工程目录下,如果需要修改配置,需要重启c ...
- 为Spring Cloud Config插上管理的翅膀
最近一致在更新Spring Cloud Config的相关内容,主要也是为这篇埋个伏笔,相信不少调研过Spring Cloud Config的用户都会吐槽它的管理能力太弱.因此,就有了下面为讲推荐的这 ...
- Spring Cloud config之一:分布式配置中心入门介绍
Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持.配置服务器为各应用的所有环境提供了一个中心化的外部配置.它实现了对服务端和客户端对Spring Environm ...
- 【八】Spring Cloud Config
一.分布式系统面临的--配置问题 微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的力度相对较小,因此系统中会出现大量的服务.由于每个服务都需要必要的配置信息才能运行,所以一套集中式的.动 ...
- Spring Boot + Spring Cloud 构建微服务系统(九):配置中心(Spring Cloud Config)
技术背景 如今微服务架构盛行,在分布式系统中,项目日益庞大,子项目日益增多,每个项目都散落着各种配置文件,且随着服务的增加而不断增多.此时,往往某一个基础服务信息变更,都会导致一系列服务的更新和重启, ...
- spring boot 2.0.3+spring cloud (Finchley)6、配置中心Spring Cloud Config
https://www.cnblogs.com/cralor/p/9239976.html Spring Cloud Config 是用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持, ...
随机推荐
- can't open the mysql.plugin table. please run mysql_upgrade to create it.
To initialize a fresh data directory, you basically (after setting your config file) just have to ru ...
- HDU5810 Balls and Boxes
Balls and Boxes Time Limi ...
- vmWare pro 14.1.1+ubuntu-desktop-amd64的总体安装流程
vmWare pro正常安装 下载后双击安装,按步骤走即可 创建虚拟机 设置虚拟机 window设置虚拟化技术 电脑重启后,弹出如下所示,选择 疑难解答->高级选项->UEFI固件设置-& ...
- DOS 命令 os系统(windows)
一.cd 相关操作 1."cd .. "or "cd ..\" --返回上一级 2.cd E:\Python -- 进入目录 二.dir --drectory ...
- 微信屏蔽APP分享链接的解决方案原理,剖析微信域名防封技术
为什么很多商家在微信封域名如此严格的情况下,还会挤破头皮去做微信营销和推广呢?又有些人问,为什么别人的域名长时间推广都没事,自己的链接在微信内一推就被拦截呢?这里你可能需要注意一点事,事出无常必有妖. ...
- shiro与项目集成开发
shiro与spring web项目开发 加入shiro的jar包 自定义realm /** * 自定义realm 继承授权realm * @author Administrator * */ pub ...
- Firewalld的结构
原文地址:http://www.excelib.com/article/287/show firewalld简介 Centos7中默认将原来的防火墙iptables升级为了firewalld,fire ...
- Nuget4.0 bug一粒
这个锅到底是nuget的还是msbuild的我也不是很确定 在使用Nuget4.0打包编译项目时 当执行到nuget pack %%~dpna.csproj -build -Prop Configur ...
- 四面美团,收割 offer
阅读本文大概需要 6 分钟. 来源:https://blog.csdn.net/csuliyajin2012/article/details/49430659 美团我是在拉勾网上投的简历,之前也投过一 ...
- node.js服务器搭建
//1.导入http 核心模块 const http = require("http"); //2.调用http.createServer 方法,创建一个web 服务器对象 con ...