• Spring Cloud教程 第十一弹 Spring Cloud Config连接git和数据库


    1、什么是Spring Cloud Config?

    Spring Cloud Config为微服务架构提供了配置管理的功能,通过Spring Cloud Config服务端提供配置中心,在各个微服务应用的客户端读取来自服务端配置中心的配置项,配置中心的数据源可以来自git、svn、数据库、操作系统的本地文件、jar包中的文件、vault、组合。

    Spring Cloud Config = 微服务配置中心

    2、EnvironmentRepository抽象

    EnvironmentRepository接口的实现可提供不同的配置源,主要实现如下:

    • CompositeEnvironmentRepository:复合,如git+数据库
    • JGitEnvironmentRepository:git
    • JdbcEnvironmentRepository:数据库

    接口EnvironmentRepository只提供了一个方法findOne,通过传入application、profile和label来获得配置项。

    application:应用名,可通过spring.application.name配置

    profile:激活的配置文件,可通过spring.profiles.active配置

    label:没有特定的含义,可以当做git的分支名或版本号来用

    3、实战:使用git作为配置源

    1、搭建config server

    在IDEA中创建一个作为config server的Maven项目,pom.xml中引入如下依赖。

    <properties>
        <spring-cloud.version>Hoxton.SR4spring-cloud.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-config-serverartifactId>
        dependency>
    dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在src/main/resources/bootstrap.yml中添加如下配置:

    spring:
      cloud:
        config:
          server:
            git:
              uri: 你的git仓库uri
              default-label: master
              search-paths: '{application}' # 搜索的目录
    server:
      servlet:
        context-path: /mall_config
      port: 20190
    debug: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    注意:

    • 这里使用了git作为配置源,需要填写你的git仓库uri,如果是私有仓库还需要配置username和password选项。
    • {application}是占位符,会被动态替换为config client的application name

    在Spring Boot启动类打上@EnableConfigServer注解,最后启动项目,config server就运行起来了。

    完整项目结构图如下所示。在这里插入图片描述

    2、搭建config client

    在IDEA中创建一个作为config client的Maven项目,pom.xml中引入如下依赖。

    <properties>
        <spring-cloud.version>Hoxton.SR4spring-cloud.version>
    properties>
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-configartifactId>
    dependency>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在src/main/resources/bootstrap.yml中添加如下配置:

    spring:
      application:
        name: mall-eureka
      cloud:
        config:
          uri: http://localhost:20190/mall_config
          name: mall-eureka
          profile: dev
          label: master
    debug: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    注意:

    • 在config client的bootstrap.yml中会放一些连接config server的配置,而其它的配置就可以放到git上了

    git仓库文件结构如下所示:

    \---mall-eureka
            mall-eureka-dev.yml
    
    • 1
    • 2

    完整项目结构图如下所示。
    在这里插入图片描述

    3、config server HTTP接口

    config server提供了如下的HTTP接口,可以直接在浏览器上访问URL看到配置。

    /{application}/{profile}[/{label}]
    /{application}-{profile}.yml
    /{label}/{application}-{profile}.yml
    /{application}-{profile}.properties
    /{label}/{application}-{profile}.properties
    
    • 1
    • 2
    • 3
    • 4
    • 5

    比如这样的一个URL:http://localhost:20190/mall_config/master/mall-eureka-dev.yml

    4、实战:使用数据库作为配置源

    首先准备一张数据库表:

    CREATE TABLE properties (
      id int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '物理主键',
      application varchar(255) COMMENT 'application',
      `profile` varchar(255) COMMENT 'profile',
      label varchar(255) COMMENT 'label',
      `key` varchar(255) COMMENT 'key',
      `value` varchar(255) COMMENT 'value',
      `desc` varchar(255) COMMENT '描述',
      create_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
      modify_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
      PRIMARY KEY (id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='spring cloud config jdbc配置源';
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    其中表名必须叫properties,且表中必须要有application、profile、label、key、value这几个字段,官方规定的。

    由于使用数据库配置源,因此要连接数据库,在config server的pom.xml中还要引入以下依赖:

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-jdbcartifactId>
    dependency>
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    使用如下的src/main/resources/bootstrap.yml配置:

    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/tudou_mall_admin?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
        username: root
        password: ok
      cloud:
        config:
          server:
            jdbc:
              sql: SELECT `KEY`, `VALUE` from `PROPERTIES` where `APPLICATION`=? and `PROFILE`=? and `LABEL`=?
    server:
      servlet:
        context-path: /mall_config
      port: 20190
    debug: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    可以看到,spring.cloud.config.server.git配置项变成了spring.cloud.config.server.jdbc,另外多个数据源的配置。

    5、实战:复合配置源

    如果我想使用git和数据库作为双重作为配置源,可能是多个git和多个数据库,该怎么办呢?

    有两种方式:

    1. 利用composite,会使用全部的配置源,优先级按列出的顺序,最顶上的优先级最高
    2. 利用spring.profiles.active激活多个配置,可动态选择使用全部配置源中的一部分,可以使用order配置项进行排序

    方式1配置如下:

    spring:
      profiles:
        active: composite
      cloud:
        config:
          server:
            composite:
            -
              type: git
              uri: https://gitee.com/bobostudy/com.tudou.mall.config.git
              default-label: master
              search-paths: '{application}' # 搜索的目录
            -
              type: jdbc
              sql: SELECT `KEY`, `VALUE` from `PROPERTIES` where `APPLICATION`=? and `PROFILE`=? and `LABEL`=?
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    方式2配置如下:

    spring:
      profiles:
        active: git,jdbc
      cloud:
        config:
          server:
    		git:
              uri: https://gitee.com/bobostudy/com.tudou.mall.config.git
              default-label: master
              search-paths: '{application}' # 搜索的目录
              order: 0
            jdbc:
              sql: SELECT `KEY`, `VALUE` from `PROPERTIES` where `APPLICATION`=? and `PROFILE`=? and `LABEL`=?
              order: 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    驱动开发(二):创建字符设备驱动
    将数据预处理嵌入AI模型的常见技巧
    231.2的幂
    短视频短剧小程序系统:创新的内容传播与互动体验
    了解 Oracle 中的视图
    使用axios 请求库结合iview组件做登录页面
    使用 Apache Camel 和 Quarkus 的微服务(一)
    Vue 组件间通信并不是每一次操作都会触发新的通信
    Unity 动画系统基本概念
    JAVAEE初阶相关内容第十六弹--网络编程
  • 原文地址:https://blog.csdn.net/xl_1803/article/details/128138379