每天學習一點點 編程PDF電子書、視頻教程免費下載:
http://www.shitanlife.com/code
一、簡介
Spring Cloud Config為分布式系統中的外部配置提供服務器和客戶端支持。使用Config Server,您可以為所有環境中的應用程序管理其外部屬性。它非常適合spring應用,也可以使用在其他語言的應用上。隨着應用程序通過從開發到測試和生產的部署流程,您可以管理這些環境之間的配置,並確定應用程序具有遷移時需要運行的一切。服務器存儲后端的默認實現使用git,因此它輕松支持標簽版本的配置環境,以及可以訪問用於管理內容的各種工具。
Spring Cloud Config服務端特性
- HTTP,為外部配置提供基於資源的API(鍵值對,或者等價的YAML內容)
- 屬性值的加密和解密(對稱加密和非對稱加密)
- 通過使用@EnableConfigServer在Spring boot應用中非常簡單的嵌入。
Config客戶端的特性(特指Spring應用)
- 綁定Config服務端,並使用遠程的屬性源初始化Spring環境。
- 屬性值的加密和解密(對稱加密和非對稱加密)
入門示例:
只要classpath下有Spring Boot Actuator和Spring Config Client,Spring Boot應用就會嘗試連接配置服務http://localhost:8888,這個地址是spring.cloud.config.uri的默認地址。如果你想修改這個地址,你可以在bootstrap.[yml或properties]中設置spring.cloud.config.uri或者通過系統屬性或者通過環境變量。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@Configuration
@EnableAutoConfiguration
@RestController
public
class
Application {
@Value
(
"${config.name}"
)
String name =
"World"
;
@RequestMapping
(
"/"
)
public
String home() {
return
"Hello "
+ name;
}
public
static
void
main(String[] args) {
SpringApplication.run(Application.
class
, args);
}
}
|
上面例子中的config.name可以來自本地的配置文件,也可以來自遠程的配置服務。默認情況下,遠程的配置服務將優先使用。
為了運行你自己的配置服務中心,你可以使用spring-cloud-config-server依賴,和@EnableConfigServer注解。如果你設置了spring.config.name=configserver,應用將會運行在8888端口,並且從一個樣本倉庫提供數據。你需要設置spring.cloud.config.server.git.uri來指定你自己的配置數據。默認的,它是一個git倉庫,也可以配置成本地的文件系統。
二、Spring Cloud Config服務端
服務器為外部配置(鍵稱值對或等效的YAML內容)提供了基於資源的HTTP。它可以在Spring Boot應用中使用@EnableConfigServer內嵌。例子如下:
1
2
3
4
5
6
7
8
|
@SpringBootApplication
@EnableConfigServer
public
class
SpringCloudConfigServerApplication {
public
static
void
main(String[] args) {
SpringApplication.run(SpringCloudConfigServerApplication.
class
, args);
}
}
|
像所有的Spring Boot應用一樣,它默認運行在8080端口,你可以通過多種方式將其切換到8888端口。最簡單的可以設置spring.config.name=configserver(在Config Server的jar包有一個configserver.yml),它設置了一個默認的配置倉庫。另外一種方式是使用你自己的application.properties,這也是小編推薦的方式:
1
2
|
server.port:
8888
spring.cloud.config.server.git.uri: git地址
|
git地址中是你的YAML或者properties文件。
環境倉庫
你想在哪里存儲配置數據?支持這種行為的策略是EnvironmentRepository,它服務於Environment實例。這個Environment是Spring Environment的一個淺副本。Environment通過3個變量被參數化。
- {application}映射客戶端的"spring.application.name"
- {profile}映射客戶端的"spring.profiles.active"(逗號分隔列表)
- {label}它是服務端的特性,標記版本的一組配置文件
倉庫的實現通常表現的像Spring boot加載配置文件一樣,"spring.config.name"等於{application}參數, "spring.profiles.active" 等於{profile}參數。profiles的優先規則和正常的規則是一樣的,活動的profiles優於默認的。如果有多個profiles,則最后一個勝出。
客戶端的配置實例:
1
2
3
4
5
|
spring:
application:
name: foo
profiles:
active: dev,mysql
|
在Spring Boot應用中,這些參數也可以通過環境變量或者命令行參數設置。
git后端
EnvironmentRepository的默認實現是使用git后端,它對管理更新、物理環境和審核更改非常的方便。要改變倉庫的地址,你可以在配置服務端設置"spring.cloud.config.server.git.uri"屬性(在application.properties文件中)。如果你用file:開頭設置它,它將從本地倉庫運行,這樣可以在沒有服務端的情況下非常快速和簡單的啟動。這種情況,服務端將直接在本地倉庫中運行。為了擴展配置服務並使它高可用,你需要把服務的所有實例指向同一個倉庫,因此只有共享文件系統可以工作。即使在這種情況下,最好使用共享文件系統存儲庫的ssh:協議,以便服務器可以將其克隆並使用本地工作副本作為緩存。
該倉庫的實現將HTTP資源中的{label}參數映射到git的標簽(提交id、分支名稱或者tag)。如果git分支或者tag名稱中包含“/”,則HTTP URL中的label要使用特殊字符“(_)”代替。例如:如果分支的名稱是foo/bar,則HTTP中的label的格式為foo(_)bar。這個特殊字符也可以用到{application}參數中。
git URI中的占位符
Spring Cloud Config Server支持在git URL中使用占位符,使用{application} 和 {profile}(如果使用{label},請記住它是使用在git標簽中的)。因此你可以輕松的支持“一個應用一個倉庫”的原則。如下:
1
2
3
4
5
6
|
spring:
cloud:
config:
server:
git:
uri: https:
//github.com/myorg/{application}
|
或者一個環境一個倉庫的原則,使用{profile}代替{application}。另外在{application}參數中使用特殊字符"(_)"可以支持多組織。
1
2
3
4
5
6
|
spring:
cloud:
config:
server:
git:
uri: https:
//github.com/{application}
|
{application}參數的格式為"organization(_)application"。
模式匹配和多倉庫
在{application}和{profile}參數中使用模式匹配可以支持更多復雜的需求。模式的格式是一組逗號分隔的{application}/{profile},其中的參數可以使用通配符。例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
spring:
cloud:
config:
server:
git:
uri: https:
//github.com/spring-cloud-samples/config-repo
repos:
simple: https:
//github.com/simple/config-repo
special:
pattern: special*/dev*,*special*/dev*
uri: https:
//github.com/special/config-repo
local:
pattern: local*
uri: file:/home/configsvc/config-repo
|
如果{application}/{profile}沒有匹配到任何模式,它將使用默認的倉庫地址:spring.cloud.config.server.git.uri。上面的例子中,"simple"倉庫匹配的是“simple/*”(它僅僅匹配一個倉庫simple,在所有的環境下)。"local"倉庫將匹配所有{application}的名字以“local”開頭的,並且也是在所有的環境下。“/*”前綴自動添加到所有沒有設置{profile}的模式中。
每一個倉庫也可以在子目錄下存儲配置文件,模式匹配也可以用於搜索這些目錄,需要制定searchPaths,如下:
1
2
3
4
5
6
7
|
spring:
cloud:
config:
server:
git:
uri: https:
//github.com/spring-cloud-samples/config-repo
searchPaths: foo,bar*
|
上面的例子中,將在foo和以bar開頭的目錄中,搜索配置文件。
默認地,服務器在第一次請求配置文件時克隆遠程的倉庫,服務器也可以配置在啟動的時候克隆倉庫,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
spring:
cloud:
config:
server:
git:
uri: https:
//git/common/config-repo.git
repos:
team-a:
pattern: team-a-*
cloneOnStart:
true
uri: http:
//git/team-a/config-repo.git
team-b:
pattern: team-b-*
cloneOnStart:
false
uri: http:
//git/team-b/config-repo.git
team-c:
pattern: team-c-*
uri: http:
//git/team-a/config-repo.git
|
在上面的例子team-a的倉庫將在服務端啟動時進行克隆,其他的倉庫將在第一次請求時克隆。
認證
如果遠程的git倉庫需要用戶名和密碼,可以參照下面的例子
1
2
3
4
5
6
7
8
|
spring:
cloud:
config:
server:
git:
uri: https:
//github.com/spring-cloud-samples/config-repo
username: trolley
password: strongpassword
|
到此,Spring Cloud Config服務端就介紹到這里,還有一些不常用的功能在這里就不介紹了,大家可以參照spring cloud官網。Spring Cloud Config服務端的代碼示例可以參照我的GitHub地址:https://github.com/bigbugliu/spring-cloud-config-server。
三、Spring Cloud Config 客戶端
Spring Boot應用可以立即使用Spring Config Server。只要在classpath中有Spring Cloud Config Client的jar包,這個應用就會請求配置的服務端。他將使用綁定的配置服務器(spring.cloud.config.uri中配置的)的屬性初始化spring環境。
在某些情況下,如果服務無法連接到配置服務器,則可能希望啟動服務失敗。如果這是所需的行為,請設置引導配置屬性spring.cloud.config.failFast=true
,客戶端將以異常停止。
如果您希望配置服務器在您的應用程序啟動時可能偶爾不可用,您可以要求它在發生故障后繼續嘗試。首先,您需要設置spring.cloud.config.failFast=true,然后您需要將spring-retry和spring-boot-starter-aop添加到您的類路徑中。默認行為是重試6次,初始退避間隔為1000ms,指數乘數為1.1,用於后續退避。您可以使用spring.cloud.config.retry.*配置屬性配置這些屬性(和其他)。