最近一緻在更新Spring Cloud Config的相關内容,主要也是為這篇埋個伏筆,相信不少調研過Spring Cloud Config的使用者都會吐槽它的管理能力太弱。是以,就有了下面為講推薦的這個開源項目,希望對已經入坑Spring Cloud Config的童鞋們有所幫助!
簡介
在Spring Cloud的微服務架構方案中雖然提供了Spring Cloud Config來擔任配置中心的角色,但是該項目的功能在配置的管理層面還是非常欠缺的。初期我們可以依賴選取的配置存儲系統(比如:Gitlab、Github)給我們提供的配置管理界面來操作所有的配置資訊,但是這樣的管理還是非常粗粒度的,是以這個項目的目的就是解決這個問題,通過此項目,我們将提供一套基于Spring Cloud Config配置中心的可視化管理系統。
在該項目中,我們對于服務治理、配置存儲、可視化操作都做了抽象,隻要目的就是為了盡可能的相容所有Spring Cloud Config的使用者。任何Spring Cloud Config僅需要通過一些簡單的配置,或者遷移工具就能将原來正在使用的配置中心統一的管理起來。
項目位址
- Github: https://github.com/dyc87112/spring-cloud-config-admin
- Gitee:https://gitee.com/didispace/spring-cloud-config-admin
- 前端Github: https://github.com/stone-jin/spring-cloud-config-admin-web
- 前端Gitee: https://gitee.com/stone-jin/spring-cloud-config-admin-web
架構概覽
本項目采用了前後端分離的架構,通過core子產品抽象了前端需要的操作,再通過persistence和discovery子產品隔離不同的配置倉庫和不同的服務注冊中心,進而達到前端程式不需要關心到底使用了什麼存儲配置以及使用了什麼注冊中心,這樣使用者可以根據自己的需要自由的組合不同的配置存儲和服務治理機制,盡可能的比對大部分Spring Cloud使用者的需求。

部署方式
由于SCCA的架構對各個功能子產品做了比較細緻的拆分,是以它存在多種不同模式的部署方式,是以它既可以為已經在使用Spring Cloud Config提供服務,也可以為從零開始使用Spring Cloud Config的使用者。
在SCCA中我們的可部署内容自底向上分為三個部分:
-
:基于Spring Cloud Config建構的配置中心服務端。SpringCloud配置中心
-
:SCCA的核心子產品,實作了SCCA配置管理的持久化内容以及所有的管理操作API。SCCA REST服務端
-
:SCCA的前端子產品,實作了可視化的配置管理操作界面。SCCA UI服務端
下面我們來看看SCCA支援哪些多樣的部署方式。
全分離模式
全分離模式就是将上述三個部分都以獨立的程序進行部署,每一個部分都可以做高可用,具體部署結構可以如下圖所示:
這種模式既可以适用于已經在使用Spring Cloud Config的使用者,也适用于正準備開始适用的使用者。其中,位于最底層的
SpringCloud配置中心
就是一個最原始的Spring Cloud Config Server。是以,對于已經在使用Spring Cloud Config的使用者隻需要再部署一套
SCCA REST服務端
和
SCCA UI服務端
,并做一些配置就可以使用SCCA來管理所有的配置資訊了。
半分離模式
所謂的半分離模式就是将上述的三個子產品中的兩個進行組合部署,以降低複雜度的部署方式。SCCA UI子產品與SCCA REST子產品合并
如下圖所示,我們可以将
SCCA UI服務端
與
SCCA REST服務端
組合在一個程式中來部署,這樣可以有效的降低全分離模式的部署複雜度,同時對于已經在使用Spring Cloud Config的使用者來說非常友好,已經部署的配置中心可以繼續沿用。
注意:對接不同存儲配置中心的配置參考分離部署中兩個SCCA REST服務端的不同配置内容進行調整。
All-In-One模式
最後介紹一種比較暴力的使用模式,SCCA支援将所有三個子產品整合在一起使用和部署,在一個Spring Boot應用中同時包含:
SpringCloud配置中心
、
SCCA REST服務端
以及
SCCA UI服務端
,具體如下所示:
配置詳解
本章節分别對三個核心子產品的建構方式以及核心的配置内容。下面所有的建構都是基于Spring Boot建構的,是以您需要對Spring Boot項目的建構有基本的認識,這裡不做介紹。
Spring Cloud配置中心的建構與配置
在SCCA的架構中,配置中心的核心完全采用Spring Cloud Config,是以如何建構一個配置中心完全遵循Spring Cloud Config的使用方法。由于目前SCCA的REST子產品主要實作了對Git存儲和DB存儲的綜合管理,是以對于Spring Cloud Config的使用也隻能支援這兩種模式。下面分别介紹兩種配置中心的搭建與配置。
Git存儲模式
這裡主要介紹幾種主要的并且SCCA能夠比較好支援的配置模式:
第一種:多個項目使用多個不同Git倉庫存儲的模式
-
spring.cloud.config.server.git.uri=https://github.com/dyc87112/{application}.git
-
spring.cloud.config.server.git.username=
-
spring.cloud.config.server.git.password=
這種模式下不同的項目會對應的不同的Git倉庫,如果項目中
spring.application.name=user-service
,那麼它的配置倉庫會定位到
https://github.com/dyc87112/user-service.git
倉庫下的配置。配置檔案按
application-{profile}.properties
的格式存儲,
{profile}
代表環境名。
第二種:多個項目公用一個Git倉庫不同目錄的存儲模式
-
spring.cloud.config.server.git.uri=https://github.com/dyc87112/config-repo.git
-
spring.cloud.config.server.git.search-paths=/{application}
-
spring.cloud.config.server.git.username=
-
spring.cloud.config.server.git.password=
這種模式下不同的項目會對應到
https://github.com/dyc87112/config-repo.git
倉庫下的不同目錄,如果項目中
spring.application.name=user-service
https://github.com/dyc87112/config-repo.git
倉庫下的
/user-service
目錄。配置檔案按
application-{profile}.properties
{profile}
Db存儲模式
在使用Db存儲模式的時候,必須使用Spring Cloud的Edgware版本以上。比如,可以采用下面的配置:
-
# config server with jdbc
-
spring.profiles.active=jdbc
-
spring.cloud.config.server.jdbc.sql=SELECT `p_key`, `p_value` FROM property a, project b, env c, label d where a.project_id=b.id and a.env_id=c.id and a.label_id=d.id and b.name=? and c.name=? and d.name=?
-
# Datasource, share with scca-rest-server
-
spring.datasource.url=jdbc:mysql://localhost:3306/config-db
-
spring.datasource.username=root
-
spring.datasource.password=
-
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
主要分為兩個部分:
- 激活采用DB存儲的模式:将
設定為jdbc,同時指定擷取配置的SQL,使用者直接複制采用一樣的配置即可。spring.profiles.active
- 指定存儲配置的DB連接配接資訊,除了mysql之外也可以使用其他主流關系型資料庫。
這裡需要注意的,使用的DB要與後續介紹的SCCA REST子產品采用同一個DB
SCCA REST服務端的建構與配置
在建構SCCA REST服務端的時候針對對接不同的配置存儲有一些不同的配置要求,是以下面按目前支援的存儲模式做不同的介紹。
當對接的配置中心采用Git存儲的時候,需要引入以下核心依賴:
-
<dependency>
-
<groupId>com.didispace</groupId>
-
<artifactId>scca-rest</artifactId>
-
<version>1.0.0-RELEASE</version>
-
</dependency>
-
<!-- scca persistence dependency -->
-
<dependency>
-
<groupId>com.didispace</groupId>
-
<artifactId>scca-persistence-git</artifactId>
-
<version>1.0.0-RELEASE</version>
-
</dependency>
需要按如下配置:
-
# if config server use git, need config these properties
-
scca.git.username=
-
scca.git.password=
-
scca.git.repo-uri=https://github.com/dyc87112/{application}.git
-
scca.git.base-path=
-
scca.git.file-pattern=application-{profile}.properties
-
# Datasource
-
spring.datasource.url=jdbc:mysql://localhost:3306/config-db
-
spring.datasource.username=root
-
spring.datasource.password=
-
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
主要兩部分:
- 對接的git存儲的配置:
-
:通路git的使用者名scca.git.username
-
:通路git的密碼scca.git.password
-
:配置git倉庫的位址,與配置中心的scca.git.repo-uri
配置一緻spring.cloud.config.server.git.uri
-
:配置檔案存儲的相對路徑,與配置中心的scca.git.base-path
spring.cloud.config.server.git.search-paths
-
:配置檔案的命名規則scca.git.file-pattern
- SCCA内部邏輯的存儲庫資料源資訊
-
<dependency>
-
<groupId>com.didispace</groupId>
-
<artifactId>scca-rest</artifactId>
-
<version>1.0.0-RELEASE</version>
-
</dependency>
-
<!-- scca persistence dependency -->
-
<dependency>
-
<groupId>com.didispace</groupId>
-
<artifactId>scca-persistence-db</artifactId>
-
<version>1.0.0-RELEASE</version>
-
</dependency>
-
# Datasource
-
spring.datasource.url=jdbc:mysql://localhost:3306/config-db
-
spring.datasource.username=root
-
spring.datasource.password=
-
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
需要注意,當配置中心采用DB存儲的時候,這裡的資料源需要一緻
服務發現支援
如果SCCA REST子產品在通路配置中心的時候基于服務發現的話還需要引入對應的支援依賴和配置
與Eureka的整合
如果使用eureak,那麼需要引入如下依賴:
-
<!-- scca discovery dependency-->
-
<dependency>
-
<groupId>com.didispace</groupId>
-
<artifactId>scca-discovery-eureka</artifactId>
-
<version>1.0.0-RELEASE</version>
-
</dependency>
并且在配置中加入eureka的配置,比如:
-
eureka.client.serviceUrl.defaultZone=http://eureka.didispace.com/eureka/
更多相關配置請參與Spring Cloud Netflix Eureka的配置文檔。
與Consul的整合
如果使用consul,那麼需要引入如下依賴:
-
<!-- scca discovery dependency-->
-
<dependency>
-
<groupId>com.didispace</groupId>
-
<artifactId>scca-discovery-consul</artifactId>
-
<version>1.0.0-RELEASE</version>
-
</dependency>
并且在配置中加入consul的相關配置,比如:
-
spring.cloud.consul.host=localhost
-
spring.cloud.consul.port=8500
更多相關配置請參與Spring Cloud Consul的配置文檔。
公共配置
SCCA REST子產品還有一個特别的配置
scca.rest.context-path=/xhr
,該配置主要用來配置所有SCCA REST子產品接口的字首,該接口主要用于與SCCA UI子產品對接時候使用,兩邊必須對接一緻才能順利對接。
SCCA UI服務端的建構與配置
SCCA UI服務端需要引入以下核心依賴:
-
<dependency>
-
<groupId>com.didispace</groupId>
-
<artifactId>scca-ui</artifactId>
-
<version>1.0.0-RELEASE</version>
-
</dependency>
另外,還需要在配置中指定具體要通路的SCCA REST子產品的位置,主要有兩種模式:
- 指定位址的配置:
-
scca.ui.rest-server-url=http://localhost:10130
- 基于服務發現的配置:
-
scca.ui.rest-server-name=scca-rest-server
除了上面的配置之後,還需要引入eureka或consul的依賴以及做對應的配置
最後,還有一個
scca.ui.rest-server-context-path=/xhr
配置,用來描述要通路的SCCA REST子產品接口的字首,與SCCA REST服務端的
scca.rest.context-path=/xhr
配置相對應。
管理功能
通過之前介紹的任何一個部署方式搭建了配置中心和管理端之後,我們就可以打開浏覽器通路我們的UI子產品實作對配置中心的管理了。
通路位址為:
http://localhost:10032/admin/
,ip與端口根據實際部署UI子產品的情況進行調整。
系統配置
在管理各個項目的配置之前,我們需要先做一些基礎配置,比如:環境的配置、環境所屬的參數配置,加密相關的配置等。
-
環境配置
環境配置主要用來維護要使用SCCA統一管理的環境以及對應的Spring Cloud Config服務端資訊。
如上圖所示,通過“新增環境”按鈕可以添加一個部署環境。當我們使用了Eureka、Consul等注冊中心時,隻需要配置注冊中心的通路位址和配置中心的服務名以及配置中心通路的字首,後續就可以友善的使用這個環境的配置中心來進行加密解密、拉取配置等一系列的操作了。
如果不采用服務發現的機制取找到配置中心,也可以将注冊中心位址留白,配置中心服務名一欄直接配置通路注冊中心的URL即可。
-
環境參數配置
環境參數配置主要用來配置每個環境所屬的一些特有配置資訊,比如:redis的位址,eureka的位址等等。這些配置資訊将使用者後續為各項目在各個環境配置的時候給予參考和快捷的替換操作提供中繼資料。
- 加密管理
加密管理主要用來維護一些通常需要加密的Key,這樣可以在後續編輯配置内容的時候,友善的進行批量加密操作。
- 配置中心
在完成了上面的系統配置之後,使用者就可以進入配置中心子產品,這裡會提供具體的管理配置内容的功能。目前主要有兩部分組成:項目管理和配置管理。
-
項目管理
項目管理主要用來維護需要在各個環境部署的應用的配置資訊,這裡可以維護這個項目需要部署在什麼環境,有多少配置的版本。
這裡的三個基本概念與Spring Cloud Config的幾個概念的對應關系如下:
- 項目名稱:application
- 部署環境:profile
- 配置版本:label
這裡配置版本(label),我們會預設采用 master
。需要同時存在多個配置版本,實作灰階配置的時候,使用者也可以自己添加label。
-
配置管理
配置管理功能是SCCA的核心,在這裡使用者可以友善對各個應用、各個環境、各個版本的配置進行編輯、加密等操作。同時,也提供了一些快捷的操作,比如:根據環境參數配置一鍵替換、根據加密Key清單實作一鍵加密、通過配置中心可以加載到的配置資訊等(更多便捷功能持續添加中...)'
用戶端接入
本頁主要提供給沒有使用過Spring Cloud Config的使用者閱讀。如果您已經使用過Spring Cloud Config,那麼用戶端如何通過Spring Cloud Config的配置中心加載配置相信已經掌握,在使用本項目的時候,無非就是搭建SCCA-REST子產品和SCCA-UI子產品來幫助管理您目前的配置内容。
用戶端加載
通過前面幾節内容,如果您已經完成了SCCA中幾個要素的搭建,下面就來看看如何建立一個Spring Boot項目并通過配置中心來加載配置資訊。
-
絕對位址接入
1. 建立一個基本的Spring Boot項目,并在pom.xml中引入依賴
-
<dependency>
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-starter-config</artifactId>
-
</dependency>
2. 建立應用主類
-
@SpringBootApplication
-
public class Application {
-
public static void main(String[] args) {
-
new SpringApplicationBuilder(Application.class).web(true).run(args);
-
}
-
}
3. 建立
bootstrap.properties
配置檔案(也可以使用yaml可以)
-
spring.application.name=config-client
-
server.port=12000
-
spring.cloud.config.uri=http://localhost:10032/scca-config-server
-
spring.cloud.config.profile=stage
-
spring.cloud.config.label=master
上述配置參數與scca中維護元素的對應關系如下:
- spring.application.name:對應scca中的項目名
- spring.cloud.config.profile:項目配置的環境名
- spring.cloud.config.label:項目配置的版本名
- spring.cloud.config.uri:配置中心的通路絕對位址
-
服務發現接入
-
<dependency>
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-starter-config</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-starter-eureka</artifactId>
-
</dependency>
上面以通過eureka做注冊中心的依賴,如果用consul,隻需要将換成
spring-cloud-starter-eureka
即可。
spring-cloud-starter-consul-discovery
-
@EnableDiscoveryClient
-
@SpringBootApplication
-
public class Application {
-
public static void main(String[] args) {
-
new SpringApplicationBuilder(Application.class).web(true).run(args);
-
}
-
}
bootstrap.properties
-
spring.application.name=config-client
-
server.port=12000
-
spring.cloud.config.discovery.enabled=true
-
spring.cloud.config.discovery.serviceId=config-server
-
spring.cloud.config.profile=stage
-
spring.cloud.config.label=master
- spring.cloud.config.discovery.enabled:開啟服務發現功能
- spring.cloud.config.discovery.serviceId:配置中心的服務名
-
讀取配置
通過上面的兩種方式從配置中心拉取配置之後,在Spring Boot項目中就可以輕松的使用所有配置内容了,比如:
-
@RefreshScope
-
@RestController
-
public class TestController {
-
@Value("${a.b.c}")
-
private String abc;
-
@RequestMapping("/abc")
-
public String abc() {
-
return this.abc;
-
}
-
}
兩個主要注解的說明:
-
:讀取配置key為@Value("${a.b.c}")
的value值a.b.c
-
:下面的配置資訊可以通過@RefreshScope
端點實作動态重新整理/refresh
其他參考
如果您還不了解Spring Cloud Config,您也可以閱讀下面的幾篇了解一下最原始的Spring Cloud Config配置中心和用戶端接入方式
- Spring Cloud建構微服務架構:分布式配置中心
- Spring Cloud建構微服務架構:分布式配置中心(加密與解密)
- END -