掃二維碼與項目經理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網交流
作者:翟永超 2017-07-28 16:41:53
云計算
分布式 Spring Cloud Config為服務端和客戶端提供了分布式系統(tǒng)的外部化配置支持。配置服務器為各應用的所有環(huán)境提供了一個中心化的外部配置。它實現(xiàn)了對服務端和客戶端對Spring Environment和PropertySource抽象的映射,所以它除了適用于Spring構建的應用程序,也可以在任何其他語言運行的應用程序中使用。

十余年的郟縣網站建設經驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。全網營銷推廣的優(yōu)勢是能夠根據用戶設備顯示端的尺寸不同,自動調整郟縣建站的顯示方式,使網站能夠適用不同顯示終端,在瀏覽器中調整網站的寬度,無論在任何一種瀏覽器上瀏覽網站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)建站從事“郟縣網站設計”,“郟縣網站推廣”以來,每個客戶項目都認真落實執(zhí)行。
Spring Cloud Config為服務端和客戶端提供了分布式系統(tǒng)的外部化配置支持。配置服務器為各應用的所有環(huán)境提供了一個中心化的外部配置。它實現(xiàn)了對服務端和客戶端對Spring Environment和PropertySource抽象的映射,所以它除了適用于Spring構建的應用程序,也可以在任何其他語言運行的應用程序中使用。作為一個應用可以通過部署管道來進行測試或者投入生產,我們可以分別為這些環(huán)境創(chuàng)建配置,并且在需要遷移環(huán)境的時候獲取對應環(huán)境的配置來運行。
配置服務器默認采用git來存儲配置信息,這樣就有助于對環(huán)境配置進行版本管理,并且可以通過git客戶端工具來方便的管理和訪問配置內容。當然他也提供本地化文件系統(tǒng)的存儲方式,下面從這兩方面介紹如何使用分布式配置來存儲微服務應用多環(huán)境的配置內容。
構建Config Server
通過Spring Cloud構建一個Config Server,非常簡單,只需要三步:
org.springframework.boot spring-boot-starter-parent 1.3.5.RELEASE org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-dependencies Brixton.RELEASE pom import
- @EnableConfigServer
- @SpringBootApplication
- public class Application {
- public static void main(String[] args) {
- new SpringApplicationBuilder(Application.class).web(true).run(args);
- }
- }
- spring.application.name=config-server
- server.port=7001
- # git管理配置
- spring.cloud.config.server.git.uri=http://git.oschina.net/didispace/SpringBoot-Learning/
- spring.cloud.config.server.git.searchPaths=Chapter9-1-4/config-repo
- spring.cloud.config.server.git.username=username
- spring.cloud.config.server.git.password=password
到這里,使用一個通過Spring Cloud Config實現(xiàn),并使用git管理內容的配置中心已經完成了,啟動該應用,成功后開始下面的內容。
Spring Cloud Config也提供本地存儲配置的方式。我們只需要設置屬性spring.profiles.active=native,Config Server會默認從應用的src/main/resource目錄下檢索配置文件。也可以通過spring.cloud.config.server.native.searchLocations=file:F:/properties/屬性來指定配置文件的位置。雖然Spring Cloud Config提供了這樣的功能,但是為了支持更好的管理內容和版本控制的功能,還是推薦使用git的方式。
服務端驗證
為了驗證上面完成的配置服務器,在http://git.oschina.net/didispace/SpringBoot-Learning/Chapter9-1-4/ 下創(chuàng)建了一個config-repo目錄作為配置倉庫,并根據不同環(huán)境新建了下面四個配置文件:
其中設置了一個from屬性,為每個配置文件分別設置了不同的值,如:
為了測試版本控制,在master中,我們都加入1.0的后綴,同時創(chuàng)建一個config-label-test分支,并將各配置文件中的值用2.0作為后綴。
完成了這些準備工作之后,我們就可以通過瀏覽器或POSTMAN等工具直接來訪問到我們的配置內容了。
URL與配置文件的映射關系如下:
上面的url會映射{application}-{profile}.properties對應的配置文件,{label}對應git上不同的分支,默認為master。
我們可以嘗試構造不同的url來訪問不同的配置內容,比如:要訪問config-label-test分支,didispace應用的prod環(huán)境,可以通過這個url:http://localhost:7001/didispace/prod/config-label-test
- {
- "name": "didispace",
- "profiles": [
- "prod"
- ],
- "label": "config-label-test",
- "version": "19de8a25575a7054a34230f74a22aa7f5575a9d1",
- "propertySources": [
- {
- "name": "http://git.oschina.net/didispace/SpringBoot-Learning/Chapter9-1-4/config-repo/didispace-prod.properties",
- "source": {
- "from": "git-prod-2.0"
- }
- },
- {
- "name": "http://git.oschina.net/didispace/SpringBoot-Learning/Chapter9-1-4/config-repo/didispace.properties",
- "source": {
- "from": "git-default-2.0"
- }
- }
- ]
- }
微服務端映射配置
在完成并驗證了配置服務中心之后,下面看看我們如何在微服務應用中獲取配置信息。
org.springframework.boot spring-boot-starter-parent 1.3.5.RELEASE org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-dependencies Brixton.RELEASE pom import
- @SpringBootApplication
- public class Application {
- public static void main(String[] args) {
- new SpringApplicationBuilder(Application.class).web(true).run(args);
- }
- }
- spring.application.name=didispace
- spring.cloud.config.profile=dev
- spring.cloud.config.label=master
- spring.cloud.config.uri=http://localhost:7001/
- server.port=7002
這里需要格外注意:上面這些屬性必須配置在bootstrap.properties中,config部分內容才能被正確加載。因為config的相關配置會先于application.properties,而bootstrap.properties的加載也是先于application.properties。
- @RefreshScope
- @RestController
- class TestController {
- @Value("${from}")
- private String from;
- @RequestMapping("/from")
- public String from() {
- return this.from;
- }
- }
通過@Value("${from}")綁定配置服務中配置的from屬性。
啟動該應用,并訪問:http://localhost:7002/from ,我們就可以根據配置內容輸出對應環(huán)境的from內容了。
完整示例:Chapter9-1-4

我們在微信上24小時期待你的聲音
解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網交流