1 原理
隨著服務(wù)越來越多泻骤,相應(yīng)的配置文件也會越來越多。我們需要將配置文件拿出來單獨管理, Spring Cloud Config對這種需求提供了支持。Spring Cloud Config分成兩個部分
- Config Server
- Config client
Config Server用來關(guān)聯(lián)外部配置降盹, 并將獲取到的配置信息提供給客戶端使用。 Config client就是我們的各個微服務(wù)應(yīng)用谤辜,我們在Config client上指定Config Server的位置蓄坏,Config client在啟動的時候就會自動去從Config Server獲取和加載配置信息。
Config Server可以從四個地方獲取配置信息丑念,分別是:
- git:默認(rèn)值涡戳,表示去Git倉庫讀取配置文件。
- subversion:表示去SVN倉庫讀取配置文件脯倚。
- native:將會去本地的文件系統(tǒng)中讀取配置文件妹蔽。
- vault:去Vault中讀取配置文件,Vault是一款資源控制工具挠将,可對資源實現(xiàn)安全訪問。
2 Config Server
創(chuàng)建一個spring boot項目编整,就叫config-server舔稀,增加spring-cloud-config-server配置文件,pom文件如下:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
編寫啟動類,并增加注解@EnableConfigServer,代碼如下:
@SpringBootApplication
@EnableConfigServer
public class ServerApp {
public static void main(String[] args) {
new SpringApplicationBuilder(ServerApp.class).run(args);
}
}
本例使用git方式存儲配置文件掌测,在Git上建一個repo内贮,名稱為springcloud-learn,包括一個config文件夾汞斧,其中有四個文件夜郁,分別是:
- app-dev.properties,內(nèi)容 feng=dev config
- app-prod.properties粘勒,內(nèi)容 feng=prod config
- app.properties竞端,內(nèi)容 feng=default config
- app-test.properties,內(nèi)容 feng=test.config
配置文件如下:
server:
port: 8888
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/javaDuQing/springcloud-learn.git
search-paths: config
username: javaDuQing
password: xxx
啟動啟動類庙睡,通過/{application}/{profile}/{label}就能訪問到我們的配置文件了事富,其中application表示配置文件的名字技俐,對應(yīng)我們上面的配置文件就是app,profile表示環(huán)境统台,我們有dev雕擂、test、prod還有默認(rèn)贱勃,label表示分支井赌,默認(rèn)我們都是放在master分支上,我們在瀏覽器上訪問結(jié)果如下:
實際上配置中心還會通過git clone命令將配置文件在本地保存了一份贵扰,這樣可以確保在git倉庫掛掉的時候我們的應(yīng)用還可以繼續(xù)運行仇穗,此時我們斷掉網(wǎng)絡(luò),再訪問http://localhost:8888/app/master拔鹰,一樣還可以拿到數(shù)據(jù)仪缸,此時的數(shù)據(jù)就是從本地獲取的。
默認(rèn)情況下列肢,Config Server 克隆下來的文件保存在C:\Users<當(dāng)前用戶>\AppData\Local\Temp目錄下恰画,
我們可以通過如下配置來修改: spring.cloud.config.server.git.basedir=E:\test\
先不要創(chuàng)建test文件夾,會自動創(chuàng)建瓷马,當(dāng)你訪問localhost:8888/app/master時拴还,會自動拷貝到本地,如下:
3 config client
其實有點感覺config server像是git和config client之間的橋梁欧聘,或者像的網(wǎng)關(guān)片林。服務(wù)都通過config server獲取到git上的配置信息。
下面創(chuàng)建一個config client項目怀骤,就叫config-client费封,pom文件如下:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
編寫啟動類如下:
@SpringBootApplication
public class ClientApp {
public static void main(String[] args) {
new SpringApplicationBuilder(ClientApp.class).run(args);
}
}
編寫配置文件,uri是config-server的地址蒋伦,application.name相當(dāng)于/{application}/{profile}/{label}中的application弓摘。
server:
port: 2008
spring:
application:
name: app
cloud:
config:
profile: dev
label: master
uri: http://localhost:8888/
編寫control,如下:
public class TestController {
@Autowired
Environment env;
@RequestMapping("/feng")
public String sang() {
return env.getProperty("feng", "未定義");
}
}
有一點需要注意:server-client的配置名稱要是bootstrap.yml或者bootstrap.properties痕届,因為server-client讀取配置文件是通過引導(dǎo)程序來加載的韧献,而引導(dǎo)程序是加載名為bootstrap的配置文件。也就說當(dāng)在我們的spring boot項目中有一個application.yml的配置文件研叫,其實在加載application.yml文件前锤窑,會先加載名為bootstrap的配置文件。
參考
- 楊恩雄 《瘋狂spring cloud微服務(wù)架構(gòu)實戰(zhàn)》