Spring Cloud Spring Boot mybatis 企業分散式微服務雲(六)分散式配置中心【Dalston版】

allalongx發表於2018-02-07

Spring Cloud Config是Spring Cloud團隊建立的一個全新專案,用來為分散式系統中的基礎設施和微服務應用提供集中化的外部配置支援,它分為服務端與客戶端兩個部分。其中服務端也稱為分散式配置中心,它是一個獨立的微服務應用,用來連線配置倉庫併為客戶端提供獲取配置資訊、加密/解密資訊等訪問介面;而客戶端則是微服務架構中的各個微服務應用或基礎設施,它們通過指定的配置中心來管理應用資源與業務相關的配置內容,並在啟動的時候從配置中心獲取和載入配置資訊。

Spring Cloud Config實現了對服務端和客戶端中環境變數和屬性配置的抽象對映,所以它除了適用於Spring構建的應用程式之外,也可以在任何其他語言執行的應用程式中使用。由於Spring Cloud Config實現的配置中心預設採用Git來儲存配置資訊,所以使用Spring Cloud Config構建的配置伺服器,天然就支援對微服務應用配置資訊的版本管理,並且可以通過Git客戶端工具來方便的管理和訪問配置內容。當然它也提供了對其他儲存方式的支援,比如:SVN倉庫、本地化檔案系統。

準備配置倉庫

準備一個git倉庫,可以在碼雲或Github上建立都可以。比如本文準備的倉庫示例:http://git.oschina.net/didispace/config-repo-demo

假設我們讀取配置中心的應用名為config-client,那麼我們可以在git倉庫中該專案的預設配置檔案config-client.yml:

info: profile: default

為了演示載入不同環境的配置,我們可以在git倉庫中再建立一個針對dev環境的配置檔案config-client-dev.yml: info: profile: dev 構建配置中心

通過Spring Cloud Config來構建一個分散式配置中心非常簡單,只需要三步:

建立一個基礎的Spring Boot工程,命名為:config-server-git,並在pom.xml中引入下面的依賴(省略了parent和dependencyManagement部分): org.springframework.cloud spring-cloud-config-server

建立Spring Boot的程式主類,並新增@EnableConfigServer註解,開啟Spring Cloud Config的服務端功能。 @EnableConfigServer @SpringBootApplication public class Application {

public static void main(String[] args) {
	new SpringApplicationBuilder(Application.class).web(true).run(args);
}
複製程式碼

}

在application.yml中新增配置服務的基本資訊以及Git倉庫的相關資訊,例如: spring application: name: config-server cloud: config: server: git: uri: http://git.oschina.net/didispace/config-repo-demo/ server: port: 1201 到這裡,使用一個通過Spring Cloud Config實現,並使用Git管理配置內容的分散式配置中心就已經完成了。我們可以將該應用先啟動起來,確保沒有錯誤產生,然後再嘗試下面的內容。

如果我們的Git倉庫需要許可權訪問,那麼可以通過配置下面的兩個屬性來實現; spring.cloud.config.server.git.username:訪問Git倉庫的使用者名稱 spring.cloud.config.server.git.password:訪問Git倉庫的使用者密碼

完成了這些準備工作之後,我們就可以通過瀏覽器、POSTMAN或CURL等工具直接來訪問到我們的配置內容了。訪問配置資訊的URL與配置檔案的對映關係如下:

/{application}/{profile}[/{label}] /{application}-{profile}.yml 上面的url會對映{application}-{profile}.properties對應的配置檔案,其中{label}對應Git上不同的分支,預設為master。我們可以嘗試構造不同的url來訪問不同的配置內容,比如,要訪問master分支,config-client應用的dev環境,就可以訪問這個url:http://localhost:1201/config-client/dev/master,並獲得如下返回:

/{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties { "name": "config-client", "profiles": [ "dev" ], "label": "master", "version": null, "state": null, "propertySources": [ { "name": "http://git.oschina.net/didispace/config-repo-demo/config-client-dev.yml", "source": { "info.profile": "dev" } }, { "name": "http://git.oschina.net/didispace/config-repo-demo/config-client.yml", "source": { "info.profile": "default" } } ] } 我們可以看到該Json中返回了應用名:config-client,環境名:dev,分支名:master,以及default環境和dev環境的配置內容。

構建客戶端

在完成了上述驗證之後,確定配置服務中心已經正常運作,下面我們嘗試如何在微服務應用中獲取上述的配置資訊。

建立一個Spring Boot應用,命名為config-client,並在pom.xml中引入下述依賴: org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-config

建立Spring Boot的應用主類,具體如下: @SpringBootApplication public class Application {

public static void main(String[] args) {
	new SpringApplicationBuilder(Application.class).web(true).run(args);
}
複製程式碼

}

建立bootstrap.yml配置,來指定獲取配置檔案的config-server-git位置,例如: spring: application: name: config-client cloud: config: uri: http://localhost:1201/ profile: default label: master

server: port: 2001 上述配置引數與Git中儲存的配置檔案中各個部分的對應關係如下:

spring.application.name:對應配置檔案規則中的{application}部分 spring.cloud.config.profile:對應配置檔案規則中的{profile}部分 spring.cloud.config.label:對應配置檔案規則中的{label}部分 這裡需要格外注意:上面這些屬性必須配置在bootstrap.properties中,這樣config-server中的配置資訊才能被正確載入。

在完成了上面你的程式碼編寫之後,讀者可以將config-server-git、config-client都啟動起來,然後訪問http://localhost:2001/info ,我們可以看到該端點將會返回從git倉庫中獲取的配置資訊:

spring.cloud.config.uri:配置中心config-server的地址 { "profile": "default" }

另外,我們也可以修改config-client的profile為dev來觀察載入配置的變化。 原始碼來源:http://minglisoft.cn/honghu/technology.html

相關文章