上篇文章我們看了Spring Cloud中分布式配置中心的一個基本使用,這里邊還涉及到許多細節,本文我們就來看看服務端配置中的一些細節。
本文是Spring Cloud系列的第二十三篇文章,了解前二十二篇文章內容有助于更好的理解本文:
1.使用Spring Cloud搭建服務注冊中心
2.使用Spring Cloud搭建高可用服務注冊中心
3.Spring Cloud中服務的發現與消費
4.Eureka中的核心概念
5.什么是客戶端負載均衡
6.Spring RestTemplate中幾種常見的請求方式
7.RestTemplate的逆襲之路,從發送請求到負載均衡
8.Spring Cloud中負載均衡器概覽
9.Spring Cloud中的負載均衡策略
10.Spring Cloud中的斷路器Hystrix
11.Spring Cloud自定義Hystrix請求命令
12.Spring Cloud中Hystrix的服務降級與異常處理
13.Spring Cloud中Hystrix的請求緩存
14.Spring Cloud中Hystrix的請求合并
15.Spring Cloud中Hystrix儀表盤與Turbine集群監控
16.Spring Cloud中聲明式服務調用Feign
17.Spring Cloud中Feign的繼承特性
18.Spring Cloud中Feign配置詳解
19.Spring Cloud中的API網關服務Zuul
20.Spring Cloud Zuul中路由配置細節
21.Spring Cloud Zuul中異常處理細節
22.分布式配置中心Spring Cloud Config初窺
我們先通過下面一張圖來看看Config Server的一個大致工作過程:
結合這張圖,我來說如下五點:
1.首先我們需要一個遠程的Git倉庫,自己學習可以直接用GitHub,在在實際生產環境中,需要自己搭建一個Git服務器,遠程Git倉庫的作用主要是用來保存我們的配置文件
2.除了遠程Git倉庫之外,我們還需要一個本地Git倉庫,每當Config Server訪問遠程Git倉庫時,都會保存一份到本地,這樣當遠程倉庫無法連接時,就直接使用本地存儲的配置信息
3.至于微服務A、微服務B則是我們具體的應用,這些應用在啟動的時候會從Config Server中來加載相應的配置信息
4.當微服務A/B嘗試去從Config Server中加載配置信息的時候,Config Server會先通過git clone命令克隆一份配置文件保存到本地
5.由于配置文件是存儲在Git倉庫中,所以配置文件天然的具備版本管理功能,Git中的Hook功能可以實時監控配置文件的修改
Git URI中的占位符
靈活的使用URI占位符,可以有效的減少我們的工作量。考慮這樣一個問題,我有ServerA、ServerB兩個服務,兩個服務對應的配置文件的存儲地址分別位于https://github.com/lenve/scCo...和https://github.com/lenve/scCo...,但是我的Config Server只有一個,那么當我的ServerA和ServerB連接上Config Server時,Config Server怎么知道去哪個地址下拿配置文件?這個時候就涉及到占位符的使用。
在上篇文章中我們已經了解了Spring Cloud Config中的三種占位符,分別是{application}、{profile}和{label},這些占位符除了用來標識配置文件的規則,還可以用在Config Server中對Git倉庫的URI配置,用在URI配置中時,這三個占位符的含義分別如下所示:
1.{application}映射到客戶端的 spring.application.name
2.{profile}映射到客戶端上的 spring.profiles.active
3.{label}這是一個服務器端功能,標記"版本"的配置文件集
此時,假設我不同環境下的配置文件分別放在下面這些目錄下:
https://github.com/lenve/scCo...
https://github.com/lenve/scCo...
https://github.com/lenve/scCo...
那么我的客戶端文件這樣配置:
spring.application.name=app
# dev根據具體情況來修改
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:2007/
server.port=2008
然后Config Server按下面這種方式配置即可:
spring.cloud.config.server.git.uri=https://github.com/lenve/scConfig.git
spring.cloud.config.server.git.search-paths={application}/{profile}
當然這種存儲規劃不一定最佳,這里只是給小伙伴們演示占位符的用法。
默認情況下,Config Server 克隆下來的文件保存在C:Users<當前用戶>AppDataLocalTemp目錄下,我們可以通過如下配置來修改:
spring.cloud.config.server.git.basedir=E:\\111\\
健康監測
默認情況下Spring Cloud Config會為配置中心服務端創建一個健康監測器,該檢測器默認情況下是訪問的倉庫文件是{application}為app的配置文件,如果倉庫中不存在這個文件,健康顯示器就會顯示倉庫無法連接,此時我們有兩種解決方案:1.倉庫中添加相應的配置文件;2.重新指定檢測的配置,重新指定方式如下:
spring.cloud.config.server.health.repositories.check.name=app
spring.cloud.config.server.health.repositories.check.label=master
spring.cloud.config.server.health.repositories.check.profiles=dev
此時,系統回去訪問http://localhost:2007/app/dev/master地址,如果能夠訪問到,則顯示倉庫已連接,如下:
安全保護
開發環境中我們的配置中心肯定是不能隨隨便便被人訪問的,我們可以加上適當的保護機制,由于微服務是構建在Spring Boot之上,所以整合Spring Security是最方便的方式。
首先添加依賴:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>
然后在application.properties中配置用戶名密碼:
security.user.name=sang
security.user.password=123
最后在配置中心的客戶端上配置用戶名和密碼即可,如下:
spring.cloud.config.username=sang
spring.cloud.config.password=123
OK,如此之后,其他人就不能隨意的獲取到我們的配置信息了。OK,本文就先說到這里,有問題歡迎留言討論。
參考資料:
1.《Spring Cloud微服務實戰》
更多JavaEE資料請關注公眾號: