什麼是Spring Cloud Config?html
建立並運行一個Spring Cloud Config Servergit
創建一個Repositorygithub
建立並運行一個Spring Cloud Config Clientweb
什麼是配置信息?
一個Application中不僅是代碼,還須要鏈接資源和其它應用,常常有不少須要外部設置的項去調整Application行爲,如切換不一樣的數據庫,i18n國際化 等.應用中的會常常見到的xml,properties,yaml等就是配置信息.spring
常見的實現信息配置的方法:數據庫
硬編碼(缺點:須要修改代碼,風險大)apache
放在xml等配置文件中,和應用一塊兒打包(缺點:須要從新打包和重啓)bootstrap
文件系統中(缺點:依賴操做系統等)app
環境變量(缺點:有大量的配置須要人工設置到環境變量中,不便於管理,且依賴平臺)maven
雲端存儲(缺點:與其餘應用耦合)
Spring Cloud Config 就是雲端存儲配置信息的,它具備中心化,版本控制,支持動態更新,平臺獨立,語言獨立等特性.
Spring Cloud Config的原理如圖所示,真正的數據存在Git等repository中,Config Server去獲取相應的信息,而後開發給Client Application,相互間的通訊基於HTTP,TCP,UDP等協議.
1.建立一個名爲my-config-server的應用,並添加spring-cloud-starter-parent,spring-cloud-config-server依賴,pom信息具體以下
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Brixton.SR4</version> <relativePath/> </parent> <groupId>org.mmb.cloud</groupId> <artifactId>mmb-config-server</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> </project>
2.在Application主類上添加@EnableConfigServer註解,具體以下
package config; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; /** * Created by mmb on 2016/7/30. */ @EnableConfigServer @SpringBootApplication public class MMBConfigServerApplication { public static void main(String[] args) { SpringApplication.run(MMBConfigServerApplication.class, args); } }
3.去本身的GitHub上建立一個repository命名=MMBConfigData,並建立一個mmb-config-client.yml的配置文件,並添加一個key爲luck-word,value mmb,或者其它任何值.具體以下
--- lucky-word: mmb
4.回本身的工程,設置應用application.yml,配置spring.cloud.config.server.git.uri爲"https://github.com/"YOUR-GITHUB-ID"/"YOUR-REPOSITORY-NAME"",並設置端口server.port爲8001
server: port: 8001 spring: cloud: config: server: git: uri: https://github.com/mumubin/MMBConfigData searchPaths: data
5.啓動應用並打開地址http://localhost:8001/mmb-con...將會看到配置好的信息,個人以下
{ "name": "mmb-config-client", "profiles": [ "default" ], "label": "master", "version": "4d9240f45fecd34136f81683d44c2e144792af86", "propertySources": [ { "name": "https://github.com/mumubin/MMBConfigData/data/mmb-config-client.yml", "source": { "lucky-word": "mmb" } } ] }
1.建立一個名爲my-config-client的應用,並添加spring-cloud-starter-parent,spring-cloud-starter-config,spring-boot-starter-web依賴,pom信息具體以下
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Brixton.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Allow for automatic restarts when classpath contents change. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> <groupId>org.mmb.cloud</groupId> <artifactId>mmb-config-client</artifactId> <version>1.0-SNAPSHOT</version> </project>
2.建立bootstrap.yml在resource下,並設置spring.application.name,spring.cloud.config.uri,server.port信息,具體以下
spring: application: name: mmb-config-client cloud: config: uri: http://localhost:8001 --- server: port: 8002
注意這裏是bootstrap.yml而不是appliction.yml,由於bootstrap.yml會在應用啓動以前讀取,而spring.cloud.config.uri會影響應用啓動
3.建立一個Controller
@RestController public class LuckyWordController { @Value("${lucky-word}") String luckyWord; @RequestMapping("/lucky-word") public String showLuckyWord() { return "The lucky word is: " + luckyWord; } }
4.啓動Application,打開http://localhost:8002/lucky-word
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
The lucky word is: mmb
特別感謝 kennyk65
Spring Cloud 中文用戶組 31777218
Spring-Cloud-Config 官方文檔-中文譯本
Spring Cloud Netflix 官網文檔-中文譯本
本文實例github地址 Config Server Config Client