天天看點

Spring Cloud Config(一):聊聊分布式配置中心 Spring Cloud Config

目錄

Spring Cloud Config(一):聊聊分布式配置中心 Spring Cloud Config

Spring Cloud Config(二):基于Git搭建配置中心

Spring Cloud Config(三):基于JDBC搭建配置中心

Spring Cloud Config(四):配置資訊動态重新整理

Spring Cloud Config(五):配置中心服務端Config Server源碼解析

Spring Cloud Config(六):配置中心用戶端Client源碼解析

Spring Cloud Config(七):配置中心自定義擴充

Spring Cloud Config(八):Client 端覆寫Server端配置屬性

Spring Cloud Config(九):Config Server 端配置檔案安全保護

Spring Cloud Config(十):快速響應失敗和重試機制

1、Spring Cloud Config 定義

Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. With the Config Server you have a central place to manage external properties for applications across all environments. The concepts on both client and server map identically to the Spring Environment and PropertySource abstractions, so they fit very well with Spring applications, but can be used with any application running in any language. As an application moves through the deployment pipeline from dev to test and into production you can manage the configuration between those environments and be certain that applications have everything they need to run when they migrate. The default implementation of the server storage backend uses git so it easily supports labelled versions of configuration environments, as well as being accessible to a wide range of tooling for managing the content. It is easy to add alternative implementations and plug them in with Spring configuration.

Spring Cloud Config 為分布式系統中的外部化配置提供伺服器和用戶端支援。 使用Config Server,您可以在所有環境中管理應用程式的外部屬性。用戶端和伺服器上的概念與Spring Environment和PropertySource抽象相同,是以它們非常适合Spring應用程式,但可以與任何語言運作的任何應用程式一起使用。當應用程式通過部署管道從開發到測試并進入生産時,您可以管理這些環境之間的配置,并確定應用程式具有遷移時需要運作的所有内容。他預設的伺服器存儲後端實作使用git,是以它可以輕松支援配置環境的标簽版本,并且可以通路各種用于管理内容的工具。 基于spring配置可以很友善的實作擴充。

2、為什麼選擇 Spring Cloud Config

那麼有人可能會問了,業界關于分布式配置中心有多種開源的元件,如攜程開源的 Apollo、 百度的 Disconf、淘寶的 Diamond 等,已經不少了,為啥還會誕生Spring Cloud Config呢?

應用服務除了實作系統功能,還需要連接配接資源和其它應用,經常需要調整服務的配置來改變應用的行為,如切換不同資料庫,設定功能開關等。随着微服務數量的不斷增加,需要系統具備可伸縮性和可擴充性,除此之外就是需要管理服務執行個體的配置資料。在開發階段配置資訊由各個服務自己管理,但是到了生産環境會給運維帶來很多不便。是以系統需要建立一個統一的配置管理中心,常見配置中心的實作方法有:

  • 寫死,缺點就是需要修改代碼
  • 放在xml等配置檔案中和應用一起打包,缺點就是配置修改需要重新打包和重新開機
  • 放在檔案系統中,缺點就是依賴作業系統
  • 配置到系統環境變量,缺點是需要大量的配置工作,不友善管理

個人看來,Spring Cloud Config 在以下幾方面還是有比較明顯的優勢,是以可能是為什麼要再造一個輪子的原因吧:

1、基于應用、環境、版本三個次元管理

應用(application)

每個配置都是屬于某一個應用的

環境(profile)

每個配置都是區分環境的,如dev, test, prod等

版本(label)

這個可能是一般的配置中心所缺乏的,就是對同一份配置的不同版本管理

Spring Cloud Config提供版本的支援,也就是說對于一個應用的不同部署執行個體,可以從服務端擷取到不同版本的配置,這對于一些特殊場景如:灰階釋出,A/B測試等提供了很好的支援。

2、多種存儲方式

基于Git存儲,一方面程式員非常熟悉,另一方面在部署上會非常簡單,而且借助于Git天生就能非常好的支援版本,同時它還支援其它的存儲如本地檔案、SVN、jdbc等

3、Spring無縫內建

它無縫支援Spring裡面Environment和PropertySource的接口

是以對于已有的Spring應用程式的遷移成本非常低,在配置擷取的接口上是完全一緻的

3、Spring Cloud Config 簡介

Spring Cloud Config(一):聊聊分布式配置中心 Spring Cloud Config

上圖簡要描述了一個普通Spring Cloud Config應用的場景。其中主要有以下幾個元件:

  • Config Client

Client很好了解,就是使用了Spring Cloud Config的應用

Spring Cloud Config提供了基于Spring的用戶端,應用隻要在代碼中引入Spring Cloud Config Client的jar包即可工作

  • Config Server

Config Server是需要獨立部署的一個web應用,它負責把git上的配置傳回給用戶端

  • Remote Git Repository

遠端Git倉庫,一般而言,我們會把配置放在一個遠端倉庫,通過git用戶端來管理配置

  • Local Git Repostiory

Config Server接到來自用戶端的配置擷取請求後,會先把遠端倉庫的配置clone到本地的臨時目錄,然後從臨時目錄讀取配置并傳回

4、總結

Spring Cloud Config 項目

  • 提供 服務端 和 用戶端 支援
  • 集中式 管理分布式環境下的應用配置
  • 基于 Spring 環境,無縫 與 Spring 應用內建
  • 可用于 任何 語言開發的程式
  • 預設實作基于 git 倉庫,可以進行 版本管理
  • 可替換 自定義實作

Spring Cloud Config Server 作為配置中心服務端

  • 拉取配置時更新 git 倉庫副本,保證是最新結果
  • 支援資料結構豐富,yml, json, properties 等
  • 配合 eureke 可實作服務發現,配合 cloud bus 可實作配置推送更新
  • 配置存儲基于 git 倉庫,可進行版本管理
  • 簡單可靠,有豐富的配套方案

Spring Cloud Config Client 預設用戶端實作

SpringBoot 項目不需要改動任何代碼,加入一個啟動配置檔案指明使用 ConfigServer 上哪個配置檔案即可

繼續閱讀