资讯专栏INFORMATION COLUMN

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

RobinQu / 3549人阅读

摘要:工程描述端口,从数据库中读取配置端口,从读取配置搭建工程创建工程,在工程的文件引入的起步依赖,的连接器,的起步依赖,代码如下在工程的配置文件下做以下的配置其中,为读取的配置文件名,从数据库中读取,必须为。

</>复制代码

  1. 转载请标明出处:
    https://blog.csdn.net/forezp/...
    本文出自方志朋的博客

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的起步依赖,代码如下:

</>复制代码

  1. org.springframework.cloud
  2. spring-cloud-config-server
  3. mysql
  4. mysql-connector-java
  5. org.springframework.boot
  6. spring-boot-starter-jdbc

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

</>复制代码

  1. spring:
  2. profiles:
  3. active: jdbc
  4. application:
  5. name: config-jdbc-server
  6. datasource:
  7. url: jdbc:mysql://127.0.0.1:3306/config-jdbc?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=GMT%2B8
  8. username: root
  9. password: 123456
  10. driver-class-name: com.mysql.jdbc.Driver
  11. cloud:
  12. config:
  13. label: master
  14. server:
  15. jdbc: true
  16. server:
  17. port: 8769
  18. 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的功能,代码如下:

</>复制代码

  1. @SpringBootApplication
  2. @EnableConfigServer
  3. public class ConfigServerApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ConfigServerApplication.class, args);
  6. }
  7. }
初始化数据库

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

</>复制代码

  1. CREATE TABLE `config_properties` (
  2. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  3. `key1` varchar(50) COLLATE utf8_bin NOT NULL,
  4. `value1` varchar(500) COLLATE utf8_bin DEFAULT NULL,
  5. `application` varchar(50) COLLATE utf8_bin NOT NULL,
  6. `profile` varchar(50) COLLATE utf8_bin NOT NULL,
  7. `label` varchar(50) COLLATE utf8_bin DEFAULT NULL,
  8. PRIMARY KEY (`id`)
  9. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin

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

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

</>复制代码

  1. insert into `config_properties` (`id`, `key1`, `value1`, `application`, `profile`, `label`) values("1","server.port","8083","config-client","dev","master");
  2. 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的起步依赖,代码如下:

</>复制代码

  1. org.springframework.boot
  2. spring-boot-starter-web
  3. org.springframework.cloud
  4. spring-cloud-starter-config

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

</>复制代码

  1. spring:
  2. application:
  3. name: config-client
  4. cloud:
  5. config:
  6. uri: http://localhost:8769
  7. fail-fast: true
  8. profiles:
  9. active: dev

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

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

</>复制代码

  1. @SpringBootApplication
  2. @RestController
  3. public class ConfigClientApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ConfigClientApplication.class, args);
  6. }
  7. @Value("${foo}")
  8. String foo;
  9. @RequestMapping(value = "/foo")
  10. public String hi(){
  11. return foo;
  12. }
  13. }

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

参考资料

https://cloud.spring.io/sprin...

源码下载

https://github.com/forezp/Spr...

</>复制代码


  1. 扫一扫,支持下作者吧
  2. (转载本站文章请注明作者和出处 方志朋的博客

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/73362.html

相关文章

  • Spring Cloud 参考文档(Spring Cloud Config Server)

    摘要:,这是标记配置文件集版本化的服务器端特性。要配置对称密钥,需要将设置为秘密字符串或使用环境变量将其排除在纯文本配置文件之外。 Spring Cloud Config Server Spring Cloud Config Server为外部配置提供基于HTTP资源的API(名称—值对或等效的YAML内容),通过使用@EnableConfigServer注解,服务器可嵌入Spring Bo...

    harryhappy 评论0 收藏0
  • Spring Cloud 参考文档(Spring Cloud Config快速入门)

    摘要:快速入门这个快速入门使用的服务器和客户端。属性在端点中显示为高优先级属性源,如以下示例所示。名为的属性源包含值为且具有最高优先级的属性。属性源名称中的是存储库,而不是配置服务器。 Spring Cloud Config快速入门 这个快速入门使用Spring Cloud Config Server的服务器和客户端。 首先,启动服务器,如下所示: $ cd spring-cloud-con...

    gekylin 评论0 收藏0
  • Spring Cloud实战(一)-Spring Cloud Config Server

    摘要:概要什么是创建并运行一个建立一个创建并运行一个是什么什么是配置信息一个中不只是代码还需要连接资源和其它应用经常有很多需要外部设置的项去调整行为如切换不同的数据库国际化等应用中的会经常见到的等就是配置信息常见的实现信息配置的方法硬编码缺点需要 概要 什么是Spring Cloud Config? 创建并运行一个Spring Cloud Config Server 建立一个Reposit...

    Dionysus_go 评论0 收藏0
  • Spring Cloud 参考文档(嵌入Config Server)

    摘要:在这种情况下,名为的可选属性非常有用,它是一个标志,指示服务器是否应从其自己的远程存储库配置自身,默认情况下,该标志处于关闭状态,因为它可能会延迟启动。 嵌入Config Server Config Server作为独立应用程序运行最佳,但是,如果需要,你可以将其嵌入另一个应用程序中,为此,请使用@EnableConfigServer注解。在这种情况下,名为spring.cloud.c...

    tainzhi 评论0 收藏0
  • Spring Cloud Config 学习(一)

    摘要:默认使用存储配置内容也可以使用本地文件系统或存储配置。会在微服务启动时,请求以获取所需要的配置属性,并且缓存在本地以提高性能。 简介 对于传统的单体应用,通常是使用配置文件来管理所有的配置,但是在微服务架构中,会存在很多的微服务,如果每一个微服务都维护自己的配置,显然是非常的麻烦且不灵活,维护成本会非常高 使用Spring Cloud Config可以实现一下功能 集中管理配置 不同...

    wthee 评论0 收藏0

发表评论

0条评论

RobinQu

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<