Spring Cloud Config
參考個人項目
參考個人項目 : (希望大家能給個star~)
https://github.com/FunriLy/springcloud-study/tree/master/%E6%A1%88%E4%BE%8B5
什么是 Spring Cloud Config?
配置管理工具包,讓你可以把配置放到遠程服務器,集中化管理集群配置,目前支持本地存儲、Git以及Subversion。
場景介紹:在一個 Application 中,很經常需要連接資源和其它應用,經常有很多需要外部設置的信息去調整Application行為。我們在實際開發應用中的會經常見到的xml、properties、yaml等就是配置信息,但這種做法有一定的缺陷:每次更新需要重新打包和重啟。
創建 Spring Cloud Config Server(Git 存儲)
-
這里我用了我原本的”服務注冊中心”(Eureka Server),將其改造為”配置中心”(Config Server)。
-
引入依賴
<!-- Config Server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
- 在啟動主類添加
@EnableConfigServer
注解,開啟 Config Server。
@SpringBootApplication @EnableEurekaServer @EnableConfigServer public class MySpringCloudApplication { public static void main(String[] args) { SpringApplication.run(MySpringCloudApplication.class, args); } }
-
在Github上創建一個項目,並在其中添加配置文件 config-client.properties,在里面添加一個屬性
config=hello world !
。 -
在
application.properties
中配置服務信息以及git信息(這里不包括了 Eureka Server 的配置,自行補充)
server.port=8761 spring.cloud.config.server.git.uri=https://github.com/FunriLy/springcloud-study/ spring.cloud.config.server.git.searchPaths=config-repo spring.cloud.config.server.git.username=Username spring.cloud.config.server.git.password=Password
- 啟動工程 Config Server。訪問 http://localhost:8761/config-client/default/ (關於這個URL請參考附錄),可以看到以下配置信息:
{ "name": "config-client", "profiles": [ "default" ], "label": null, "version": "af7ce2a15dcdea9dab42e6b44d37e401072382d8", "propertySources": [ { "name": "https://github.com/FunriLy/springcloud-study/config-repo/config-client.properties", "source": { "configword": "hello world !" } } ] }
創建一個Spring Cloud Config Client
-
這里我用到了原來的”服務提供者”(Eureka Client),將其改造為 Config Client。
-
在
resource
下創建bootstrap.properties
,並設置信息,具體如下:
spring.application.name=config-client spring.cloud.config.profile=default spring.cloud.config.label=master spring.cloud.config.uri=http://localhost:8761/
注意這里是bootstrap.properties而不是appliction.properties。 因為bootstrap.properties會在應用啟動之前讀取,而spring.cloud.config.uri會影響應用啟動
- 創建一個Controller來進行測試。
@RestController public class ConfigController { @Value("${configword}") String configword; @RequestMapping("/config") public String printfConfig(){ return "The Config Word Is : "+configword; } }
- 啟動 Config Client,訪問 http://localhost:1111/config 。就能看到:
The Config Word Is : hello world !
附錄
來源於 Spring Cloud Config 中文文檔。
URL與配置文件的映射關系
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
application是SpringApplication
的spring.config.name
,(一般來說’application’是一個常規的Spring Boot應用),profile是一個active的profile(或者逗號分隔的屬性列表),label是一個可選的git標簽(默認為”master”)。
比如,我的文件名是”config-client”,一般在Github上都是default環境,默認為master分支。所以就是/config-client/default/master
Config Server 配置文件
- spring.cloud.config.server.git.uri:配置git倉庫位置
- spring.cloud.config.server.git.searchPaths:配置倉庫路徑下的相對搜索位置,可以配置多個
- spring.cloud.config.server.git.username:訪問git倉庫的用戶名
- spring.cloud.config.server.git.password:訪問git倉庫的用戶密碼
Config Client 配置文件
- spring.application.name:對應前配置文件中的{application}部分
- spring.cloud.config.profile:對應前配置文件中的{profile}部分
- spring.cloud.config.label:對應前配置文件的git分支
- spring.cloud.config.uri:配置中心的地址
其他
在Config Server中,還有一種不使用Git的”native”的配置方式,這種方式是從本地classpath 或文件系統中加載配置文件(使用 “spring.cloud.config.server.native.searchLocations”配置項進行設置)。 加載Config Server 的”spring.profiles.active=native”配置項可以開啟native配置。如:
spring.profiles.active=native spring.cloud.config.server.native.searchLocations=file:D:/properties
注意 : 牢記使用file:
前綴來指示資源(默認沒有前綴是從classpath中去文件)。也可以嵌入${}
環境參數占位符,但是windows系統下使用絕對路徑,前綴后面需要多加個”/”。