歡迎來到菜鳥SpringCloud實戰入門系列(SpringCloudForNoob),該系列經過層層遞進的實戰視角,來一步步學習和理解SpringCloud。html
本系列適合有必定Java以及SpringBoot基礎的同窗閱讀。git
每篇文章末尾都附有本文對應的Github源代碼,方便同窗調試。github
Github倉庫地址:算法
github.com/qqxx6661/sp…spring
你能夠經過如下兩種途徑查看菜鳥SpringCloud實戰入門系列:bootstrap
前文回顧:後端
每一個項目都會有不少的配置文件,若是採用分佈式的開發模式,須要的配置文件隨着服務增長而不斷增多。配置中心即是解決此類問題的靈丹妙藥。安全
Spring Cloud Config核心功能:springboot
Spring Cloud Config項目是一個解決分佈式系統的配置管理方案。它包含了Client和Server兩個部分,server提供配置文件的存儲、以接口的形式將配置文件的內容提供出去,client經過接口獲取數據、並依據此數據初始化本身的應用。Spring cloud使用git或svn存放配置文件,默認狀況下使用git,咱們先以git爲例作一套示例。bash
在github上面建立了一個文件夾config-repo用來存放配置文件,爲了模擬生產環境,咱們建立如下三個配置文件:
// 開發環境
spring-cloud-config-dev.properties
// 測試環境
spring-cloud-config-test.properties
// 生產環境
spring-cloud-config-prod.properties
複製代碼
目錄爲:
每一個配置文件中都寫一個屬性config.hello,屬性值分別是 hello Im dev/test/pro 。
下面咱們開始配置server端,咱們新建一個模塊名爲config-server,建立模塊請參考教程第一章。
pom中添加依賴:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
複製代碼
而後修改配置文件:
這裏須要配置你本身的github或者別的git倉庫,而且須要填寫本身的帳戶密碼,你能夠fork個人springcloud_for_noob項目,在項目基礎上進行修改。
server:
port: 8769
spring:
application:
name: spring-cloud-config-server
cloud:
config:
server:
git:
uri: https://github.com/xxxxx(你本身的帳戶名)/springcloud_for_noob.git # 配置git倉庫的地址
search-paths: config-repo # git倉庫地址下的相對地址,能夠配置多個,用,分割。
username: xxxxxxxxx # git倉庫的帳號
password: xxxxxxx
複製代碼
Spring Cloud Config也提供本地存儲配置的方式。咱們只須要設置屬性spring.profiles.active=native,Config Server會默認從應用的src/main/resource目錄下檢索配置文件。也能夠經過spring.cloud.config.server.native.searchLocations=file:E:/properties/屬性來指定配置文件的位置。雖然Spring Cloud Config提供了這樣的功能,可是爲了支持更好的管理內容和版本控制的功能,仍是推薦使用git的方式。
啓動類添加@EnableConfigServer:
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
複製代碼
直接查看配置文件中的配置信息可訪問:
http://localhost:8769/spring-cloud-config-dev.properties
這樣服務端就創建好了,而且鏈接上了遠程配置文件倉庫。
server搞起來以後,最終仍是要在業務項目中去獲取server端的配置信息
咱們新建一個模塊名爲config-client,建立模塊請參考教程第一章。
pom中添加依賴:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--沒法引入:spring-cloud-config-server-->
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
</dependencies>
複製代碼
配置文件:
爲了方便起見,咱們迴歸使用properties文件
須要配置兩個配置文件:
application.properties以下:
server.port = 8771
spring.application.name = spring-cloud-config-client
複製代碼
bootstrap.properties以下:
spring.cloud.config.name = spring-cloud-config
spring.cloud.config.profile = dev
spring.cloud.config.uri = http://localhost:8769/
spring.cloud.config.label = master
複製代碼
特別注意:
上面這些與spring-cloud相關的屬性必須配置在bootstrap.properties中,config部份內容才能被正確加載。
由於config的相關配置會先於application.properties,而bootstrap.properties的加載也是先於application.properties。
啓動類:
啓動類不用附加註解,直接能夠開始了。
測試類HelloController:
爲了測試客戶端可否正確獲取參數,咱們建立一個測試類HelloController
最終測試:
啓動項目後訪問:http://localhost:8769/hello ,返回:
如圖所示,拿到了配置參數,咱們完成了客戶端的測試。
但實際中,如更改了配置並將其push到了git服務器上,咱們經過客戶端訪問,仍然會獲取舊的參數。這是由於springboot項目只有在啓動的時候纔會獲取配置文件的值,修改git信息後,client端並無再次去獲取。下一章咱們的目標就是使client實現刷新機制。
springcloud(六):配置中心git示例
www.ityouknow.com/springcloud…
菜鳥SpringCloud實戰入門專欄全導航:經過如下兩種途徑查看
我是蠻三刀把刀,後端開發。主要關注後端開發,數據安全,爬蟲等方向。
來微信和我聊聊:yangzd1102
Github我的主頁:
同步更新公衆號及如下所有博客:
1. Csdn
2. 知乎
3. 掘金
4. 簡書
若是文章對你有幫助,不妨收藏起來並轉發給您的朋友們~