SpringCloud Config連接git與數(shù)據(jù)庫流程分析講解
1、什么是Spring Cloud Config
Spring Cloud Config為微服務(wù)架構(gòu)提供了配置管理的功能,通過Spring Cloud Config服務(wù)端提供配置中心,在各個微服務(wù)應(yīng)用的客戶端讀取來自服務(wù)端配置中心的配置項,配置中心的數(shù)據(jù)源可以來自git、svn、數(shù)據(jù)庫、操作系統(tǒng)的本地文件、jar包中的文件、vault、組合。
Spring Cloud Config = 微服務(wù)配置中心。
2、EnvironmentRepository抽象
EnvironmentRepository接口的實現(xiàn)可提供不同的配置源,主要實現(xiàn)如下:
- CompositeEnvironmentRepository:復(fù)合,如git+數(shù)據(jù)庫
- JGitEnvironmentRepository:git
- JdbcEnvironmentRepository:數(shù)據(jù)庫
接口EnvironmentRepository只提供了一個方法findOne,通過傳入application、profile和label來獲得配置項。
application:應(yīng)用名,可通過spring.application.name配置
profile:激活的配置文件,可通過spring.profiles.active配置
label:沒有特定的含義,可以當做git的分支名或版本號來用
3、實戰(zhàn)-使用git作為配置源
1、搭建config server
在IDEA中創(chuàng)建一個作為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}是占位符,會被動態(tài)替換為config client的application name
在Spring Boot啟動類打上@EnableConfigServer注解,最后啟動項目,config server就運行起來了。
完整項目結(jié)構(gòu)圖如下所示。
2、搭建config client
在IDEA中創(chuàng)建一個作為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倉庫文件結(jié)構(gòu)如下所示:
\---mall-eureka
mall-eureka-dev.yml
完整項目結(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
比如這樣的一個URL:http://localhost:20190/mall_config/master/mall-eureka-dev.yml
4、實戰(zhàn)-使用數(shù)據(jù)庫作為配置源
首先準備一張數(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)建時間', 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這幾個字段,官方規(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配置項變成了spring.cloud.config.server.jdbc,另外多個數(shù)據(jù)源的配置。
5、實戰(zhàn)-復(fù)合配置源
如果我想使用git和數(shù)據(jù)庫作為雙重作為配置源,可能是多個git和多個數(shù)據(jù)庫,該怎么辦呢?
有兩種方式:
利用composite,會使用全部的配置源,優(yōu)先級按列出的順序,最頂上的優(yōu)先級最高
利用spring.profiles.active激活多個配置,可動態(tài)選擇使用全部配置源中的一部分,可以使用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
到此這篇關(guān)于SpringCloud Config連接git與數(shù)據(jù)庫流程分析講解的文章就介紹到這了,更多相關(guān)SpringCloud Config連接git內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于SpringBoot中Ajax跨域以及Cookie無法獲取丟失問題
這篇文章主要介紹了關(guān)于SpringBoot中Ajax跨域以及Cookie無法獲取丟失問題,本文具有參考意義,遇到相同或者類似問題的小伙伴希望可以從中找到靈感2023-03-03java?SpringBoot?分布式事務(wù)的解決方案(JTA+Atomic+多數(shù)據(jù)源)
這篇文章主要介紹了java?SpringBoot?分布式事務(wù)的解決方案(JTA+Atomic+多數(shù)據(jù)源),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-08-08Java+opencv3.2.0實現(xiàn)hough直線檢測
這篇文章主要為大家詳細介紹了Java+opencv3.2.0之hough直線檢測,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02SpringBoot中優(yōu)化Undertow性能的方法總結(jié)
Undertow是一個采用 Java 開發(fā)的靈活的高性能Web服務(wù)器,提供包括阻塞和基于NIO的非堵塞機制,本文將給大家介紹SpringBoot中優(yōu)化Undertow性能的方法,文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下2024-08-08SpringBoot請求參數(shù)傳遞與接收說明小結(jié)
這篇文章主要介紹了SpringBoot請求參數(shù)傳遞與接收,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12關(guān)于@EnableGlobalMethodSecurity注解的用法解讀
這篇文章主要介紹了關(guān)于@EnableGlobalMethodSecurity注解的用法解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03