0
点赞
收藏
分享

微信扫一扫

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


Spring Cloud Config

  • ​​1、什么是Spring Cloud Config?​​
  • ​​2、EnvironmentRepository抽象​​
  • ​​3、实战:使用git作为配置源​​
  • ​​1、搭建config server​​
  • ​​2、搭建config client​​
  • ​​3、config server HTTP接口​​
  • ​​4、实战:使用数据库作为配置源​​
  • ​​5、实战:复合配置源​​

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.SR4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

在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

注意:

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

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

完整项目结构图如下所示。

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

2、搭建config client

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

<properties>
<spring-cloud.version>Hoxton.SR4</spring-cloud.version>
</properties>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

在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

注意:

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

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

\---mall-eureka
mall-eureka-dev.yml

完整项目结构图如下所示。

Spring Cloud教程 第十一弹 Spring Cloud Config连接git和数据库_动态刷新_02

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

比如这样的一个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配置源';

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

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

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

使用如下的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

可以看到,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`=?

方式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


举报

相关推荐

0 条评论