介紹
Spring Cloud Config是Spring Cloud團(tuán)隊(duì)創(chuàng)建的一個(gè)全新項(xiàng)目,用來為分布式系統(tǒng)中的基礎(chǔ)設(shè)施和微服務(wù)應(yīng)用提供集中化的外部配置支持,它分為服務(wù)端與客戶端兩個(gè)部分。其中服務(wù)端也稱為分布式配置中心,它是一個(gè)獨(dú)立的微服務(wù)應(yīng)用,用來連接配置倉庫并為客戶端提供獲取配置信息、加密/解密信息等訪問接口;而客戶端則是微服務(wù)架構(gòu)中的各個(gè)微服務(wù)應(yīng)用或基礎(chǔ)設(shè)施,它們通過指定的配置中心來管理應(yīng)用資源與業(yè)務(wù)相關(guān)的配置內(nèi)容,并在啟動的時(shí)候從配置中心獲取和加載配置信息。Spring Cloud Config實(shí)現(xiàn)了對服務(wù)端和客戶端中環(huán)境變量和屬性配置的抽象映射,所以它除了適用于Spring構(gòu)建的應(yīng)用程序之外,也可以在任何其他語言運(yùn)行的應(yīng)用程序中使用。由于Spring Cloud Config實(shí)現(xiàn)的配置中心默認(rèn)采用Git來存儲配置信息,所以使用Spring Cloud Config構(gòu)建的配置服務(wù)器,天然就支持對微服務(wù)應(yīng)用配置信息的版本管理,并且可以通過Git客戶端工具來方便的管理和訪問配置內(nèi)容。當(dāng)然它也提供了對其他存儲方式的支持,比如:SVN倉庫、本地化文件系統(tǒng)。
依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
配置文件
spring.application.name=config-server
server.port=1201
# git倉庫配置
spring.cloud.config.server.git.uri=https://github.com/zhumeilu/springcloud-config-repo-demo/
#spring.cloud.config.server.git.searchPaths=Chapter1-1-8/config-repo
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
Application.java
@EnableConfigServer //開啟配置中心服務(wù)
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
{label}對應(yīng)Git上不同的分支,默認(rèn)為master,{application}為配置的文件名稱,{profile}為環(huán)境
訪問http://localhost:1201/config-client/dev/master就可以看到配置文件了
客戶端
構(gòu)建一個(gè)簡單的springboot應(yīng)用
配置文件
spring.application.name=eureka-consumer
server.port=4444
eureka.instance.hostname=localhost
#服務(wù)注冊中心
#eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
spring.cloud.config.profile=dev
spring.cloud.config.name=config-client
spring.cloud.config.uri=http://localhost:1201/
spring.cloud.config.label=master
在controller中添加
@Value("${from}")
String from;
@GetMapping("/getFrom")
public String getFrom(){
return from;
}
訪問http://localhost:4444/getForm
得到配置文件中的form的值
在客戶端使用配置中心時(shí),spring.cloud.config.name和spring.application.name都可以確定配置文件名稱,最好使用spring.cloud.config.name=config-client來確定配置文件名稱,不要使用spring.application.name=config-client來確定。如果都存在的話,默認(rèn)使用spring.cloud.config.name
配置中心集群
將多個(gè)配置中心注冊為服務(wù),使其實(shí)現(xiàn)集群和負(fù)載均衡
服務(wù)端
依賴
在之前的基礎(chǔ)上添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
配置文件
在之前的基礎(chǔ)上添加注冊中心
# 配置服務(wù)注冊中心
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
Application.java
添加注解@EnableDiscoveryClient //注解使其成為服務(wù)
客戶端
配置文件
在原來的基礎(chǔ)是添加
#開啟通過服務(wù)來訪問Config Server的功能
spring.cloud.config.discovery.enabled=true
#指定Config Server注冊的服務(wù)名
spring.cloud.config.discovery.serviceId=config-server
當(dāng)git上的配置文件更新時(shí),可以在項(xiàng)目中添加spring-boot-starter-actuator模塊,在需要刷新的controller上添加注解@RefreshScope
然后訪問/refresh,需要注意的是springboot中默認(rèn)開啟安全策略,無法通過fidder或者postman之類的工具調(diào)用/refresh,所以需要設(shè)置management.security.enabled= false