本篇主要介紹的是SpringCloud中的分佈式配置中心(SpringCloud Config)的相關使用教程。git
Spring Cloud Config項目是一個解決分佈式系統的配置管理方案。它包含了Client和Server兩個部分,server提供配置文件的存儲、以接口的形式將配置文件的內容提供出去,client經過接口獲取數據、並依據此數據初始化本身的應用。github
開發環境spring
注:不必定非要用上述的版本,能夠根據狀況進行相應的調整。須要注意的是SpringBoot2.x之後,jdk的版本必須是1.8以上!bootstrap
確認了開發環境以後,咱們再來添加相關的pom依賴。瀏覽器
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
目前SpringCloud Config的使用主要是經過Git/SVN方式作一個配置中心,而後每一個服務從其中獲取自身配置所需的參數。SpringCloud Config也支持本地參數配置的獲取。若是使用本地存儲的方式,在 application.properties
或 application.yml
文件添加 spring.profiles.active=native
配置便可,它會從項目的 resources路徑下讀取配置文件。若是是讀取指定的配置文件,那麼可使用 spring.cloud.config.server.native.searchLocations = file:D:/properties/
來讀取。app
首先是服務端這塊,首先建立一個註冊中心,爲了進行區分,建立一個springcloud-config-eureka
的項目。 代碼和配置和以前的基本同樣。
application.properties
配置信息:分佈式
配置信息:測試
spring.application.name=springcloud-hystrix-eureka-server server.port=8005 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://localhost:8005/eureka/
配置說明:fetch
服務端這邊只須要在SpringBoot啓動類添加@EnableEurekaServer
註解就能夠了,該註解表示此服務是一個服務註冊中心服務。this
代碼示例:
@SpringBootApplication @EnableEurekaServer public class ConfigEurekaApplication { public static void main(String[] args) { SpringApplication.run(ConfigEurekaApplication.class, args); System.out.println("config 註冊中心服務啓動..."); } }
建立好了註冊中心以後,咱們再來建立一個配置中心,用於管理配置。
建立一個springcloud-config-server
的項目。而後在application.properties
配置文件添加以下配置:
配置信息:
spring.application.name=springcloud-config-server server.port=9005 eureka.client.serviceUrl.defaultZone=http://localhost:8005/eureka/ spring.cloud.config.server.git.uri = https://github.com/xuwujing/springcloud-study/ spring.cloud.config.server.git.search-paths = /springcloud-config/config-repo spring.cloud.config.server.git.username = spring.cloud.config.server.git.password =
配置說明:
注:若是想使用本地方式讀取配置信息,那麼只需將spring.cloud.config.server.git
的配置改爲spring.profiles.active=native
,而後在resources路徑下新增一個文件便可。
這裏爲了進行本地配置文件測試,新建一個configtest.properties
配置文件,添加以下內容:
word=hello world
代碼這塊也很簡單,在程序主類中,額外添加@EnableConfigServer
註解,該註解表示啓用config配置中心功能。代碼以下:
、、、
@EnableDiscoveryClient @EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); System.out.println("配置中心服務端啓動成功!"); } }
、、、
完成上述代碼以後,咱們的配置中心服務端已經構建完成了。
咱們新建一個springcloud-config-client
的項目,用於作讀取配置中心的配置。pom依賴仍是和配置中心同樣,不過須要新增一個配置,用於指定配置的讀取。
建立一個bootstrap.properties
文件,並添加以下信息:
配置信息:
spring.cloud.config.name=configtest spring.cloud.config.profile=pro spring.cloud.config.label=master spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.serviceId=springcloud-config-server eureka.client.serviceUrl.defaultZone=http://localhost:8005/eureka/
配置說明:
注:上面這些與spring-cloud相關的屬性必須配置在bootstrap.properties中,config部份內容才能被正確加載。由於bootstrap.properties的相關配置會先於application.properties,而bootstrap.properties的加載也是先於application.properties。須要注意的是eureka.client.serviceUrl.defaultZone
要配置在bootstrap.properties,否則客戶端是沒法獲取配置中心參數的,會啓動失敗!
application.properties配置
spring.application.name=springcloud-config-client server.port=9006
配置說明:
程序主類代碼,和以前的基本一致。代碼以下:
代碼示例:
@EnableDiscoveryClient @SpringBootApplication public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); System.out.println("配置中心客戶端啓動成功!"); } }
爲了方便查詢,在控制中進行參數的獲取,並返回。@Value
註解是默認是從application.properties
配置文件獲取參數,可是這裏咱們在客戶端並無進行配置,該配置在配置中心服務端,咱們只需指定好了配置文件以後便可進行使用。
代碼示例:
@RestController public class ClientController { @Value("${word}") private String word; @RequestMapping("/hello") public String index(@RequestParam String name) { return name+","+this.word; } }
到此,客戶端項目也就構建完成了。
完成如上的工程開發以後,咱們來進行測試。
首先咱們把springcloud-config-server
項目的application.properties
配置文件添加spring.profiles.active=native
配置,註釋掉spring.cloud.config.server.git
相關的配置,而後在src/main/resources目錄下新建一個configtest.properties
文件,而後在裏面添加一個配置word=hello world
。
添加完成以後,咱們依次啓動springcloud-config-eureka
、springcloud-config-server
、springcloud-config-client
這三個項目。啓動成功以前,先看來看看配置中心服務端的配置文件獲取,在瀏覽器輸入:
http://localhost:9005/configtest-1.properties
查看該文件的配置信息。
注:配置文件的名稱是configtest.properties
,可是若是直接該名稱的話是獲取不到的,由於在配置文件名須要經過-
來進行獲取,若是配置文件名稱沒有-
,那麼添加了-
以後,會自動進行匹配搜索。
springcloud config 的URL與配置文件的映射關係以下:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
上面的url會映射{application}-{profile}.properties對應的配置文件,{label}對應git上不一樣的分支,默認爲master。
界面返回:
word: hello world
而後調用客戶端的接口,查看是否可以獲取配置信息。在瀏覽器上輸入:
http://localhost:9006//hello?name=pancm
界面返回:
pancm,hello world
示例圖:
在完成本地測試以後,咱們把這個spring.profiles.active=native
配置註釋掉,解除spring.cloud.config.server.git
相關的註釋(帳號和密碼要填寫真實的),而後在git倉庫上創建一個config-repo 文件夾,新建configtest-pro.properties
、configtest-dev.properties
兩個配置,這兩個的配置分別是word=hello world!!
和word=hello world!
, 而後和configtest.properties
配置文件一塊兒上傳到config-repo 文件夾中。
首先在瀏覽器輸入:
http://localhost:9005/configtest-dev.properties
瀏覽器返回:
word: hello world!
而後再瀏覽器輸入:
http://localhost:9005/configtest-pro.properties
瀏覽器返回:
word: hello world!!
上傳了configtest.properties
文件,可是這個文件名稱沒有-
,咱們想獲取其中參數的信息的話,能夠在而後-
隨意添加一個參數,它會自動進行匹配,在瀏覽器輸入:
http://localhost:9005/configtest-1.properties
瀏覽器返回:
word: hello world
而後進行客戶端接口調用測試,在瀏覽器輸入:
http://localhost:9006/hello?name=pancm
瀏覽器返回:
pancm,Hello World!!
因爲這裏我配置的前綴是 pro ,因此讀取的是 configtest-pro.properties 文件的數據,想要獲取其餘的配置,修改spring.cloud.config.profile
配置便可。
示例圖:
基於SpringBoot2.x、SpringCloud的Finchley版本開發的地址:https://github.com/xuwujing/springcloud-study
基於SpringBoot1.x、SpringCloud 的Dalston版本開發的地址: https://github.com/xuwujing/springcloud-study-old
若是感受項目不錯,但願能給個star,謝謝!
原創不易,若是感受不錯,但願留言推薦!您的支持是我寫做的最大動力! 版權聲明: 做者:虛無境 博客園出處:http://www.cnblogs.com/xuwujing CSDN出處:http://blog.csdn.net/qazwsxpcm 我的博客出處:http://www.panchengming.com