1 原理
隨著服務(wù)越來(lái)越多,相應(yīng)的配置文件也會(huì)越來(lái)越多。我們需要將配置文件拿出來(lái)單獨(dú)管理, Spring Cloud Config對(duì)這種需求提供了支持。Spring Cloud Config分成兩個(gè)部分
- Config Server
- Config client
Config Server用來(lái)關(guān)聯(lián)外部配置, 并將獲取到的配置信息提供給客戶端使用。 Config client就是我們的各個(gè)微服務(wù)應(yīng)用,我們?cè)贑onfig client上指定Config Server的位置,Config client在啟動(dòng)的時(shí)候就會(huì)自動(dòng)去從Config Server獲取和加載配置信息。
Config Server可以從四個(gè)地方獲取配置信息,分別是:
- git:默認(rèn)值,表示去Git倉(cāng)庫(kù)讀取配置文件。
- subversion:表示去SVN倉(cāng)庫(kù)讀取配置文件。
- native:將會(huì)去本地的文件系統(tǒng)中讀取配置文件。
- vault:去Vault中讀取配置文件,Vault是一款資源控制工具,可對(duì)資源實(shí)現(xiàn)安全訪問(wèn)。
2 Config Server
創(chuàng)建一個(gè)spring boot項(xiàng)目,就叫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>
編寫(xiě)啟動(dòng)類,并增加注解@EnableConfigServer,代碼如下:
@SpringBootApplication
@EnableConfigServer
public class ServerApp {
public static void main(String[] args) {
new SpringApplicationBuilder(ServerApp.class).run(args);
}
}
本例使用git方式存儲(chǔ)配置文件,在Git上建一個(gè)repo,名稱為springcloud-learn,包括一個(gè)config文件夾,其中有四個(gè)文件,分別是:
- 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
啟動(dòng)啟動(dòng)類,通過(guò)/{application}/{profile}/{label}就能訪問(wèn)到我們的配置文件了,其中application表示配置文件的名字,對(duì)應(yīng)我們上面的配置文件就是app,profile表示環(huán)境,我們有dev、test、prod還有默認(rèn),label表示分支,默認(rèn)我們都是放在master分支上,我們?cè)跒g覽器上訪問(wèn)結(jié)果如下:
實(shí)際上配置中心還會(huì)通過(guò)git clone命令將配置文件在本地保存了一份,這樣可以確保在git倉(cāng)庫(kù)掛掉的時(shí)候我們的應(yīng)用還可以繼續(xù)運(yùn)行,此時(shí)我們斷掉網(wǎng)絡(luò),再訪問(wèn)http://localhost:8888/app/master,一樣還可以拿到數(shù)據(jù),此時(shí)的數(shù)據(jù)就是從本地獲取的。
默認(rèn)情況下,Config Server 克隆下來(lái)的文件保存在C:\Users<當(dāng)前用戶>\AppData\Local\Temp目錄下,
image.png
我們可以通過(guò)如下配置來(lái)修改: spring.cloud.config.server.git.basedir=E:\test\
先不要?jiǎng)?chuàng)建test文件夾,會(huì)自動(dòng)創(chuàng)建,當(dāng)你訪問(wèn)localhost:8888/app/master時(shí),會(huì)自動(dòng)拷貝到本地,如下:
image.png
3 config client
其實(shí)有點(diǎn)感覺(jué)config server像是git和config client之間的橋梁,或者像的網(wǎng)關(guān)。服務(wù)都通過(guò)config server獲取到git上的配置信息。
下面創(chuàng)建一個(gè)config client項(xiàng)目,就叫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>
編寫(xiě)啟動(dòng)類如下:
@SpringBootApplication
public class ClientApp {
public static void main(String[] args) {
new SpringApplicationBuilder(ClientApp.class).run(args);
}
}
編寫(xiě)配置文件,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/
編寫(xiě)control,如下:
public class TestController {
@Autowired
Environment env;
@RequestMapping("/feng")
public String sang() {
return env.getProperty("feng", "未定義");
}
}
訪問(wèn):http://localhost:2008/feng
有一點(diǎn)需要注意:server-client的配置名稱要是bootstrap.yml或者bootstrap.properties,因?yàn)閟erver-client讀取配置文件是通過(guò)引導(dǎo)程序來(lái)加載的,而引導(dǎo)程序是加載名為bootstrap的配置文件。也就說(shuō)當(dāng)在我們的spring boot項(xiàng)目中有一個(gè)application.yml的配置文件,其實(shí)在加載application.yml文件前,會(huì)先加載名為bootstrap的配置文件。
參考
- 楊恩雄 《瘋狂spring cloud微服務(wù)架構(gòu)實(shí)戰(zhàn)》