点击上方“方志朋”,选择“置顶或者星标”

你的关注意义重大!

Spring Cloud Config Server最常见是将配置文件放在本地或者远程Git仓库,放在本地是将将所有的配置文件统一写在Config Server工程目录下,如果需要修改配置,需要重启config server;放在Git仓库,是将配置统一放在Git仓库,可以利用Git仓库的版本控制。本文将介绍使用另外一种方式存放配置信息,即将配置存放在Mysql中。

整个流程:Config Sever暴露Http API接口,Config Client 通过调用Config Sever的Http API接口来读取配置Config Server的配置信息,Config Server从数据中读取具体的应用的配置。流程图如下:

案例实战

在本案例中需要由2个工程,分为config-server和config-client,其中config-server工程需要连接Mysql数据库,读取配置;config-client则在启动的时候从config-server工程读取。本案例Spring Cloud版本为Greenwich.RELEASE,Spring Boot版本为2.1.0.RELEASE。

工程 描述
config-server 端口8769,从数据库中读取配置
config-client 端口8083,从config-server读取配置

搭建config-server工程

创建工程config-server,在工程的pom文件引入config-server的起步依赖,mysql的连接器,jdbc的起步依赖,代码如下:

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

在工程的配置文件application.yml下做以下的配置:

spring:profiles:active: jdbcapplication:name: config-jdbc-serverdatasource:url: jdbc:mysql://127.0.0.1:3306/config-jdbc?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=GMT%2B8username: rootpassword: 123456driver-class-name: com.mysql.jdbc.Drivercloud:config:label: masterserver:jdbc: true
server:port: 8769
spring.cloud.config.server.jdbc.sql: SELECT key1, value1 from config_properties where APPLICATION=? and PROFILE=? and LABEL=?

其中,spring.profiles.active为spring读取的配置文件名,从数据库中读取,必须为jdbc。spring.datasource配置了数据库相关的信息,spring.cloud.config.label读取的配置的分支,这个需要在数据库中数据对应。spring.cloud.config.server.jdbc.sql为查询数据库的sql语句,该语句的字段必须与数据库的表字段一致。

在程序的启动文件ConfigServerApplication加上@EnableConfigServer注解,开启ConfigServer的功能,代码如下:

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);}
}

初始化数据库

由于Config-server需要从数据库中读取,所以读者需要先安装MySQL数据库,安装成功后,创建config-jdbc数据库,数据库编码为utf-8,然后在config-jdbc数据库下,执行以下的数据库脚本:

CREATE TABLE `config_properties` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`key1` varchar(50) COLLATE utf8_bin NOT NULL,`value1` varchar(500) COLLATE utf8_bin DEFAULT NULL,`application` varchar(50) COLLATE utf8_bin NOT NULL,`profile` varchar(50) COLLATE utf8_bin NOT NULL,`label` varchar(50) COLLATE utf8_bin DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin

其中key1字段为配置的key,value1字段为配置的值,application字段对应于应用名,profile对应于环境,label对应于读取的分支,一般为master。

插入数据config-client 的2条数据,包括server.port和foo两个配置,具体数据库脚本如下:

insert into `config_properties` (`id`, `key1`, `value1`, `application`, `profile`, `label`) values('1','server.port','8083','config-client','dev','master');
insert into `config_properties` (`id`, `key1`, `value1`, `application`, `profile`, `label`) values('2','foo','bar-jdbc','config-client','dev','master');

搭建config-client

在 config-client工程的pom文件,引入web和config的起步依赖,代码如下:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId>
</dependency>

在程序的启动配置文件 bootstrap.yml做程序的相关配置,一定要是bootstrap.yml,不可以是application.yml,bootstrap.yml的读取优先级更高,配置如下:

spring:application:name: config-clientcloud:config:uri: http://localhost:8769fail-fast: trueprofiles:active: dev

其中spring.cloud.config.uri配置的config-server的地址,spring.cloud.config.fail-fast配置的是读取配置失败后,执行快速失败。spring.profiles.active配置的是spring读取配置文件的环境。

在程序的启动文件ConfigClientApplication,写一个RestAPI,读取配置文件的foo配置,返回给浏览器,代码如下:

@SpringBootApplication
@RestController
public class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);}@Value("${foo}")String foo;@RequestMapping(value = "/foo")public String hi(){return foo;}
}

依次启动2个工程,其中config-client的启动端口为8083,这个是在数据库中的,可见config-client从 config-server中读取了配置。在浏览器上访问http://localhost:8083/foo,浏览器显示bar-jdbc,这个是在数据库中的,可见config-client从 config-server中读取了配置。

参考资料

https://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html#jdbcbackend

源码下载

https://github.com/forezp/SpringCloudLearning/tree/master/chapter10-5-jdbc

-更多文章-

这 10 款插件让你的 GitHub 更好用、更有趣

Nginx是什么 ? 能干嘛 ?

MAT入门到精通(二)

MAT入门到精通(一)

为了效率,扎克伯格的26张PPT

分布式架构知识体系

Spring Cloud Consul 之Greenwich版本全攻略

-关注我-

看完了,帮我点个“好看”鸭

点鸭点鸭

↓↓↓↓

spring cloud config将配置存储在数据库中相关推荐

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

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

  2. Spring Cloud Config的配置中心获取不到最新配置信息的问题

    本篇源于Spring Cloud Config的一个问题,但这个问题并非所有人都会遇到.如果您遇到了,那必须得看看这篇,如果没有遇到您也应该看看,防患于未然! 问题描述 之前有朋友提出Spring C ...

  3. 第十二章 Spring Cloud Config 统一配置中心详解

    目录 一.配置问题分析及解决方案 1.问题分析 2.解决方案 二.Spring Cloud Config 介绍 1.Spring Cloud Config特性 2.Spring Cloud Confi ...

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

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

  5. Spring cloud config 分布式配置中心(一) 服务端

    作用: 为分布式系统中的基础设施和微服务应用提供外部集中化的配置支持,分客户端和服务端 服务端: 即分布式配置中心,是一个独立的微服务应用,连接配置仓库,为客户端提供一些访问接口,如加密 / 解密信息 ...

  6. 为Spring Cloud Config Server配置远程git仓库

    简介 虽然在开发过程,在本地创建git仓库操作起来非常方便,但是在实际项目应用中,多个项目组需要通过一个中心服务器来共享配置,所以Spring Cloud配置中心支持远程git仓库,以使分散的项目组更 ...

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

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

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

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

  9. 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 ...

最新文章

  1. ionic 定位 android,ionic3定位 (android)
  2. android 电源管理 关闭屏幕,Android之PowerManager电源管理
  3. TF学习——TF之API:TensorFlow的高级机器学习API—tf.contrib.learn的简介、使用方法、案例应用之详细攻略
  4. php实现注册登陆验证
  5. SIEM比以往更重要的5个原因
  6. 条件编译#define、#undef、#if、#elif、#elif defined、#elif !defined 、#endif用法
  7. 字符串转HTML段落
  8. Duplicate key
  9. 给《人人都是产品经理》的一封信
  10. PID控制原理(全干货)
  11. python股票行情接口实时获取股市数据
  12. vue实现搜索框记录搜索历史_Vue 实现输入框新增搜索历史记录功能
  13. 麦克风没声音,这个选项你注意到了吗?
  14. 使用 AndroidSocketClient 库建立 SSL 安全链接
  15. 6G八大关键技术(国泰君安团队)
  16. win7装xp(win7装xp双系统教程)
  17. linux电脑接电视,Ubuntu下如何给通过HDMI连接电视机的计算机强制设置1920*1080分辨率...
  18. 航空Ethernet嵌入式测试平台ETest
  19. 阿里云国际版CDN的优势
  20. 支付宝公众服务是什么?

热门文章

  1. Entity Framework应用:根据实体的EntityState状态实现增删改查
  2. vb.net与matlab的混合编程
  3. 控件包含代码块,因此无法修改控件集合
  4. Python-字符串操作方法 [转]
  5. 【组队学习】【33期】动手学数据分析
  6. 里氏替换原则(Liskov Substitution Principle,LSP)
  7. Visual Studio UML Activity Diagram(2)
  8. delphi xe 文件服务器,DelphiXE7中创建WebService(服务端+客户端)
  9. 微软全球副总裁洪小文:应对数字化转型挑战,跨界共创正当时
  10. 日本「AI 鱼脸识别」项目,每分钟识别 100 条