在上一篇文章講述zuul的時候,已經提到過,使用配置服務來保存各個服務的配置文件。它就是Spring Cloud Config。html
在分佈式系統中,因爲服務數量巨多,爲了方便服務配置文件統一管理,實時更新,因此須要分佈式配置中心組件。在Spring Cloud中,有分佈式配置中心組件spring cloud config ,它支持配置服務放在配置服務的內存中(即本地),也支持放在遠程Git倉庫中。在spring cloud config 組件中,分兩個角色,一是config server,二是config client。java
父maven工程省略,父pom文件:git
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.forezp</groupId> 8 <artifactId>sc-f-chapter6</artifactId> 9 <version>0.0.1-SNAPSHOT</version> 10 <packaging>pom</packaging> 11 12 <modules> 13 <module>config-server</module> 14 <module>config-client</module> 15 </modules> 16 17 <name>sc-f-chapter6</name> 18 <description>Demo project for Spring Boot</description> 19 20 <parent> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-parent</artifactId> 23 <version>2.0.3.RELEASE</version> 24 <relativePath/> 25 </parent> 26 27 28 <properties> 29 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 30 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 31 <java.version>1.8</java.version> 32 <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> 33 </properties> 34 35 <dependencies> 36 <dependency> 37 <groupId>org.springframework.boot</groupId> 38 <artifactId>spring-boot-starter-test</artifactId> 39 <scope>test</scope> 40 </dependency> 41 </dependencies> 42 43 <dependencyManagement> 44 <dependencies> 45 <dependency> 46 <groupId>org.springframework.cloud</groupId> 47 <artifactId>spring-cloud-dependencies</artifactId> 48 <version>${spring-cloud.version}</version> 49 <type>pom</type> 50 <scope>import</scope> 51 </dependency> 52 </dependencies> 53 </dependencyManagement> 54 55 <build> 56 <plugins> 57 <plugin> 58 <groupId>org.springframework.boot</groupId> 59 <artifactId>spring-boot-maven-plugin</artifactId> 60 </plugin> 61 </plugins> 62 </build> 63 </project>
建立一個spring-boot項目,取名爲config-server,其pom.xml以下:github
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.forezp</groupId> 7 <artifactId>config-server</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>config-server</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>com.forezp</groupId> 16 <artifactId>sc-f-chapter6</artifactId> 17 <version>0.0.1-SNAPSHOT</version> 18 </parent> 19 20 <dependencies> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-web</artifactId> 24 </dependency> 25 <dependency> 26 <groupId>org.springframework.cloud</groupId> 27 <artifactId>spring-cloud-config-server</artifactId> 28 </dependency> 29 </dependencies> 30 <build> 31 <plugins> 32 <plugin> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-maven-plugin</artifactId> 35 </plugin> 36 </plugins> 37 </build> 38 </project>
在程序的入口Application類加上@EnableConfigServer註解開啓配置服務器的功能,代碼以下:web
1 @SpringBootApplication 2 @EnableConfigServer 3 public class ConfigServerApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(ConfigServerApplication.class, args); 7 } 8 }
須要在程序的配置文件application.properties文件配置如下:spring
1 spring.application.name=config-server 2 server.port=8888 3 4 spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/ 5 spring.cloud.config.server.git.searchPaths=respo 6 spring.cloud.config.label=master 7 spring.cloud.config.server.git.username= 8 spring.cloud.config.server.git.password=
若是Git倉庫爲公開倉庫,能夠不填寫用戶名和密碼,若是是私有倉庫須要填寫,本例子是公開倉庫,放心使用。apache
遠程倉庫https://github.com/forezp/SpringcloudConfig/ 中有個文件config-client-dev.properties文件中有一個屬性:bootstrap
foo = foo version 3springboot
啓動程序:訪問http://localhost:8888/foo/dev服務器
1 {"name":"foo","profiles":["dev"],"label":"master", 2 "version":"792ffc77c03f4b138d28e89b576900ac5e01a44b","state":null,"propertySources":[]}
證實配置服務中心能夠從遠程程序獲取配置信息。
http請求地址和資源文件映射以下:
從新建立一個springboot項目,取名爲config-client,其pom文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.forezp</groupId> 7 <artifactId>config-client</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>config-client</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>com.forezp</groupId> 16 <artifactId>sc-f-chapter6</artifactId> 17 <version>0.0.1-SNAPSHOT</version> 18 </parent> 19 20 <dependencies> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-web</artifactId> 24 </dependency> 25 <dependency> 26 <groupId>org.springframework.cloud</groupId> 27 <artifactId>spring-cloud-starter-config</artifactId> 28 </dependency> 29 </dependencies> 30 31 <build> 32 <plugins> 33 <plugin> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-maven-plugin</artifactId> 36 </plugin> 37 </plugins> 38 </build> 39 40 41 </project>
其配置文件bootstrap.properties:
1 spring.application.name=config-client 2 spring.cloud.config.label=master 3 spring.cloud.config.profile=dev 4 spring.cloud.config.uri= http://localhost:8888/ 5 server.port=8881
spring.cloud.config.profile
spring.cloud.config.uri= http://localhost:8888/ 指明配置服務中心的網址。
程序的入口類,寫一個API接口「/hi」,返回從配置中心讀取的foo變量的值,代碼以下:
1 @SpringBootApplication 2 @RestController 3 public class ConfigClientApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(ConfigClientApplication.class, args); 7 } 8 9 @Value("${foo}") 10 String foo; 11 @RequestMapping(value = "/hi") 12 public String hi(){ 13 return foo; 14 } 15 }
打開網址訪問:http://localhost:8881/hi,網頁顯示:
foo version 3
這就說明,config-client從config-server獲取了foo的屬性,而config-server是從git倉庫讀取的,如圖:
本文源碼下載:
https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter6
http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html
感謝您的關注!可加QQ1羣:135430763,QQ2羣:454796847,QQ3羣:187424846。QQ羣進羣密碼:xttblog,想加微信羣的朋友,能夠微信搜索:xmtxtt,備註:「xttblog」,添加助理微信拉你進羣。備註錯誤不會贊成好友申請。再次感謝您的關注!後續有精彩內容會第一時間發給您!原創文章投稿請發送至532009913@qq.com郵箱。商務合做可添加助理微信進行溝通!