SpringCloud Config連接git與數(shù)據(jù)庫流程分析講解
1、什么是Spring Cloud Config
Spring Cloud Config為微服務(wù)架構(gòu)提供了配置管理的功能,通過Spring Cloud Config服務(wù)端提供配置中心,在各個(gè)微服務(wù)應(yīng)用的客戶端讀取來自服務(wù)端配置中心的配置項(xiàng),配置中心的數(shù)據(jù)源可以來自git、svn、數(shù)據(jù)庫、操作系統(tǒng)的本地文件、jar包中的文件、vault、組合。
Spring Cloud Config = 微服務(wù)配置中心。
2、EnvironmentRepository抽象
EnvironmentRepository接口的實(shí)現(xiàn)可提供不同的配置源,主要實(shí)現(xiàn)如下:
- CompositeEnvironmentRepository:復(fù)合,如git+數(shù)據(jù)庫
- JGitEnvironmentRepository:git
- JdbcEnvironmentRepository:數(shù)據(jù)庫
接口EnvironmentRepository只提供了一個(gè)方法findOne,通過傳入application、profile和label來獲得配置項(xiàng)。
application:應(yīng)用名,可通過spring.application.name配置
profile:激活的配置文件,可通過spring.profiles.active配置
label:沒有特定的含義,可以當(dāng)做git的分支名或版本號(hào)來用
3、實(shí)戰(zhàn)-使用git作為配置源
1、搭建config server
在IDEA中創(chuàng)建一個(gè)作為config server的Maven項(xiàng)目,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選項(xiàng)。
{application}是占位符,會(huì)被動(dòng)態(tài)替換為config client的application name
在Spring Boot啟動(dòng)類打上@EnableConfigServer注解,最后啟動(dòng)項(xiàng)目,config server就運(yùn)行起來了。
完整項(xiàng)目結(jié)構(gòu)圖如下所示。

2、搭建config client
在IDEA中創(chuàng)建一個(gè)作為config client的Maven項(xiàng)目,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中會(huì)放一些連接config server的配置,而其它的配置就可以放到git上了
git倉庫文件結(jié)構(gòu)如下所示:
\---mall-eureka
mall-eureka-dev.yml
完整項(xiàng)目結(jié)構(gòu)圖如下所示。

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
比如這樣的一個(gè)URL:http://localhost:20190/mall_config/master/mall-eureka-dev.yml
4、實(shí)戰(zhàn)-使用數(shù)據(jù)庫作為配置源
首先準(zhǔn)備一張數(shù)據(jù)庫表:
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 '創(chuàng)建時(shí)間', modify_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改時(shí)間', PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='spring cloud config jdbc配置源';
其中表名必須叫properties,且表中必須要有application、profile、label、key、value這幾個(gè)字段,官方規(guī)定的。
由于使用數(shù)據(jù)庫配置源,因此要連接數(shù)據(jù)庫,在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配置項(xiàng)變成了spring.cloud.config.server.jdbc,另外多個(gè)數(shù)據(jù)源的配置。
5、實(shí)戰(zhàn)-復(fù)合配置源
如果我想使用git和數(shù)據(jù)庫作為雙重作為配置源,可能是多個(gè)git和多個(gè)數(shù)據(jù)庫,該怎么辦呢?
有兩種方式:
利用composite,會(huì)使用全部的配置源,優(yōu)先級(jí)按列出的順序,最頂上的優(yōu)先級(jí)最高
利用spring.profiles.active激活多個(gè)配置,可動(dòng)態(tài)選擇使用全部配置源中的一部分,可以使用order配置項(xiàng)進(jìn)行排序
方式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
到此這篇關(guān)于SpringCloud Config連接git與數(shù)據(jù)庫流程分析講解的文章就介紹到這了,更多相關(guān)SpringCloud Config連接git內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解eclipse項(xiàng)目中的.classpath文件原理
這篇文章介紹了eclipse項(xiàng)目中的.classpath文件的原理,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
Java中獲取類路徑classpath的簡(jiǎn)單方法(推薦)
下面小編就為大家?guī)硪黄狫ava中獲取類路徑classpath的簡(jiǎn)單方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
Java開發(fā)實(shí)現(xiàn)飛機(jī)大戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了Java開發(fā)實(shí)現(xiàn)飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05

