Spring Cloud Config配置中心是什么
在分布式系統中,尤其是當我們的分布式項目越來越多,每個項目都有自己的配置文件,對配置文件的統一管理就成了一種需要,而 Spring Cloud Config 就提供了對各個分布式項目配置文件的統一管理支持。Spring Cloud Config 也叫分布式配置中心,市面上開源的分布式配置中心有很多,比如國內的, 360 的QConf、淘寶的 diamond、百度的 disconf 都是解決分布式系統配置管理問題,國外也有很多開源的配置中心 Apache 的 Apache Commons Configuration、owner、cfg4j 等等;
Spring Cloud Config 是一個解決分布式系統的配置管理方案。它包含 Client和 Server 兩個部分,Server 提供配置文件的存儲、以接口的形式將配置文件的內容提供出去,Client 通過接口獲取數據、并依據此數據初始化自己的應用。
Spring cloud 使用 git 或 svn 存放配置文件,默認情況下使用 git。
構建一個 spring cloud config 配置中心按照如下方式進行:
1、創建一個普通的 Spring Boot 項目
2、在 pom.xml 文件中添加如下依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
3、在入口類,也就是 main 方法的類上添加注解 @EnableConfigServer
4、在 application.properties 中配置一下 git 倉庫信息,此處我們使用 GitHub( 也可以使用碼云 gitee ) , 首先在我的 Github 上創建一個名為spring-cloud-config 的項目,創建之后,再做如下配置:
server.port=3721
spring.application.name=07-springcloud-config-server
spring.cloud.config.server.git.uri=https://github.com/myspring/sprin
g-cloud-config.git
spring.cloud.config.server.git.search-paths=config-center
[email protected]
spring.cloud.config.server.git.password=xxxx123456
其中:
● uri 表示配置中心所在倉庫的位置
● search-paths 表示倉庫下的子目錄
● username 表示你的 GitHub 用戶名
● password 表示你的 GitHub 密碼
至此我們的配置中心服務端就創建好了。
接下來我們需要在 github 上設置好配置中心,首先在本地創建一個文件夾叫wkcto,然后在里面創建一個文件夾叫 config-center,然后在 config-center中創建四個配置文件,如下:
application.properties
application-dev.properties
application-test.properties
application-online.properties
在四個文件里面分別寫上要測試的內容:
url=http://www.wkcto.com
url=http://dev.wkcto.com
url=http://test.wkcto.com
url=http://online.wkcto.com
然后回到 wkcto 目錄下,依次執行如下命令將本地文件同步到 Github 倉庫中:
說明:在使用命令之前先從 https://git-scm.com/ 網站下載安裝 git 的 window 客戶端
1、添加提交人的賬號信息,git 需要知道提交人的信息作為標識;
git config --global user.name 'junge'
git config --global user.email '[email protected]'
2、將該目錄變為 git 可以管理的目錄;
git init
3、將文件添加到暫存區
git add config-center/
4、把文件提交到本地倉庫;
git commit -m 'add config-center'
5、添加遠程主機;
git remote add origin https://github.com/hnylj/spring-cloud-config.git
6、將本地的 master 分支推送到 origin 主機;
git push -u origin master
至此,我們的配置文件就上傳到 GitHub 上了。
此時啟動我們的配置中心,通過/{application}/{profile}/{label}就能訪問到我們的配置文件了;
其中:
{application}表示配置文件的名字,對應的配置文件即application,
{profile}表示環境,有dev、test、online及默認,
{label}
通過瀏覽器上訪問 http://localhost:3721/application/dev/master
返回的 JSON 格式的數據:
name 表示配置文件名 application 部分,profiles 表示環境部分,label 表示分支,version 表示 GitHub 上提交時產生的版本號,同時當我們訪問成功后,在控制臺會打印了相關的日志信息;當訪問成功后配置中心會通過 git clone 命令將遠程配置文件在本地也保存一份,以確保在 git 倉庫故障時我們的應用還可以繼續正常使用。
前面已經搭建好了配置中心的服務端,接下來我們來看看如何在客戶端應用中使用。
1、創建一個普通的 Spring Boot 工程 08-springcloud-config-client,并添加如下依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2、創建 bootstrap.properties 文件,用于獲取配置信息,文件內容如下:
(注意這些信息一定要放在 bootstrap.properties 文件中才有效)
server.port=3722
spring.application.name=application
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:3721/
其中:
name 對應配置文件中的 application 部分,profile 對應了 profile 部分,label 對應了 label 部分,uri 表示配置中心的地址。
3、創建一個 Controller 進行測試:
@RestController
@RefreshScope
public class ConfigController {
@Value("${url}")
http://www.wkcto.com
private String url;
@Autowired
private Environment env;
@RequestMapping("/cloud/url")
public String url () {
return this.url;
}
@RequestMapping("/cloud/url2")
public String url2 () {
return env.getProperty("url");
}
}
我們可以直接使用@Value 注解注入配置的屬性值,也可以通過 Environment對象來獲取配置的屬性值。
通過客戶端應用測試是否能夠獲取到配置中心配置的數據;
Spring cloud Config Server 的工作過程如下圖所示:
1、首先需要一個遠程 Git 倉庫,平時測試可以使用 GitHub,在實際生產環境中,需要自己搭建一個 Git 服務器,遠程 Git 倉庫的主要作用是用來保存我們的配置文件;
2、除了遠程 Git 倉庫之外,我們還需要一個本地 Git 倉庫,每當 Config Server訪問遠程 Git 倉庫時,都會克隆一份到本地,這樣當遠程倉庫無法連接時,就直接使用本地存儲的配置信息;
3、微服務 A、微服務 B 則是我們的具體應用,這些應用在啟動的時會從 ConfigServer 中獲取相應的配置信息;
4.當微服務 A、微服務 B 嘗試從 Config Server 中加載配置信息的時候,ConfigServer 會先通過 git clone 命令克隆一份配置文件保存到本地;
5、由于配置文件是存儲在 Git 倉庫中,所以配置文件天然具有版本管理功能;
生產環境中我們的配置中心肯定是不能隨隨便便被人訪問的,我們可以加上適當的保護機制,由于微服務是構建在 Spring Boot 之上,所以整合 Spring Security是最方便的方式。
1、在 springcloud config server 項目添加依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2、在 springcloud config server 項目的 application.properties 中配置用戶名密碼:
spring.security.user.name=wkcto
spring.security.user.password=123456
3、在 springcloud config client 上 bootstrap.properties 配置用戶名和密碼:
spring.cloud.config.username=wkcto
spring.cloud.config.password=123456
4、最后測試驗證;