一俊扭、概述
1、分布式系統(tǒng)面臨的配置問題
? 微服務(wù)意味著要將單體應(yīng)用中的業(yè)務(wù)拆分成一個個子服務(wù)坠陈,每個服務(wù)的粒度相對較小萨惑,因此系統(tǒng)中會出現(xiàn)大量的服務(wù),由于每個服務(wù)都需要必要的配置信息才能運行仇矾,所以一套集中式的庸蔼、動態(tài)的配置管理設(shè)施是必不可少的。SpringCloud提供了ConfigServer來解決這個問題贮匕,我們每一個微服務(wù)自己帶著一個application.yml姐仅,上百個配置文件的管理....
2、是什么刻盐?
? SpringCloud Config 為微服務(wù)架構(gòu)中的微服務(wù)提供集中化的外部配置支持掏膏,配置服務(wù)器為各個不同微服務(wù)應(yīng)用的所有環(huán)境提供了一個中心化的外部配置。
3敦锌、怎么玩馒疹?
? SpringCloud Config 分為服務(wù)端和客戶端兩部分。
- 服務(wù)端也稱為分布式配置中心乙墙,它是一個獨立的微服務(wù)應(yīng)用颖变,用來連接配置服務(wù)器并為客戶端提供獲取配置信息,加密/解密信息等訪問接口听想。
- 客戶端則是通過指定的配置中心來管理應(yīng)用資源腥刹,以及與業(yè)務(wù)相關(guān)的配置內(nèi)容,并在啟動的時候從配置中心獲取和加載配置信息配置服務(wù)器默認采用git來存儲配置信息汉买,這樣就有助于對環(huán)境配置進行版本管理衔峰,并且可以通過git客戶端工具來方便的管理和訪問配置內(nèi)容。
4、能干嗎垫卤?
(1)威彰、集中管理配置文件
(2)、不同環(huán)境不同配置葫男,動態(tài)化的配置更新抱冷,分環(huán)境部署比如dev/test/prod/beta/release
(3)、運行期間動態(tài)調(diào)整配置梢褐,不再需要在每個服務(wù)部署的機器上編寫配置文件旺遮,服務(wù)會向配置中心統(tǒng)一拉取配置自己的信息
(4)、當配置發(fā)生變動時盈咳,服務(wù)不需要重啟即可感知到配置的變化并應(yīng)用新的配置
(5)耿眉、將配置信息以REST接口的形式暴露
5、與GitHub整合配置
? 由于SpringCloud Config默認使用Git來存儲配置文件(也有其它方式鱼响,比如支持SVN和本地文件)鸣剪,但最推薦的還是Git,而且使用的是http/https訪問形式
二丈积、SpringCloud Config 服務(wù)端配置
(1)筐骇、用自己的GitHub賬號在GitHub 上新建一個名為 microservicecloud-config 的新Repository
(2)、由上一步獲得SSH協(xié)議的git地址:git@github.com:ll996960921/microservicecloud-config.git
(3)江滨、本地磁盤目錄上新建git倉庫clone
??? |--- 本地地址:E:\my-project\190728\mySpringCloud
??? |--- git 命令:git clone git@github.com:ll996960921/microservicecloud-config.git
(4)铛纬、本地 E:\my-project\190728\mySpringCloud 里面新建一個 application.yml
??? |--- yml內(nèi)容如下:spring: profiles: active: - dev --- spring: profiles: dev #開發(fā)環(huán)境 application: name: microservicecloud-config-ll-dev --- spring: profiles: test #測試環(huán)境 application: name: microservicecloud-config-ll-dev # 請保存為UTF-8格式
??? |--- 格式必須為UTF-8
(5)、將上一步的yml文件推送到github上
????? |--- 命令清單
??? |--- git add .
??? |--- git commit -m "init file"
??? |--- git push origin master
??? |--- 步驟結(jié)果:
????? |---GitHub(6)唬滑、新建Module模塊 microservicecloud-config-3344 它即為Cloud的配置中心模塊
(7)告唆、pom文件
??? |--- 修改內(nèi)容:<!-- springCloud Config --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
??? |--- 全部內(nèi)容:
<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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>cn.smilexl.springcloud</groupId> <artifactId>microservicecloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>microservicecloud-config-3344</artifactId> <dependencies> <!-- springCloud Config --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- 避免Config的Git插件報錯:>org/eclipse/jgit/api/TransportConfigCallback --> <dependency> <groupId>org.eclipse.jgit</groupId> <artifactId>org.eclipse.jgit</artifactId> <!-- <version>4.10.0.201712302008-r</version> --> </dependency> <!-- 圖形化監(jiān)控 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</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-test</artifactId> </dependency> <!-- 熱部署插件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
(8)、yml文件
server: port: 3344 spring: application: name: microservicecloud-config cloud: config: server: git: uri: https://github.com/ll996960921/microservicecloud-config.git #GitHub上面的git倉庫名字
(9)晶密、創(chuàng)建主啟動類 Config_3344_StartSpringCloudApp.java 并添加
@EnableConfigServer 注解package cn.smilexl.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer @SpringBootApplication public class Config_3344_StartSpringCloudApp { public static void main(String[] args) { SpringApplication.run(Config_3344_StartSpringCloudApp.class, args); } }
(10)擒悬、windows下修改host文件,增加映射 127.0.0.1 config-3344.com
(11)稻艰、測試通過 Config 微服務(wù)是否可以從 GitHub 上獲取配置內(nèi)容
???? |--- 啟動微服務(wù)3344
???? |--- http://config-3344.com:3344/application-dev.yml
???? |--- http://config-3344.com:3344/application-testyml
???? |--- http://config-3344.com:3344/application-xxx.yml(不存在的配置)(12)懂牧、配置讀取規(guī)則
???? |--- /{application}-{profile}.yml
?????? |--- http://config-3344.com:3344/application-dev.yml
?????? |--- http://config-3344.com:3344/application-test.yml
?????? |--- http://config-3344.com:3344/application-xxx.yml(不存在的配置)
???? |--- /{application}/{profile}/[/{label}]
?????? |--- http://config-3344.com:3344/application/dev/master
?????? |--- http://config-3344.com:3344/application/test/master
?????? |--- http://config-3344.com:3344/application/xxx/master
???? |--- /{label}/{application}-{profile}.yml
?????? |--- http://config-3344.com:3344/master/application-dev.yml
?????? |--- http://config-3344.com:3344/master/application-test.yml(13)、成功實現(xiàn)了用SpringCloud Config 通過Github 獲取配置信息
三连锯、SpringCloud Config 客戶端配置與測試
(1)归苍、在本地 E:\my-project\190728\mySpringCloud\microservicecloud-config 路徑下新建文件 microservicecloud-config-client.yml
(2)、 microservicecloud-config-client.yml 中添加如下內(nèi)容
spring: profiles: active: - dev --- server: port: 8201 spring: profiles: dev application: name: microservicecloud-config-client eureka: client: service-url: defaultZone: http://eureka-dev.com:7001/eureka/ --- server: port: 8202 spring: profiles: test application: name: microservicecloud-config-client eureka: client: service-url: defaultZone: http://eureka-test.com:7001/eureka/
(3)运怖、將上一步的 microservicecloud-config-client.yml 文件提交到GitHub上
(4)、新建 microservicecloud-config-client-3355
(5)夏伊、pom文件
??? |--- 修改內(nèi)容:<!-- springCloud Config客戶端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
??? |--- 全部內(nèi)容:
<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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>cn.smilexl.springcloud</groupId> <artifactId>microservicecloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>microservicecloud-config-client-3355</artifactId> <dependencies> <!-- springCloud Config客戶端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- 圖形化監(jiān)控 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</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-test</artifactId> </dependency> <!-- 熱部署插件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
(6)摇展、新建bootstrap.yml文件
??? |--- application.yml 是用戶級的資源配置項
??? |--- bootstrap.yml 是系統(tǒng)級的,優(yōu)先級更高
????? |--- SpringCloud 會創(chuàng)建一個 `Bootstrap Context`溺忧,作為Spring應(yīng)用的`Application Context`的父上下文咏连。初始化的時候盯孙,`Bootsrap Context`負責從外部源加載配置屬性并解析配置。這兩個上下文共享一個從外部獲取的`Environment`祟滴。`Bootstrap`屬性有高優(yōu)先級振惰,默認情況下,他們不會被本地配置覆蓋垄懂。`Bootstrap Context`和`Application Context`有著不同的約定骑晶,所以新增了一個`Bootstrap.yml`文件,保證`Bootstrap Context`和`Application Context`配置分離草慧。
????? |--- 內(nèi)容如下:spring: cloud: config: name: microservicecloud-config-client #需要從GitHub上讀取的資源名稱桶蛔,注意沒有yml后綴名 profile: dev #本次訪問的配置項 label: master uri: http://config-3344.com:3344 #本微服務(wù)啟動后先去找3344號服務(wù),通過SpringCloudConfig獲取GitHub的服務(wù)地址
(7)漫谷、新建application.yml文件
spring: application: name: microservicecloud-config-client
(8)仔雷、windows 下修改host文件,增加映射 127.0.0.1 client-config.com
(9)舔示、主啟動類 Config_3355_StartSpringCloudApp.java
package cn.smilexl.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Config_3355_StartSpringCloudApp { public static void main(String[] args) { SpringApplication.run(Config_3355_StartSpringCloudApp.class, args); } }
(10)碟婆、新建rest類,驗證是否能從GitHub上讀取配置
?? 類 ConfigClientRest.javapackage cn.smilexl.springcloud.res; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConfigClientRest { @Value("${spring.application.name}") private String applicationName; @Value("${eureka.client.service-url.defaultZone}") private String eurekaServers; @Value("${server.port}") private String port; @GetMapping("/config") public String getConfig() { String str = "appliactionName: "+applicationName+ "-------eurekaServers: " +eurekaServers+"-----------port: "+port; System.out.println("*******str: "+str); return str; } }
(11)惕稻、測試
???|--- 啟動Config配置中心3344微服務(wù)并自測:http://config-3344.com:3344/application-dev.yml
???|--- 啟動3355作為Client準備訪問
???|--- bootstrap.yml 里面的 profile 值是什么竖共,決定從GitHub上讀取什么
?????|--- 假如目前是 profile: dev
???????|--- dev默認在GitHub上對應(yīng)的端口是8201
???????|--- http://client-config.com:8201/config
?????|--- 假如目前是 profile: tes
???????|--- test默認在GitHub上對應(yīng)的端口是8202
???????|--- http://client-config.com:8202/config(12)、成功實現(xiàn)了客戶端3355訪問SpringCloud Config3344通過GitHub獲取配置信息
四缩宜、SpringCloud Config 配置實戰(zhàn)
1肘迎、目前情況
(1)、Config服務(wù)端配置锻煌,配置OK且測試通過妓布,我們可以和config+GitHub進行配置修改并且獲得內(nèi)容
(2)、此時我們做一個eureka服務(wù)+一個Dept訪問的微服務(wù)宋梧,將兩個微服務(wù)的配置統(tǒng)一由GitHub獲得實現(xiàn)統(tǒng)一配置分布式管理匣沼,完成多環(huán)境的變更。