Spring Cloud Config配置中心关系型数据库MySQL

> 关系型数据库的配置中心的实现:

Spring Cloud Config 提供了JDBC的方式,使用的数据库是MySQL
大体的请求流程是: config-client 请求 config-server,
config-server根据配置信息获取数据库中的表的相关配置。

整体流程图:
在这里插入图片描述

使用Mysql代替git存储进行配置中心配置,接下来创建工程:

** > 创建父工程 Code

  <!--  利用传递依赖, 公共部分  -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

** > 创建config-server-db 子项目工程

    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>
@SpringBootApplication
@EnableConfigServer
public class DbConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DbConfigServerApplication.class, args); }}
server:
  port: 9090
spring:
  application:
    name: config-server-db
  cloud:
    config:
      server:
        jdbc:
          sql: SELECT `KEY`, `VALUE` FROM PROPERTIES 
          			WHERE application =? AND profile =? AND lable =? 
          			# 上面sql语句是在调用时,使用的SQL
      label: master 
    refresh:
        refreshable: none #表示用来解决DataSource循环依赖问题
  profiles:
    active: jdbc #表示使用的激活方式是JDBC

  ## 数据配置
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/spring-cloud?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
logging:
  level:
    org.springframework.jdbc.core: DEBUG
    org.springframework.jdbc.core.StatementCreatorUtils: Trace

接下来需要在Mysql数据库上创建名称为 spring-cloud的数据库,
再创建Properties(性能)表如下:

-- 创建类型
CREATE TABLE `PROPERTIES` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `KEY` TEXT DEFAULT NULL,
  `VALUE` TEXT DEFAULT NULL,
  `APPLICATION` TEXT DEFAULT NULL,
  `PROFILE` TEXT DEFAULT NULL,
  `LABLE` TEXT DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;


INSERT INTO `spring-cloud`.`properties` (`ID`, `KEY`, `VALUE`, `APPLICATION`, `PROFILE`, `LABLE`) VALUES ('3', 'cn.springcloud.book.config', 'I am the mysql configuration file from dev environment.', 'config-info', 'dev', 'master');
INSERT INTO `spring-cloud`.`properties` (`ID`, `KEY`, `VALUE`, `APPLICATION`, `PROFILE`, `LABLE`) VALUES ('4', 'cn.springcloud.book.config', 'I am the mysql configuration file from test environment.', 'config-info', 'test', 'master');
INSERT INTO `spring-cloud`.`properties` (`ID`, `KEY`, `VALUE`, `APPLICATION`, `PROFILE`, `LABLE`) VALUES ('5', 'cn.springcloud.book.config', 'I am the mysql configuration file from prod environment.', 'config-info', 'prod', 'master');

基于这里数据库配置方式的Server端就完成了,接下来配置config-client
创建config-client-db

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
<!-- application.yml 配置文件 -->
spring:
  application:
    name: config-client-db

server:
  port: 9096

logging:
      level:
          root: INFO
<!-- booststrap.yml 配置文件 -->
spring:
    cloud:
        config:
            label: master
            uri: http://localhost:9090
            name: config-info
            profile: dev

基于这里JDBC的客户端配置也完成了
启动服务端和客户端进行
访问:localhost:9096/configConsumer/get-ConfigInfo 即可,控制层代码查看上一篇文章
运行结果,成功获取了数据库中的配置信息,使用DB的好处是在封闭的环境内,
不搭建git也可以使用配置中心。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值