學習目標
今天我們要學習的是 Spring Cloud Config Server(配置服務器),首先我們先創(chuàng)建項目:打開 https://start.spring.io/斑司,填寫相關信息苹威,項目名為 spring-cloud-chapter-3-config-server
杨幼,添加 Web琉朽、Actuator 以及 Config Server 依賴见秽,點擊 “Generate Project” 按鈕生成項目汗洒,并導入到 idea 中珠漂。(注:此處使用的 Spring Boot 版本為 1.X 系列)
一浪南、認識 Spring Cloud 配置服務器
1)Spring Cloud Config Server:
Spring Cloud 配置服務器提供分布式笼才、動態(tài)化集中管理應用配置信息的能力。
- 分布式:通過負載均衡將流量分發(fā)到不同的配置服務器上
- 動態(tài)化:數(shù)據(jù)源(Git络凿,數(shù)據(jù)庫)發(fā)生變化時患整,客戶端能夠感知的到
- 集中管理:一個配置服務器可能管理多個配置客戶端
2)構建 Spring Cloud 配置服務器
使用 @EnableConfigServer
注解標注
二、服務端 Environment 倉儲
1)EnvironmentRepository
Spring Cloud 配置服務器管理多個客戶端應用的配置信息喷众,然而這些配置信息需要通過一定的規(guī)則獲取各谚。Spring Cloud Config Server 提供 EnvironmentRepository 接口供客戶端應用獲取,其中獲取維度有三:
- {application}:配置客戶端應用名稱到千,即配置項:spring.application.name
- {profile}:配置客戶端應用當前激活的 Profile昌渤,即配置項:spring.profiles.active
- {label}:配置服務端標記的版本信息,如 Git 中的分支名
2)兩種方式搭建 Spring Cloud Config Server
- 基于文件系統(tǒng)實現(xiàn)
- spirng.cloud.config.server.git.uri = ${user.dir}/configs
- 基于 Git 版本控制
- spirng.cloud.config.server.git.uri = https://github.com/AlanShelby/blogs-temporary.git
3)服務端配置映射
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
三憔四、搭建 Spring Cloud Config Server 之基于文件系統(tǒng)創(chuàng)建本地倉庫
上面我們說到了有兩種方式可以搭建 Spring Cloud Config Server膀息,我們先來看第一種--基于文件系統(tǒng)創(chuàng)建本地倉庫般眉。
1)在啟動類中添加 @EnableConfigServer
注解,激活應用配置服務器潜支。
package top.alanshelby.springcloudchapter3configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class SpringCloudChapter3ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudChapter3ConfigServerApplication.class, args);
}
}
2)創(chuàng)建本地目錄
理解 Java 中的 ${user.dir}甸赃,在 IDE 中是指的當前項目物理路徑
可以使用下面代碼進行輸出:
System.out.println(System.getProperty("user.dir"));
輸出的結果為當前項目的路徑:
E:\Project\CourseProject\SpringCloud\spring-cloud-chapter\spring-cloud-chapter-3-config-server
在 idea 中 \src\main\resources
目錄下創(chuàng)建名為 configs
的目錄,它的絕對路徑:${user.dir}\\src\\main\\resources\\configs
3)配置 git 本地倉庫 uri
在 application.properties 文件中添加如下配置信息:
# 配置服務器文件系統(tǒng) git 倉庫
spring.cloud.config.server.git.uri = ${user.dir}\\src\\main\\resources\\configs
4)給應用 blogs
創(chuàng)建三個環(huán)境的配置文件冗酿,并添加 name
屬性埠对,這三個配置文件均位于 configs
目錄下:
- blogs.properties
- name = blogs
- blogs-prod.properties
- name = blogs-prod
- blogs-test.properties
- name = blogs-test
三個文件的環(huán)境 profile 分別是:default
、prod
裁替、test
5)初始化本地 git 倉庫
在 ${user.dir}\src\main\resources\configs 路徑下執(zhí)行以下 git 命令:
- git init
- git add .
- git commit -m "xxx"
出現(xiàn)以下結果表示成功:
3 files changed, 7 insertions(+)
create mode 100644 blogs-prod.properties
create mode 100644 blogs-test.properties
create mode 100644 blogs.properties
6)測試配置服務器
啟動項目项玛,通過瀏覽器測試應用為 blogs
(與創(chuàng)建的三個配置文件名稱相對應,具體映射關系可看本文第二部分中的服務端配置映射)弱判,Profile 為 test
的配置內容襟沮,訪問 http://localhost:9090/blogs/test,這時就會看到 blogs.properties
和 blogs-test.properties
兩個配置文件的信息昌腰,請注意:當指定了 Profile 時开伏,默認的 Profile(不指定)配置信息也會輸出 blogs.properties
。
四遭商、搭建 Spring Cloud Config Server 之基于遠程 git 倉庫
上面我們學習了如何利用文件系統(tǒng)創(chuàng)建本地倉庫固灵,接下來我們來看看如何基于遠程 git 倉庫來 Spring Cloud Config Server。
1)在啟動類中添加 @EnableConfigServer 注解株婴,激活應用配置服務器怎虫。
2)配置遠程 Git 倉庫地址
# 配置服務器遠程 Git 倉庫(GitHub)
spring.cloud.config.server.git.uri = https://github.com/AlanShelby/blogs-temporary.git
3)本地 clone 遠程 Git 倉庫
https://github.com/AlanShelby/blogs-temporary.git
4)給應用 blogstemp
創(chuàng)建三個環(huán)境的配置文件
- blogstemp.properties
- name = blogstemp
- blogstemp-prod.properties
- name = blogstemp-prod
- blogstemp-test.properties
- name = blogstemp-test
三個文件的環(huán)境 profile 分別是:default
暑认、prod
困介、test
5)提交到遠程 Git 倉庫(默認分支為 master,即后面所用到的 label)
git add blogstemp*.properties
git commit -m "add blogstemp config files"
git push
6)配置強制拉取內容
# 強制拉去 Git 內容
spring.cloud.config.server.git.force-pull = true
7)測試配置服務器
啟動項目蘸际,通過瀏覽器測試應用為 blogstemp座哩,Profile 為 test 的配置內容,訪問 http://localhost:9090/blogstemp/test粮彤,這時就會看到 blogstemp.properties 和 blogstemp-test.properties 兩個配置文件的信息根穷。
注意:這里的 version 與你 git 提交的 version 是一致的,可在本地使用 git log 命令查看导坟。
五屿良、Spring Cloud 配置客戶端
1)創(chuàng)建 Spring Cloud Config Client 應用
創(chuàng)建一個名為 spring-cloud-chapter-3-config-client
的應用
2)在 classPath 下面創(chuàng)建 bootstrap.properties
3)配置 bootstrap.properties
配置以 spring.cloud.config.
開頭的配置信息
# 配置客戶端應用關聯(lián)的應用,該配置時可選的惫周,如果不配置尘惧,采用 spring.application.name
spring.cloud.config.name = blogstemp
# 關聯(lián) profile
spring.cloud.config.profile = prod
# 關聯(lián) label
spring.cloud.config.label = master
# 關聯(lián) uri
spring.cloud.config.uri = http://localhost:9090/
在 application.properties
中添加如下配置:
spring.application.name = spring-cloud-config-client
server.port = 8080
management.security.enabled = false
4)先啟動 spring-cloud-chapter-3-config-server
然后再啟動 spring-cloud-chapter-3-config-client 項目,在 spring-cloud-chapter-3-config-client 啟動日志中可以看到如下信息递递,表示 clien 已經(jīng)連接到了 server喷橙,并取到了 GitHub 上的 blogstemp-prod.properties 配置文件啥么。
Fetching config from server at: http://localhost:9090/
Located environment: name=blogstemp, profiles=[prod], label=master, version=11399ab7d42f35c3282bb2c775f97e2d77b2c0e5, state=null
Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://github.com/AlanShelby/blogs-temporary.git/blogstemp-prod.properties'}, MapPropertySource {name='https://github.com/AlanShelby/blogs-temporary.git/blogstemp.properties'}]]
5)測試 Spring Cloud 配置客戶端
通過瀏覽器訪問 http://localhost:8080/env
至此,關于Spring Cloud Config Server(配置服務器)就講解完了贰逾,這是我的理解悬荣,各位看官如果有不同見解或文章中有錯誤,請不吝指正疙剑。
所用代碼碼云地址:https://gitee.com/AlanShelby/spring-cloud-chapter
知乎專欄地址:https://zhuanlan.zhihu.com/c_200981602
個人微信公眾號:AlanShelby(多多關注氯迂,感謝~)