SpringCloud Config分布式配置中心
SpringCloud Config分布式配置中心概述分布式系統(tǒng)面臨的---配置問(wèn)題是什么能干嘛與GitHub整合配置官網(wǎng)Config服務(wù)端配置與測(cè)試用你自己的賬號(hào)在GitHub上新建一個(gè)名為springcloud-config的新Repository由上一步獲得剛新建的git地址本地硬盤(pán)目錄上新建git倉(cāng)庫(kù)并clonegit命令此時(shí)在本地D盤(pán)符下D:\44\SpringCloud2020\springcloud-config表示多個(gè)環(huán)境的配置文件保存格式必須為UTF-8如果需要修改,此處模擬運(yùn)維人員操作git和github新建Module模塊cloud-config-center-3344它即為Cloud的配置中心模塊cloudConfig CenterPOMYML主啟動(dòng)類(lèi)(@EnableConfigServer)windows下修改hosts文件,增加映射測(cè)試通過(guò)Config微服務(wù)是否可以從GitHub上獲取配置內(nèi)容啟動(dòng)微服務(wù)3344配置讀取規(guī)則官網(wǎng)/{label}/{application}-{profile}.ymlmaster分支dev分支/{application}-{profile}.yml/{application}/{profile}[/{label}]重要配置細(xì)節(jié)總結(jié)成功實(shí)現(xiàn)了用SpringCloud Config通過(guò)GitHub獲取配置信息Config客戶(hù)端配置與測(cè)試新建cloud-config-client-3355POMbootstrap.yml是什么內(nèi)容說(shuō)明修改config-dev.yml配置并提交到GitHub中,比如加個(gè)變量age或者版本號(hào)version主啟動(dòng)業(yè)務(wù)類(lèi)測(cè)試啟動(dòng)Config配置中心3344微服務(wù)并自測(cè)啟動(dòng)3355作為Client準(zhǔn)備訪問(wèn)成功實(shí)現(xiàn)了客戶(hù)端3355訪問(wèn)SpringCloud Config3344通過(guò)GitHub獲取配置信息問(wèn)題隨時(shí)而來(lái),分布式配置的動(dòng)態(tài)刷新問(wèn)題Config客戶(hù)端之動(dòng)態(tài)刷新避免每次更新配置都要重啟客戶(hù)端微服務(wù)3355動(dòng)態(tài)刷新步驟修改3355模塊POM引入actuator監(jiān)控修改YML谚鄙,暴露監(jiān)控端口@RefreshScope業(yè)務(wù)類(lèi)Controller修改此時(shí)修改github---> 3344 ---->3355How需要運(yùn)維人員發(fā)送Post請(qǐng)求刷新3355再次想想還有什么問(wèn)題?
概述
分布式系統(tǒng)面臨的---配置問(wèn)題
微服務(wù)意味著要將單體應(yīng)用中的業(yè)務(wù)拆分成一個(gè)個(gè)子服務(wù),每個(gè)服務(wù)的粒度相對(duì)較小葛账,因此系統(tǒng)中會(huì)出現(xiàn)大量的服務(wù)。由于每個(gè)服務(wù)都需要必要的配置信息才能運(yùn)行皮仁,所以一套集中式的籍琳、動(dòng)態(tài)的配置管理設(shè)施是必不可少的。
SpringCloud提供了ConfigServer來(lái)解決這個(gè)問(wèn)題贷祈,我們每一個(gè)微服務(wù)自己帶著一個(gè)application.yml趋急,上百個(gè)配置文件的管理...... /(ㄒoㄒ)/~~
是什么
SpringCloud Config為微服務(wù)架構(gòu)中的微服務(wù)提供集中化的外部配置支持,配置服務(wù)器為各個(gè)不同微服務(wù)應(yīng)用的所有環(huán)境提供了一個(gè)中心化的外部配置势誊。
怎么玩 SpringCloud Config分為服務(wù)端和客戶(hù)端兩部分呜达。
服務(wù)端也稱(chēng)為分布式配置中心,它是一個(gè)獨(dú)立的微服務(wù)應(yīng)用粟耻,用來(lái)連接配置服務(wù)器并為客戶(hù)端提供獲取配置信息查近,加密/解密信息等訪問(wèn)接口
客戶(hù)端則是通過(guò)指定的配置中心來(lái)管理應(yīng)用資源漩怎,以及與業(yè)務(wù)相關(guān)的配置內(nèi)容,并在啟動(dòng)的時(shí)候從配置中心獲取和加載配置信息配置服務(wù)器默認(rèn)采用git來(lái)存儲(chǔ)配置信息嗦嗡,這樣就有助于對(duì)環(huán)境配置進(jìn)行版本管理勋锤,并且可以通過(guò)git客戶(hù)端工具來(lái)方便的管理和訪問(wèn)配置內(nèi)容。
能干嘛
集中管理配置文件
不同環(huán)境不同配置侥祭,動(dòng)態(tài)化的配置更新叁执,分環(huán)境部署比如dev/test/prod/beta/release
運(yùn)行期間動(dòng)態(tài)調(diào)整配置,不再需要在每個(gè)服務(wù)部署的機(jī)器上編寫(xiě)配置文件矮冬,服務(wù)會(huì)向配置中心統(tǒng)一拉取配置自己的信息
當(dāng)配置發(fā)生變動(dòng)時(shí)谈宛,服務(wù)不需要重啟即可感知到配置的變化并應(yīng)用新的配置
-
將配置信息以REST接口的形式暴露
5.1post、curl訪問(wèn)刷新均可......
與GitHub整合配置
由于SpringCloud Config默認(rèn)使用Git來(lái)存儲(chǔ)配置文件(也有其它方式,比如支持SVN和本地文件)胎署, 但最推薦的還是Git吆录,而且使用的是http/https訪問(wèn)的形式
官網(wǎng)
https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/
Config服務(wù)端配置與測(cè)試
用你自己的賬號(hào)在GitHub上新建一個(gè)名為springcloud-config的新Repository
由上一步獲得剛新建的git地址
git@github.com:zzyybs/springcloud-config.git
本地硬盤(pán)目錄上新建git倉(cāng)庫(kù)并clone
本地地址:D:\44\SpringCloud2020
git命令
git clone git@github.com:zzyybs/springcloud-config.git
此時(shí)在本地D盤(pán)符下D:\44\SpringCloud2020\springcloud-config
[圖片上傳失敗...(image-cb7e10-1646876641857)]
表示多個(gè)環(huán)境的配置文件
保存格式必須為UTF-8
如果需要修改,此處模擬運(yùn)維人員操作git和github
git add .暫存
git commit -m "init yml"提交到本地
git push origin master推送到master分支
新建Module模塊cloud-config-center-3344它即為Cloud的配置中心模塊cloudConfig Center
POM
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>mscloud</artifactId> <groupId>com.atguigu.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n53" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><artifactId>cloud-config-center-3344</artifactId></pre>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n55" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies></pre>
</project>
YML
server: port: 3344
spring: application: name: cloud-config-center #注冊(cè)進(jìn)Eureka服務(wù)器的微服務(wù)名 cloud: config: server: git: uri: git@github.com:zzyybs/springcloud-config.git #GitHub上面的git倉(cāng)庫(kù)名字 ####搜索目錄 search-paths: - springcloud-config ####讀取分支 label: master
服務(wù)注冊(cè)到eureka地址 eureka: client: service-url: defaultZone: http://localhost:7001/eureka
主啟動(dòng)類(lèi)(@EnableConfigServer)
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer;
/**
@auther zzyy
@create 2019-11-15 14:32 */ @SpringBootApplication @EnableConfigServer public class ConfigCenterMain3344 { public static void main(String[] args) { SpringApplication.run(ConfigCenterMain3344.class, args); } }
windows下修改hosts文件琼牧,增加映射
127.0.0.1 config-3344.com
測(cè)試通過(guò)Config微服務(wù)是否可以從GitHub上獲取配置內(nèi)容
啟動(dòng)微服務(wù)3344
http://config-3344.com:3344/master/config-dev.yml
配置讀取規(guī)則
官網(wǎng)
[圖片上傳失敗...(image-ea88e-1646876641857)]
/{label}/{application}-{profile}.yml
master分支
http://config-3344.com:3344/master/config-dev.yml
http://config-3344.com:3344/master/config-test.yml
http://config-3344.com:3344/master/config-prod.yml
dev分支
http://config-3344.com:3344/dev/config-dev.yml
http://config-3344.com:3344/dev/config-test.yml
http://config-3344.com:3344/dev/config-prod.yml
/{application}-{profile}.yml
http://config-3344.com:3344/config-dev.yml
http://config-3344.com:3344/config-test.yml
http://config-3344.com:3344/config-prod.yml
http://config-3344.com:3344/config-xxxx.yml(
/{application}/{profile}[/{label}]
http://config-3344.com:3344/config/dev/master
http://config-3344.com:3344/config/test/master
http://config-3344.com:3344/config/test/dev
重要配置細(xì)節(jié)總結(jié)
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n98" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> /{name}-{profiles}.yml</pre>
/{label}-{name}-{profiles}.yml
label:分支(branch) name :服務(wù)名 profiles:環(huán)境(dev/test/prod)
成功實(shí)現(xiàn)了用SpringCloud Config通過(guò)GitHub獲取配置信息
Config客戶(hù)端配置與測(cè)試
新建cloud-config-client-3355
POM
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>mscloud</artifactId> <groupId>com.atguigu.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n106" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><artifactId>cloud-config-client-3355</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies></pre>
</project>
bootstrap.yml
是什么
applicaiton.yml是用戶(hù)級(jí)的資源配置項(xiàng) bootstrap.yml是系統(tǒng)級(jí)的恢筝,優(yōu)先級(jí)更加高
Spring Cloud會(huì)創(chuàng)建一個(gè)“Bootstrap Context”,作為Spring應(yīng)用的Application Context
的父上下文巨坊。初始化的時(shí)候撬槽,Bootstrap Context
負(fù)責(zé)從外部源加載配置屬性并解析配置。這兩個(gè)上下文共享一個(gè)從外部獲取的Environment
趾撵。
Bootstrap
屬性有高優(yōu)先級(jí)侄柔,默認(rèn)情況下,它們不會(huì)被本地配置覆蓋占调。 Bootstrap context
和Application Context
有著不同的約定暂题,所以新增了一個(gè)bootstrap.yml
文件,保證Bootstrap Context
和Application Context
配置的分離究珊。
要將Client模塊下的application.yml文件改為bootstrap.yml,這是很關(guān)鍵的薪者, 因?yàn)閎ootstrap.yml是比application.yml先加載的。bootstrap.yml優(yōu)先級(jí)高于application.yml
內(nèi)容
server: port: 3355
spring: application: name: config-client cloud: #Config客戶(hù)端配置 config: label: master #分支名稱(chēng) name: config #配置文件名稱(chēng) profile: dev #讀取后綴名稱(chēng) 上述3個(gè)綜合:master分支上config-dev.yml的配置文件被讀取http://config-3344.com:3344/master/config-dev.yml uri: http://localhost:3344 #配置中心地址k
服務(wù)注冊(cè)到eureka地址 eureka: client: service-url: defaultZone: http://localhost:7001/eureka
說(shuō)明
[圖片上傳失敗...(image-99c2db-1646876641857)]
修改config-dev.yml配置并提交到GitHub中苦银,比如加個(gè)變量age或者版本號(hào)version
主啟動(dòng)
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
@auther zzyy
@create 2020-02-08 11:09 */ @EnableEurekaClient @SpringBootApplication public class ConfigClientMain3355 { public static void main(String[] args) { SpringApplication.run(ConfigClientMain3355.class,args); } }
業(yè)務(wù)類(lèi)
package com.atguigu.springcloud.controller;
import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.beans.factory.annotation.Value;
/**
@auther zzyy
-
@create 2019-11-15 15:00 */ @RestController public class ConfigClientController { @Value("${config.info}") private String configInfo;
@GetMapping("/configInfo") public String getConfigInfo() { return configInfo; } }
測(cè)試
啟動(dòng)Config配置中心3344微服務(wù)并自測(cè)
http://config-3344.com:3344/master/config-prod.yml
http://config-3344.com:3344/master/config-dev.yml
啟動(dòng)3355作為Client準(zhǔn)備訪問(wèn)
http://localhost:3355/configInfo
成功實(shí)現(xiàn)了客戶(hù)端3355訪問(wèn)SpringCloud Config3344通過(guò)GitHub獲取配置信息
問(wèn)題隨時(shí)而來(lái)啸胧,分布式配置的動(dòng)態(tài)刷新問(wèn)題
Linux運(yùn)維修改GitHub上的配置文件內(nèi)容做調(diào)整
刷新3344,發(fā)現(xiàn)ConfigServer配置中心立刻響應(yīng)
刷新3355幔虏,發(fā)現(xiàn)ConfigClient客戶(hù)端沒(méi)有任何響應(yīng)
3355沒(méi)有變化除非自己重啟或者重新加載
難到每次運(yùn)維修改配置文件纺念,客戶(hù)端都需要重啟?想括?噩夢(mèng)
Config客戶(hù)端之動(dòng)態(tài)刷新
避免每次更新配置都要重啟客戶(hù)端微服務(wù)3355
動(dòng)態(tài)刷新
步驟
修改3355模塊
POM引入actuator監(jiān)控
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
修改YML陷谱,暴露監(jiān)控端口
server: port: 3355
spring: application: name: config-client cloud: #Config客戶(hù)端配置 config: label: master #分支名稱(chēng) name: config #配置文件名稱(chēng) profile: dev #讀取后綴名稱(chēng) 上述3個(gè)綜合:master分支上config-dev.yml的配置文件被讀取 uri: http://localhost:3344 #配置中心地址k
服務(wù)注冊(cè)到eureka地址 eureka: client: service-url: defaultZone: http://localhost:7001/eureka
暴露監(jiān)控端點(diǎn)
management: endpoints: web: exposure: include: "*"
@RefreshScope業(yè)務(wù)類(lèi)Controller修改
package com.atguigu.springcloud.controller;
import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.beans.factory.annotation.Value;
/**
@auther zzyy
-
@create 2019-11-15 15:00 */ @RestController @RefreshScope public class ConfigClientController { @Value("${config.info}") private String configInfo;
@GetMapping("/configInfo") public String getConfigInfo() { return configInfo; } }
此時(shí)修改github---> 3344 ---->3355
http://localhost:3355/configInfo
3355改變沒(méi)有??烟逊? 沒(méi)有渣窜,/(ㄒoㄒ)/~~
How
需要運(yùn)維人員發(fā)送Post請(qǐng)求刷新3355
必須是POST請(qǐng)求
curl -X POST "http://localhost:3355/actuator/refresh"
[圖片上傳失敗...(image-e8c23e-1646876641857)]
再次
http://localhost:3355/configInfo
成功實(shí)現(xiàn)了客戶(hù)端3355刷新到最新配置內(nèi)容
避免了服務(wù)重啟
想想還有什么問(wèn)題?
假如有多個(gè)微服務(wù)客戶(hù)端3355/3366/3377宪躯。乔宿。。访雪。详瑞。。
每個(gè)微服務(wù)都要執(zhí)行一次post請(qǐng)求臣缀,手動(dòng)刷新坝橡?
可否廣播,一次通知精置,處處生效计寇?
我們想大范圍的自動(dòng)刷新,求方法